Skip to content

Commit

Permalink
ENH: Reduce cyclomatic complexity introducing "spell_check_comment" f…
Browse files Browse the repository at this point in the history
…unction
  • Loading branch information
jcfr committed Jul 9, 2023
1 parent 938b850 commit 29d57f1
Showing 1 changed file with 67 additions and 56 deletions.
123 changes: 67 additions & 56 deletions codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,70 @@ def load_text_file(filename):
return output


def spell_check_comment(
spell_checker: SpellChecker,
c: comment_parser.common.Comment,
prefixes: list[str] = [],
output_lvl=2,
) -> list[str]:
"""Check comment and return list of identified issues if any."""

if output_lvl > 1:
print(f"Comment: {c}")
print(type(c))

mistakes = []
spell_checker.set_text(c.text())

for error in spell_checker:
if output_lvl > 1:
print(f"Error: {error.word}")

# Check if the bad word starts with a prefix.
# If so, spell check the word without that prefix.
for pre in prefixes:
if error.word.startswith(pre):
# check if the word is only the prefix
if len(pre) == len(error.word):
break

# remove the prefix
wrd = error.word[len(pre) :]
if output_lvl > 1:
print(f"Trying without '{pre}' prefix: {error.word} -> {wrd}")
try:
if spell_checker.check(wrd):
break
except BaseException:
print(f"Caught an exception for word {error.word} {wrd}")

# Try splitting camel case words and checking each sub-word
if output_lvl > 1:
print(f"Trying splitting camel case word: {error.word}")
sub_words = splitCamelCase(error.word)
if len(sub_words) > 1:
ok_flag = True
for s in sub_words:
if not spell_checker.check(s):
ok_flag = False
if ok_flag:
continue

# Check for possessive words
if error.word.endswith("'s"):
wrd = error.word[:-2]
if spell_checker.check(wrd):
continue

if output_lvl > 1:
msg = f"error: '{error.word}', suggestions: {spell_checker.suggest()}"
else:
msg = error.word
mistakes.append(msg)

return mistakes


def spell_check_file(filename, spell_checker, mime_type="", output_lvl=1, prefixes=[]):
"""Check spelling in ``filename``."""

Expand All @@ -103,62 +167,9 @@ def spell_check_file(filename, spell_checker, mime_type="", output_lvl=1, prefix
bad_words = []

for c in clist:
if output_lvl > 1:
print(f"Comment: {c}")
print(type(c))

mistakes = []
spell_checker.set_text(c.text())

for error in spell_checker:
if output_lvl > 1:
print(f"Error: {error.word}")

# Check if the bad word starts with a prefix.
# If so, spell check the word without that prefix.
#
for pre in prefixes:
if error.word.startswith(pre):
# check if the word is only the prefix
if len(pre) == len(error.word):
break

# remove the prefix
wrd = error.word[len(pre) :]
if output_lvl > 1:
print(f"Trying without '{pre}' prefix: {error.word} -> {wrd}")
try:
if spell_checker.check(wrd):
break
except BaseException:
print(f"Caught an exception for word {error.word} {wrd}")

# Try splitting camel case words and checking each sub-word

if output_lvl > 1:
print(f"Trying splitting camel case word: {error.word}")
sub_words = splitCamelCase(error.word)
if len(sub_words) > 1:
ok_flag = True
for s in sub_words:
if not spell_checker.check(s):
ok_flag = False
if ok_flag:
continue

# Check for possessive words

if error.word.endswith("'s"):
wrd = error.word[:-2]
if spell_checker.check(wrd):
continue

if output_lvl > 1:
msg = f"error: '{error.word}', suggestions: {spell_checker.suggest()}"
else:
msg = error.word
mistakes.append(msg)

mistakes = spell_check_comment(
spell_checker, c, prefixes=prefixes, output_lvl=output_lvl
)
if len(mistakes):
if output_lvl > 0:
print(f"\nLine number {c.line_number()}")
Expand Down

0 comments on commit 29d57f1

Please sign in to comment.