forked from ariesiitr/Lip-Reading
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspell.py
More file actions
57 lines (44 loc) · 2.07 KB
/
Copy pathspell.py
File metadata and controls
57 lines (44 loc) · 2.07 KB
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
import re
import string
from collections import Counter
def untokenize(words):
text = ' '.join(words)
step1 = text.replace("`` ", '"').replace(" ''", '"').replace('. . .', '...')
step2 = step1.replace(" ( ", " (").replace(" ) ", ") ")
step3 = re.sub(r' ([.,:;?!%]+)([ \'"`])', r"\1\2", step2)
step4 = re.sub(r' ([.,:;?!%]+)$', r"\1", step3)
step5 = step4.replace(" '", "'").replace(" n't", "n't").replace(
"can not", "cannot")
step6 = step5.replace(" ` ", " '")
return step6.strip()
def tokenize(text):
return re.findall(r"\w+|[^\w\s]", text, re.UNICODE)
class Spell(object):
def __init__(self, path):
self.dictionary = Counter(list(string.punctuation) + self.words(open(path).read()))
def words(self, text):
return re.findall(r'\w+', text.lower())
def P(self, word, N=None):
if N is None:
N = sum(self.dictionary.values())
return self.dictionary[word] / N
def correction(self, word):
return max(self.candidates(word), key=self.P)
def candidates(self, word):
return (self.known([word]) or self.known(self.edits1(word)) or self.known(self.edits2(word)) or [word])
def known(self, words):
return set(w for w in words if w in self.dictionary)
def edits1(self, word):
letters = 'abcdefghijklmnopqrstuvwxyz'
splits = [(word[:i], word[i:]) for i in range(len(word) + 1)]
deletes = [L + R[1:] for L, R in splits if R]
transposes = [L + R[1] + R[0] + R[2:] for L, R in splits if len(R)>1]
replaces = [L + c + R[1:] for L, R in splits if R for c in letters]
inserts = [L + c + R for L, R in splits for c in letters]
return set(deletes + transposes + replaces + inserts)
def edits2(self, word):
return (e2 for e1 in self.edits1(word) for e2 in self.edits1(e1))
def corrections(self, words):
return [self.correction(word) for word in words]
def sentence(self, sentence):
return untokenize(self.corrections(tokenize(sentence)))