@@ -81,6 +81,70 @@ def load_text_file(filename):
8181 return output
8282
8383
84+ def spell_check_comment (
85+ spell_checker : SpellChecker ,
86+ c : comment_parser .common .Comment ,
87+ prefixes : list [str ] = [],
88+ output_lvl = 2 ,
89+ ) -> list [str ]:
90+ """Check comment and return list of identified issues if any."""
91+
92+ if output_lvl > 1 :
93+ print (f"Comment: { c } " )
94+ print (type (c ))
95+
96+ mistakes = []
97+ spell_checker .set_text (c .text ())
98+
99+ for error in spell_checker :
100+ if output_lvl > 1 :
101+ print (f"Error: { error .word } " )
102+
103+ # Check if the bad word starts with a prefix.
104+ # If so, spell check the word without that prefix.
105+ for pre in prefixes :
106+ if error .word .startswith (pre ):
107+ # check if the word is only the prefix
108+ if len (pre ) == len (error .word ):
109+ break
110+
111+ # remove the prefix
112+ wrd = error .word [len (pre ) :]
113+ if output_lvl > 1 :
114+ print (f"Trying without '{ pre } ' prefix: { error .word } -> { wrd } " )
115+ try :
116+ if spell_checker .check (wrd ):
117+ break
118+ except BaseException :
119+ print (f"Caught an exception for word { error .word } { wrd } " )
120+
121+ # Try splitting camel case words and checking each sub-word
122+ if output_lvl > 1 :
123+ print (f"Trying splitting camel case word: { error .word } " )
124+ sub_words = splitCamelCase (error .word )
125+ if len (sub_words ) > 1 :
126+ ok_flag = True
127+ for s in sub_words :
128+ if not spell_checker .check (s ):
129+ ok_flag = False
130+ if ok_flag :
131+ continue
132+
133+ # Check for possessive words
134+ if error .word .endswith ("'s" ):
135+ wrd = error .word [:- 2 ]
136+ if spell_checker .check (wrd ):
137+ continue
138+
139+ if output_lvl > 1 :
140+ msg = f"error: '{ error .word } ', suggestions: { spell_checker .suggest ()} "
141+ else :
142+ msg = error .word
143+ mistakes .append (msg )
144+
145+ return mistakes
146+
147+
84148def spell_check_file (filename , spell_checker , mime_type = "" , output_lvl = 1 , prefixes = []):
85149 """Check spelling in ``filename``."""
86150
@@ -103,62 +167,9 @@ def spell_check_file(filename, spell_checker, mime_type="", output_lvl=1, prefix
103167 bad_words = []
104168
105169 for c in clist :
106- if output_lvl > 1 :
107- print (f"Comment: { c } " )
108- print (type (c ))
109-
110- mistakes = []
111- spell_checker .set_text (c .text ())
112-
113- for error in spell_checker :
114- if output_lvl > 1 :
115- print (f"Error: { error .word } " )
116-
117- # Check if the bad word starts with a prefix.
118- # If so, spell check the word without that prefix.
119- #
120- for pre in prefixes :
121- if error .word .startswith (pre ):
122- # check if the word is only the prefix
123- if len (pre ) == len (error .word ):
124- break
125-
126- # remove the prefix
127- wrd = error .word [len (pre ) :]
128- if output_lvl > 1 :
129- print (f"Trying without '{ pre } ' prefix: { error .word } -> { wrd } " )
130- try :
131- if spell_checker .check (wrd ):
132- break
133- except BaseException :
134- print (f"Caught an exception for word { error .word } { wrd } " )
135-
136- # Try splitting camel case words and checking each sub-word
137-
138- if output_lvl > 1 :
139- print (f"Trying splitting camel case word: { error .word } " )
140- sub_words = splitCamelCase (error .word )
141- if len (sub_words ) > 1 :
142- ok_flag = True
143- for s in sub_words :
144- if not spell_checker .check (s ):
145- ok_flag = False
146- if ok_flag :
147- continue
148-
149- # Check for possessive words
150-
151- if error .word .endswith ("'s" ):
152- wrd = error .word [:- 2 ]
153- if spell_checker .check (wrd ):
154- continue
155-
156- if output_lvl > 1 :
157- msg = f"error: '{ error .word } ', suggestions: { spell_checker .suggest ()} "
158- else :
159- msg = error .word
160- mistakes .append (msg )
161-
170+ mistakes = spell_check_comment (
171+ spell_checker , c , prefixes = prefixes , output_lvl = output_lvl
172+ )
162173 if len (mistakes ):
163174 if output_lvl > 0 :
164175 print (f"\n Line number { c .line_number ()} " )
0 commit comments