-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathft_spell_checker.py
95 lines (81 loc) · 3.28 KB
/
ft_spell_checker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# -*- coding: utf-8 -*-
# Copyright (c) 2020 GBC Networks Oy. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import sys
from ft_dbconnect import MysqlDB
# check python version >= 3.6
assert sys.version_info >= (3, 6)
class SpellChecker:
def __init__(self, config):
self.language = config['LANGUAGE'].lower()
if self.language == 'finnish':
import libvoikko
self.checker = libvoikko.Voikko(u'fi')
elif self.language == 'japanese':
print('Warning:'
' Spelling checker for Japanese is not yet implemented')
self.checker = False
else:
text_language = self.language.capitalize()
print('Warning:'
f' Spell checking for {text_language} is not supported')
self.checker = False
def spelling(self, word):
if self.language == 'finnish':
return self.checker.spell(word)
else:
return True
def morpho(self, word):
if self.language == 'finnish':
morpho = self.checker.analyze(word)
if len(morpho):
classification = morpho[0].get('CLASS', 'UNKNOWN')
plurality = morpho[0].get('NUMBER', 'UNKNOWN')
else:
classification = 'UNKNOWN'
plurality = 'UNKNOWN'
else:
classification = 'UNKNOWN'
plurality = 'UNKNOWN'
return classification, plurality
def run_spell_checker(config):
db = MysqlDB(config)
garbwords = db.get_all_words()
if not garbwords:
print('\tError: No garbage words found in database')
return False
# remove word from database if it passes spell checker
sc = SpellChecker(config)
count = 0
print('Removing words from database')
for json in garbwords:
count += 1
word = str(json.get('word'))
if sc.spelling(word):
db.words_remove_garbword(word)
db.commit()
print('.', end='') # show removed word as dot
if not count % 100:
print('') # print carriage return every 100 iterations
if __name__ == "__main__":
# execute only if run as a script
from ft_config import load_config
config = load_config()
run_spell_checker(config)