Skip to content

Commit c0e2f55

Browse files
committed
Black re-format
1 parent 8323f23 commit c0e2f55

File tree

6 files changed

+84
-10
lines changed

6 files changed

+84
-10
lines changed

comment_spell_check/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
""" comment_spell_check """
2+
23
from comment_spell_check import comment_spell_check
34

5+
46
def main():
57
"""Entry point for the application script"""
68
comment_spell_check.main()

comment_spell_check/comment_spell_check.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,13 @@ def spell_check_words(spell_checker: SpellChecker, words: list[str]):
113113
return False
114114
return True
115115

116-
def find_misspellings(spell: SpellChecker, line: str, verbose: bool = False) -> list[str]:
116+
117+
def find_misspellings(
118+
spell: SpellChecker, line: str, verbose: bool = False
119+
) -> list[str]:
117120
"""Find misspellings in a line of text."""
118121

119-
l2 = re.sub(r'[^a-zA-Z]', ' ', line)
122+
l2 = re.sub(r"[^a-zA-Z]", " ", line)
120123
words = l2.split()
121124

122125
mistakes = []
@@ -128,6 +131,7 @@ def find_misspellings(spell: SpellChecker, line: str, verbose: bool = False) ->
128131
mistakes.append(word)
129132
return mistakes
130133

134+
131135
def spell_check_comment(
132136
spell: SpellChecker,
133137
c: comment_parser.common.Comment,
@@ -191,9 +195,7 @@ def spell_check_comment(
191195
if output_lvl > 1:
192196
print("Trying splitting camel case word: {wrd}")
193197
sub_words = split_camel_case(wrd)
194-
if len(sub_words) > 1 and spell_check_words(
195-
spell, sub_words
196-
):
198+
if len(sub_words) > 1 and spell_check_words(spell, sub_words):
197199
valid = True
198200
break
199201
except TypeError:
@@ -459,6 +461,7 @@ def add_bibtex_words(spell, bibtex_files, verbose=False):
459461
print(f"Loading bibtex file: {bibtex_file}")
460462
bibtex_loader.add_bibtex(spell, bibtex_file, verbose=verbose)
461463

464+
462465
def main():
463466
"""comment_spell_check main function."""
464467
args = parse_args()
@@ -475,7 +478,7 @@ def main():
475478

476479
dict_list = build_dictionary_list(args)
477480

478-
spell = create_checker.create_checker(dict_list, output_lvl>1)
481+
spell = create_checker.create_checker(dict_list, output_lvl > 1)
479482

480483
if args.bibtex:
481484
add_bibtex_words(spell, args.bibtex, verbose=output_lvl > 1)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# he/him/his

comment_spell_check/lib/bibtex_loader.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
21
""" Load Bibtex files into a spell checking dictionary. """
32

43
import bibtexparser
54
import spellchecker
65

7-
def split_bibtex_name(name:str):
6+
7+
def split_bibtex_name(name: str):
88
"""
99
Split a Bibtex name, which is two words seperated by a number.
1010
"""
@@ -18,8 +18,8 @@ def split_bibtex_name(name:str):
1818
return words
1919

2020

21-
def add_bibtex(spell: spellchecker.SpellChecker, filename:str, verbose:bool=False):
22-
"""Update ``spell_checker`` spell checking dictionary with names
21+
def add_bibtex(spell: spellchecker.SpellChecker, filename: str, verbose: bool = False):
22+
"""Update ``spell`` spell checking dictionary with names
2323
from ``filename``, a Bibtex file."""
2424

2525
if verbose:
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#! /usr/bin/env python
2+
3+
""" Create a case sensitive spell checker with the English dictionary and
4+
additional dictionaries if provided.
5+
"""
6+
7+
import sys
8+
import importlib.resources
9+
import spellchecker
10+
11+
12+
def create_checker(dict_list="", verbose=False):
13+
"""Create a case sensitive spell checker with the English dictionary and
14+
additional dictionaries if provided."""
15+
16+
# create an empty SpellChecker object, because we want a case
17+
# sensitive checker
18+
checker = spellchecker.SpellChecker(language=None, case_sensitive=True)
19+
20+
# load the English dictionary
21+
lib_path = importlib.resources.files(spellchecker)
22+
english_dict = str(lib_path) + "/resources/en.json.gz"
23+
if verbose:
24+
print("Loading English dictionary from: ", english_dict)
25+
checker.word_frequency.load_dictionary(english_dict)
26+
27+
# load the additional dictionaries
28+
if not isinstance(dict_list, list):
29+
return checker
30+
if len(dict_list) > 0:
31+
for d in dict_list:
32+
if verbose:
33+
print("Loading additional dictionary from: ", d)
34+
checker.word_frequency.load_text_file(d)
35+
36+
return checker
37+
38+
39+
if __name__ == "__main__":
40+
41+
print(sys.argv[1:])
42+
spell = create_checker(sys.argv[1:], True)
43+
44+
# find those words that may be misspelled
45+
misspelled = spell.unknown(["something", "is", "hapenning", "here"])
46+
47+
for word in misspelled:
48+
print("\nMisspelled: ", word)
49+
# Get the one `most likely` answer
50+
print(spell.correction(word))
51+
52+
# Get a list of `likely` options
53+
print(spell.candidates(word))
54+
55+
# test if case sensitive checking from the additional dictionaries works
56+
my_words = [
57+
"Zsize",
58+
"Zuerich",
59+
"accesor",
60+
"accessor",
61+
"zsize",
62+
"zuerich",
63+
"sdfasdfas",
64+
]
65+
66+
for w in my_words:
67+
print(w, w in spell)

tests/test_comment_spell_check.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,6 @@ def test_bibtex(self):
100100
)
101101
self.assertEqual(runresult.returncode, 0, runresult.stdout)
102102

103+
103104
if __name__ == "__main__":
104105
unittest.main()

0 commit comments

Comments
 (0)