Skip to content

Commit

Permalink
fix: grammar issue regarding parsing of strings
Browse files Browse the repository at this point in the history
This commit fixes #21 where a full stop would be followed
immediately by a character.

A function (along with a test) has been implemented to fix this.

Issues: #10, #21

Authored-by: Joshua Rose <[email protected]>
  • Loading branch information
H4ppy-04 committed May 25, 2023
1 parent 942f998 commit 8dc3132
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
31 changes: 25 additions & 6 deletions src/urban.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ def fetch_word_from_remote(_word: str) -> dict[str, str] | None:
if not isinstance(words_as_str, str) or words_as_str == "":
words_as_str = "Definition not found or not available."
print(f"Debug (words_as_str variable): {words_as_str}")
else:
words_as_str = insert_space_after_fullstop(list(words_as_str))

# Return definition, author, date all as dict
post_author = get_author_from_soup(_soup)
Expand All @@ -525,18 +527,35 @@ def fetch_word_from_remote(_word: str) -> dict[str, str] | None:
return {"definition": words_as_str, "author": post_author, "date": "Unknown"}


def insert_space_after_fullstop(text: list[str]) -> str:
"""
Detects if a letter immediately follows a full stop and inserts a space after the full stop for grammatical correctness.
Parameters:
- text (list[str]): The input text to process.
Returns:
- str: The modified text with spaces inserted after full stops.
"""

for n in range(0, len(list(text))-1):
if text[n] == "." and text[n+1] != " ":
text.insert(n+1, " ")
return "".join(text)


def main():
"""main as global function called from dunder condition
"""main as global function called from dunder condition.
Description:
Fetches and printsdefinition, author and date
Raises:
Warning if words is None
TypeError if dictionary is invalid
- Warning if words is None
- TypeError if dictionary is invalid
Returns:
None
- None
"""

word = join_words()
Expand All @@ -553,8 +572,8 @@ def main():
definition, author, date = return_dict.values()

print(definition)
print(f"Defined by {colorama.Fore.BLUE}{author}{colorama.Fore.RESET}")
print(f"\nDate: {date}")
print(f"\nDefined by {colorama.Fore.BLUE}{author}{colorama.Fore.RESET}")
print(f"Date: {date}")

deinit_sys_exit()

Expand Down
13 changes: 12 additions & 1 deletion tests/test_unit_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import unittest
import random

from bs4 import BeautifulSoup, NavigableString, Tag
from bs4 import BeautifulSoup

sys.path.insert(0, os.getcwd())

from src.urban import (
assert_soup_and_index_valid,
get_found_word_from_soup,
get_soup_object_from_word,
insert_space_after_fullstop,
)


Expand Down Expand Up @@ -59,3 +60,13 @@ def test_word_from_soup_raises_index_error(self):
word_soup_raises_key_error.get_text(strip=False), "html.parser"
)
)

def test_space_insert_after_fullstop(self):
"""Test that a full stop before a character has a space inserted after
this function has been run. (for grammar)
"""

incorrect_string = "Four brown ferrets.Five fantastic fairies."
correct_string = "Four brown ferrets. Five fantastic fairies."

self.assertEqual(insert_space_after_fullstop(list(incorrect_string)), correct_string)

0 comments on commit 8dc3132

Please sign in to comment.