-
Notifications
You must be signed in to change notification settings - Fork 3
/
correct.py
56 lines (49 loc) · 1.64 KB
/
correct.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
from collections import defaultdict
import string
import readline
import dill
import pybktree
import editdistance
from nltk import sent_tokenize, wordpunct_tokenize
import config
from probabilistic_distance import probabilistic_distance
from learn import tokenize_sentence, generalize_tokens
from Viterbi import Viterbi
def rebuild(tokens, correct_tokens):
rebuilt_tokens = correct_tokens.copy()
for i, corr_token in enumerate(correct_tokens):
if corr_token == "PERSON_NAME":
rebuilt_tokens[i] = tokens[i]
return rebuilt_tokens
class Corrector():
def load_model(self):
print("Loading model")
model = dill.load(open(f"{config.MODEL}/model.dill", 'rb'))
words = model['words']
words_inverse = model['words_inverse']
tree = model['tree']
viterbi = Viterbi(words, words_inverse, tree)
print("Ready.")
self.viterbi = viterbi
self.words = words
self.words_inverse = words_inverse
self.tree = tree
def correct(self, text):
sentences = sent_tokenize(text)
corrected_sentences = []
for sentence in sentences:
corrected_sentences.append(" ".join(self.correct_sentence(sentence)))
return corrected_sentences
def correct_sentence(self, sentence):
tokens = tokenize_sentence(sentence)
generalized_tokens = generalize_tokens(tokens)
corrected_tokens = self.viterbi.run(generalized_tokens)
rebuilt_tokens = rebuild(tokens, corrected_tokens)
return rebuilt_tokens
if __name__ == "__main__":
corrector = Corrector()
corrector.load_model()
while True:
text = input(">>> ")
corrected_sentences = corrector.correct(text)
[print(sent) for sent in corrected_sentences]