Debian Codegraph @

lib/spellcheck_enchant.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # QBrz - Qt frontend to Breezy commands
4 # Copyright (C) 2008 Lukáš Lalinský <[email protected]om>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 import re
21 import enchant
22 from enchant.checker import SpellChecker
23 from enchant.tokenize import EmailFilter, URLFilter, Filter, tokenize
24
25
26 class camel_case_tokenize(tokenize):
27
28 def __next__(self):
29 offset = self.offset
30 text = self._text[offset:]
31 if not text:
32 raise StopIteration()
33 match = re.match(br'(\w+?)[A-Z]', text)
34 if match is None:
35 word = text
36 else:
37 word = match.group(1)
38 self.offset += len(word)
39 return word, offset
40
41
42 class CamelCaseFilter(Filter):
43
44 def _split(self, word):
45 return camel_case_tokenize(word)
46
47
48 class EnchantSpellChecker(object):
49
50 def __init__(self, language):
51 try:
52 self.dict = enchant.Dict(language)
53 self.checker = SpellChecker(language, filters=[EmailFilter, URLFilter, CamelCaseFilter])
54 except enchant.DictNotFoundError:
55 self.checker = None
56
57 def check(self, text):
58 if self.checker is None:
59 return
60 self.checker.set_text(text)
61 for err in self.checker:
62 yield err.wordpos, len(err.word)
63
64 def suggest(self, text):
65 return self.dict.suggest(text)
66
67 @classmethod
68 def list_languages(cls):
69 return list(set(lang.replace("_", "-") for lang in enchant.list_languages()))