forked from shibing624/pycorrector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spell.py
311 lines (269 loc) · 9.34 KB
/
spell.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# -*- coding: utf-8 -*-
# Author: XuMing <xuming624@qq.com>
# Brief:
import codecs
import kenlm
import math
import os
from collections import Counter
import numpy as np
import config
from util import tokenize, preprocess, load_pkl, dump_pkl
bigram_path = 'data/kenlm/zhwiki_bigram.klm'
bigram = kenlm.Model(bigram_path)
print('Loaded bigram language model from {}'.format(bigram_path))
trigram_path = 'data/kenlm/zhwiki_trigram.klm'
trigram = kenlm.Model(trigram_path)
print('Loaded trigram language model from {}'.format(trigram_path))
text_path = 'data/train_input.txt'
text_counter_path = 'data/train_input_counter.pkl'
# 字频统计
if os.path.exists(text_counter_path):
char_counter = load_pkl(text_counter_path)
else:
print('generate counter from text file:', text_path)
char_counter = Counter((codecs.open(text_path, 'r', encoding='utf-8').read()))
dump_pkl(char_counter, text_counter_path)
def load_same_pinyin(path, sep='\t'):
"""
加载同音字
:param path:
:return:
"""
result = dict()
if not os.path.exists(path):
print("file not exists:", path)
return result
with codecs.open(path, 'r', encoding='utf-8') as f:
for line in f:
parts = line.strip().split(sep)
if parts and len(parts) > 2:
key_char = parts[0]
same_pron_same_tone = set(list(parts[1]))
same_pron_diff_tone = set(list(parts[2]))
value = same_pron_same_tone.union(same_pron_diff_tone)
if len(key_char) > 1 or not value:
continue
result[key_char] = value
return result
def load_same_stroke(path, sep='\t'):
"""
加载形似字
:param path:
:param sep:
:return:
"""
result = dict()
if not os.path.exists(path):
print("file not exists:", path)
return result
with codecs.open(path, 'r', encoding='utf-8') as f:
for line in f:
parts = line.strip().split(sep)
if parts and len(parts) > 1:
key_char = parts[0]
result[key_char] = set(list(parts[1]))
return result
same_pinyin_text_path = 'data/same_pinyin.txt'
same_pinyin_model_path = 'data/same_pinyin.pkl'
# 同音字
if os.path.exists(same_pinyin_model_path):
same_pinyin = load_pkl(same_pinyin_model_path)
else:
print('load same pinyin from text file:', same_pinyin_text_path)
same_pinyin = load_same_pinyin(same_pinyin_text_path)
dump_pkl(same_pinyin, same_pinyin_model_path)
# 形似字
same_stroke_text_path = 'data/same_stroke.txt'
same_stroke_model_path = 'data/same_stroke.pkl'
if os.path.exists(same_stroke_model_path):
same_stroke = load_pkl(same_stroke_model_path)
else:
print('load same stroke from text file:', same_stroke_text_path)
same_stroke = load_same_stroke(same_stroke_text_path)
dump_pkl(same_stroke, same_stroke_model_path)
def get_same_pinyin(char):
"""
取同音字
:param char:
:return:
"""
return same_pinyin.get(char, set())
def get_same_stroke(char):
"""
取形似字
:param char:
:return:
"""
return same_stroke.get(char, set())
def get_model(n):
return {2: bigram, 3: trigram, }.get(n, bigram)
def get_ngram_score(chars, model=bigram):
return model.score(' '.join(chars), bos=False, eos=False)
def mad_score(scores, ratio=0.6745):
"""
平均绝对离差值
:param ratio:
:param scores:
:return: median absolute deviation (MAD) score
"""
scores = np.array(scores)
if len(scores.shape) == 1:
scores = scores[:, None]
median = np.median(scores, axis=0) # get median of all scores
margin_median = np.sqrt(np.sum((scores - median) ** 2, axis=-1)) # deviation from the median
med_abs_deviation = np.median(margin_median)
y_score = ratio * margin_median / med_abs_deviation
return scores, med_abs_deviation, y_score, median
def _get_maybe_error_index(scores, y_score, median, threshold=1.4):
"""
取疑似错字的位置,通过平均绝对离差(MAD)
:param scores: np.array
:param threshold: 阈值越小,得到疑似错别字越多
:return:
"""
scores = scores.flatten()
maybe_error_indices = np.where((y_score > threshold) & (scores < median))
maybe_error_scores = scores[maybe_error_indices]
return list(maybe_error_indices[0]), maybe_error_scores
def overlap(l1, l2):
"""
检测两个列表中是否有重复值
:param l1: lists
:param l2:
:return:
"""
if l1[0] < l2[0]:
if l1[1] <= l2[0]:
return False
else:
return True
elif l1[0] == l2[0]:
return True
else:
if l1[0] >= l2[1]:
return False
else:
return True
def overlap_ranges(range1, range2):
range_set = set()
for i in range2:
for j in range1:
if overlap(j, i):
range_set.add(tuple(i))
return [list(overlap_range) for overlap_range in range_set]
def merge_ranges(ranges):
"""
合并
:param ranges:
:return:
"""
result = []
ranges.sort()
temp = ranges[0][:]
for start, end in ranges:
if start <= temp[1]:
temp[1] = max(temp[1], end)
else:
result.append(temp[:])
temp[0] = start
temp[1] = end
result.append(temp[:])
return result
def score_sentence(sentence):
ngram_words = []
ngram_scores = []
ngram_avg_scores = []
for n in [2, 3]:
words = []
scores = []
for i in range(len(sentence) - n + 1):
word = sentence[i:i + n]
words.append(word)
score = get_ngram_score(word, model=get_model(n))
scores.append(score)
ngram_words.append(words)
ngrams_scores = list(zip(words, [round(score, 3) for score in scores]))
print(ngrams_scores)
ngram_scores.append(scores)
# 移动窗口补全得分
for _ in range(n - 1):
scores.insert(0, scores[0])
scores.append(scores[-1])
avg_scores = [sum(scores[i:i + n]) / len(scores[i:i + n]) for i in range(len(sentence))]
ngram_avg_scores.append(avg_scores)
# 取拼接后的平均得分
sent_scores = list(np.average(np.array(ngram_avg_scores), axis=0))
scores, mad, y_score, median = mad_score(sent_scores)
maybe_error_indices, _ = _get_maybe_error_index(scores, y_score, median)
merge_range = []
if maybe_error_indices:
merge_range = merge_ranges([[index, index + 1] for index in maybe_error_indices])
return sent_scores, ngrams_scores, merge_range
def get_frequency(char, counter, total):
"""
取字符在样本中的词频
:param char:
:return:
"""
return counter[char] / total
def generate_chars(c, fraction=2):
"""
取音似、形似字
:param c:
:param fraction:
:return:
"""
confusion_char_set = get_same_pinyin(c).union(get_same_stroke(c))
if not confusion_char_set:
confusion_char_set = {c}
confusion_char_set.add(c)
confusion_char_list = list(confusion_char_set)
all_confusion_char = sorted(confusion_char_list, key=lambda k: \
get_frequency(k, char_counter, sum(char_counter.values())),
reverse=True)
return all_confusion_char[:len(confusion_char_list) // fraction + 1]
def correct_chars(sentence, start_index, end_index):
"""
纠正错字,逐字处理
:param sentence:
:param start_index:
:param end_index:
:return: corrected characters 修正的汉字
"""
assert end_index > start_index, "end index must be more than start index"
chars = sentence[start_index:end_index]
for i, c in enumerate(chars):
# 取得所有可能正确的汉字
maybe_chars = generate_chars(c)
print('num of possible replacements for {} is {}'.format(c, len(maybe_chars)))
before = sentence[:start_index] + chars[:i]
after = chars[i + 1:] + sentence[end_index:]
correct_char = max(maybe_chars, key=lambda k: get_ngram_score(before + k + after) + math.log(5) ** (k == c))
chars = chars[:i] + correct_char + chars[i + 1:]
return chars
def correct(sentence):
sentence = preprocess(sentence)
tokens = tokenize(sentence)
print('segment sentens is:', ''.join([str(token) for token in tokens]))
seg_range = [[token[1], token[2]] for token in tokens]
_, _, maybe_error_range = score_sentence(sentence)
maybe_error_ranges = []
if maybe_error_range:
print('maybe error range:', maybe_error_range)
maybe_error_ranges = merge_ranges(overlap_ranges(maybe_error_range, seg_range))
for range in maybe_error_ranges:
start_index, end_index = range
print('maybe error words:', sentence[start_index:end_index])
corrected_words = correct_chars(sentence, start_index, end_index)
print('corrected words:', corrected_words)
sentence = sentence[:start_index] + corrected_words + sentence[end_index:]
return sentence, maybe_error_ranges
def main():
line = '我们现今所使用的大部分舒学符号' # ,你们用的什么婊点符号
print('input sentence is:', line)
corrected_sent, correct_ranges = correct(line)
print('corrected_sent:', corrected_sent)
print('correct_ranges:', correct_ranges)
if __name__ == '__main__':
main()