py/gadict_spellcheck.py
changeset 662 a0ef60715efe
parent 660 5305f170237d
child 691 cc362c8d17d4
equal deleted inserted replaced
661:bd74a7baf7dd 662:a0ef60715efe
     7 ################################################################
     7 ################################################################
     8 
     8 
     9 class EofReached (Exception):
     9 class EofReached (Exception):
    10     pass
    10     pass
    11 
    11 
    12 class EmptyChecker:
       
    13     def __init__(self):
       
    14         pass
       
    15     def check(self, word):
       
    16         pass
       
    17 
       
    18 class GadictSpellChecker:
    12 class GadictSpellChecker:
    19 
    13 
    20     SEPARATOR_RE = regex.compile(u"^__$")
    14     SEPARATOR_RE = regex.compile(u"^__$")
    21     EMPTY_RE = regex.compile( u"^$" )
    15     EMPTY_RE = regex.compile( u"^$" )
    22     HEADWORD_ATTR_RE = regex.compile( u"^ " )
    16     HEADWORD_ATTR_RE = regex.compile( u"^ " )
    23 
    17 
    24     def _get_checker(self, lang):
    18     def _add_checker(self, lang):
    25         try:
    19         try:
    26             dic = enchant.Dict(lang)
    20             self.dicts.append(enchant.Dict(lang))
    27         except enchant.errors.DictNotFoundError:
    21         except enchant.errors.DictNotFoundError:
    28             print("Dictionary '{:s}' is not found...".format(lang))
    22             print("Dictionary '{:s}' is not found...".format(lang))
    29             dic = EmptyChecker()
       
    30         return dic
       
    31 
    23 
    32     def __init__(self, stream, fname):
    24     def __init__(self, stream, fname):
    33         self.stream = stream
    25         self.stream = stream
    34         self.fname = fname
    26         self.fname = fname
    35         self.lineno = 0
    27         self.lineno = 0
    36         self.dict_us = self._get_checker('en_US')
    28         self.dicts = []
    37         self.dict_gb = self._get_checker('en_GB')
    29         self._add_checker('en_US')
    38         self.dict_au = self._get_checker('en_AU')
    30         self._add_checker('en_GB')
    39         self.dict_ca = self._get_checker('en_CA')
    31         # self._add_checker('en_AU')
       
    32         # self._add_checker('en_CA')
    40 
    33 
    41     def _readline(self):
    34     def _readline(self):
    42         line = self.stream.readline()
    35         line = self.stream.readline()
    43         if len(line) == 0:
    36         if len(line) == 0:
    44             raise EofReached
    37             raise EofReached
    65                 m = self.HEADWORD_ATTR_RE.match(line)
    58                 m = self.HEADWORD_ATTR_RE.match(line)
    66                 if m:
    59                 if m:
    67                     continue
    60                     continue
    68                 line = line.strip()
    61                 line = line.strip()
    69                 for word in regex.split("[ ,]+", line):
    62                 for word in regex.split("[ ,]+", line):
    70                     if self.dict_us.check(word) or self.dict_gb.check(word) or self.dict_au.check(word) or self.dict_ca.check(word):
    63                     if any([dic.check(word) for dic in self.dicts]):
    71                         continue
    64                         continue
    72                     print("""{:s}:{:d}: "{:s}" is misspelled""".format(self.fname, self.lineno, word))
    65                     print("""{:s}:{:d}: "{:s}" is misspelled""".format(self.fname, self.lineno, word))
    73 
    66 
    74     def check(self):
    67     def check(self):
    75         try:
    68         try: