1- #! /usr/bin/env
1+ #!/usr/bin/env python3
22
33import sys
44import os
55import glob
66import argparse
77import re
8+ from pathlib import Path
89
910from enchant .checker import SpellChecker
1011from enchant .tokenize import EmailFilter , URLFilter
@@ -103,7 +104,7 @@ def spell_check_file(filename, spell_checker, mimetype='',
103104 if ok_flag :
104105 continue
105106
106- # Check for possesive words
107+ # Check for possessive words
107108
108109 if error .word .endswith ("'s" ):
109110 wrd = error .word [:- 2 ]
@@ -190,14 +191,15 @@ def add_dict(enchant_dict, filename):
190191 enchant_dict .add_to_pwl (wrd )
191192
192193
193- if __name__ == '__main__' :
194-
194+ def main ():
195195 args = parse_args ()
196196 # print(args)
197197
198- thisdir = os .path .dirname (os .path .abspath (__file__ ))
198+ initial_dct = Path (__file__ ) / 'additional_dictionary.txt'
199+ if not initial_dct .exists ():
200+ initial_dct = None
199201
200- sitk_dict = DictWithPWL ('en_US' , thisdir + '/additional_dictionary.txt' )
202+ sitk_dict = DictWithPWL ('en_US' , initial_dct )
201203
202204 if args .dict is not None :
203205 for d in args .dict :
@@ -233,7 +235,7 @@ def add_dict(enchant_dict, filename):
233235 if os .path .isdir (f ):
234236
235237 # f is a directory, so search for files inside
236- dir_entries = glob .glob (f + '/**/*' + args .suffix , recursive = True )
238+ dir_entries = glob .glob (f + '/**/*' + args .suffix , recursive = True )
237239
238240 # spell check the files found in f
239241 for x in dir_entries :
@@ -247,7 +249,7 @@ def add_dict(enchant_dict, filename):
247249 result = spell_check_file (x , spell_checker ,
248250 output_lvl = output_lvl ,
249251 prefixes = prefixes )
250- bad_words = sorted (bad_words + result )
252+ bad_words = sorted (bad_words + result )
251253 else :
252254
253255 if exclude_check (f , args .exclude ):
@@ -258,7 +260,7 @@ def add_dict(enchant_dict, filename):
258260 result = spell_check_file (f , spell_checker ,
259261 output_lvl = output_lvl , prefixes = prefixes )
260262
261- bad_words = sorted (bad_words + result )
263+ bad_words = sorted (bad_words + result )
262264
263265 if not args .miss :
264266 print ("\n Bad words\n " )
@@ -268,12 +270,16 @@ def add_dict(enchant_dict, filename):
268270 if x [0 ] == prev :
269271 sys .stdout .write ('.' )
270272 continue
271- print ("\n " , x [0 ],": " ,x [1 ], ", " , x [2 ], sep = '' )
273+ print ("\n " , x [0 ], ": " , x [1 ], ", " , x [2 ], sep = '' )
272274 prev = x [0 ]
273275
274276 if not args .miss :
275277 print ("" )
276278
277- print (len (bad_words ), "misspellings found" )
278-
279+ print (len (bad_words ), "misspellings found" )
280+
279281 sys .exit (len (bad_words ))
282+
283+
284+ if __name__ == '__main__' :
285+ main ()
0 commit comments