-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_check.py
34 lines (28 loc) · 1.05 KB
/
edit_check.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This file is for check edit distance
"""
import pickle
import gzip
from pypinyin import lazy_pinyin, Style
def edits1(phrase):
"All edits that are one edit away from `phrase`."
# phrase = phrase.decode("utf-8")
splits = [(phrase[:i], phrase[i:]) for i in range(len(phrase) + 1)]
deletes = [L + R[1:] for L, R in splits if R]
deletes2 = [L + R[2:] 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 cn_words_dict]
inserts = [L + c + R for L, R in splits for c in cn_words_dict]
# return transposes
return set(deletes + deletes2 + transposes + replaces + inserts)
#with open('checker.pickle') as cp:
try:
f = gzip.open('checker.pickle', 'rb')
d = pickle.loads(f.read())
except IOError:
f = open('checker.pickle', 'rb')
d = pickle.loads(f.read())
f.close()
cn_words_dict = d['words']