-
Notifications
You must be signed in to change notification settings - Fork 49
/
detector.py
435 lines (401 loc) · 16.1 KB
/
detector.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# -*- coding: utf-8 -*-
# Author: XuMing <[email protected]>
# Brief: error word detector
import codecs
import os
import re
import time
import numpy as np
import config
from utils.get_file import get_file
from utils.logger import logger
from utils.text_utils import uniform, is_alphabet_string, convert_to_unicode, is_chinese_string
from utils.tokenizer import Tokenizer
# \u4E00-\u9FD5a-zA-Z0-9+#&\._ : All non-space characters. Will be handled with re_han
# \r\n|\s : whitespace characters. Will not be handled.
re_han = re.compile("([\u4E00-\u9FD5a-zA-Z0-9+#&]+)", re.U)
re_skip = re.compile("(\r\n\\s)", re.U)
class ErrorType(object):
# error_type = {"confusion": 1, "word": 2, "char": 3}
confusion = 'confusion'
word = 'word'
char = 'char'
class Detector(object):
pre_trained_language_models = {
# 语言模型 2.95GB
'zh_giga.no_cna_cmn.prune01244.klm': 'https://deepspeech.bj.bcebos.com/zh_lm/zh_giga.no_cna_cmn.prune01244.klm',
# 人民日报训练语言模型 20MB
'people_chars_lm.klm': 'https://www.borntowin.cn/mm/emb_models/people_chars_lm.klm'
}
def __init__(self, language_model_path=config.language_model_path,
word_freq_path=config.word_freq_path,
custom_word_freq_path=config.custom_word_freq_path,
custom_confusion_path=config.custom_confusion_path,
person_name_path=config.person_name_path,
place_name_path=config.place_name_path,
stopwords_path=config.stopwords_path):
self.name = 'detector'
self.language_model_path = language_model_path
self.word_freq_path = word_freq_path
self.custom_word_freq_path = custom_word_freq_path
self.custom_confusion_path = custom_confusion_path
self.person_name_path = person_name_path
self.place_name_path = place_name_path
self.stopwords_path = stopwords_path
self.is_char_error_detect = True
self.is_word_error_detect = True
self.initialized_detector = False
self.lm = None
self.word_freq = None
self.custom_confusion = None
self.custom_word_freq = None
self.person_names = None
self.place_names = None
self.stopwords = None
self.tokenizer = None
def _initialize_detector(self):
t1 = time.time()
try:
import kenlm
except ImportError:
raise ImportError('pycorrector dependencies are not fully installed, '
'they are required for statistical language model.'
'Please use "pip install kenlm" to install it.'
'if you are Win, Please install kenlm in cgwin.')
if not os.path.exists(self.language_model_path):
filename = self.pre_trained_language_models.get(self.language_model_path,
'zh_giga.no_cna_cmn.prune01244.klm')
url = self.pre_trained_language_models.get(filename)
get_file(
filename, url, extract=True,
cache_dir=config.USER_DIR,
cache_subdir=config.USER_DATA_DIR,
verbose=1
)
self.lm = kenlm.Model(self.language_model_path)
t2 = time.time()
logger.debug('Loaded language model: %s, spend: %.3f s.' % (self.language_model_path, t2 - t1))
# 词、频数dict
self.word_freq = self.load_word_freq_dict(self.word_freq_path)
# 自定义混淆集
self.custom_confusion = self._get_custom_confusion_dict(self.custom_confusion_path)
# 自定义切词词典
self.custom_word_freq = self.load_word_freq_dict(self.custom_word_freq_path)
self.person_names = self.load_word_freq_dict(self.person_name_path)
self.place_names = self.load_word_freq_dict(self.place_name_path)
self.stopwords = self.load_word_freq_dict(self.stopwords_path)
# 合并切词词典及自定义词典
self.custom_word_freq.update(self.person_names)
self.custom_word_freq.update(self.place_names)
self.custom_word_freq.update(self.stopwords)
self.word_freq.update(self.custom_word_freq)
self.tokenizer = Tokenizer(dict_path=self.word_freq_path, custom_word_freq_dict=self.custom_word_freq,
custom_confusion_dict=self.custom_confusion)
t3 = time.time()
logger.debug('Loaded dict file, spend: %.3f s.' % (t3 - t2))
self.initialized_detector = True
def check_detector_initialized(self):
if not self.initialized_detector:
self._initialize_detector()
@staticmethod
def load_word_freq_dict(path):
"""
加载切词词典
:param path:
:return:
"""
word_freq = {}
if not os.path.exists(path):
logger.warning('file not found.%s' % path)
return word_freq
with codecs.open(path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if line.startswith('#'):
continue
info = line.split()
if len(info) < 1:
continue
word = info[0]
# 取词频,默认1
freq = int(info[1]) if len(info) > 1 else 1
word_freq[word] = freq
return word_freq
def _get_custom_confusion_dict(self, path):
"""
取自定义困惑集
:param path:
:return: dict, {variant: origin}, eg: {"交通先行": "交通限行"}
"""
confusion = {}
if not os.path.exists(path):
logger.warning('file not found.%s' % path)
return confusion
with codecs.open(path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if line.startswith('#'):
continue
info = line.split()
if len(info) < 2:
continue
variant = info[0]
origin = info[1]
freq = int(info[2]) if len(info) > 2 else 1
self.word_freq[origin] = freq
confusion[variant] = origin
return confusion
def set_language_model_path(self, path):
self.check_detector_initialized()
import kenlm
self.lm = kenlm.Model(path)
logger.debug('Loaded language model: %s' % path)
def set_custom_confusion_dict(self, path):
self.check_detector_initialized()
custom_confusion = self._get_custom_confusion_dict(path)
self.custom_confusion.update(custom_confusion)
logger.debug('Loaded confusion path: %s, size: %d' % (path, len(custom_confusion)))
def set_custom_word(self, path):
self.check_detector_initialized()
word_freqs = self.load_word_freq_dict(path)
# 合并字典
self.custom_word_freq.update(word_freqs)
# 合并切词词典及自定义词典
self.word_freq.update(self.custom_word_freq)
self.tokenizer = Tokenizer(dict_path=self.word_freq_path, custom_word_freq_dict=self.custom_word_freq,
custom_confusion_dict=self.custom_confusion)
for k, v in word_freqs.items():
self.set_word_frequency(k, v)
logger.debug('Loaded custom word path: %s, size: %d' % (path, len(word_freqs)))
def enable_char_error(self, enable=True):
"""
is open char error detect
:param enable:
:return:
"""
self.is_char_error_detect = enable
def enable_word_error(self, enable=True):
"""
is open word error detect
:param enable:
:return:
"""
self.is_word_error_detect = enable
def ngram_score(self, chars):
"""
取n元文法得分
:param chars: list, 以词或字切分
:return:
"""
self.check_detector_initialized()
return self.lm.score(' '.join(chars), bos=False, eos=False)
def ppl_score(self, words):
"""
取语言模型困惑度得分,越小句子越通顺
:param words: list, 以词或字切分
:return:
"""
self.check_detector_initialized()
return self.lm.perplexity(' '.join(words))
def word_frequency(self, word):
"""
取词在样本中的词频
:param word:
:return:
"""
self.check_detector_initialized()
return self.word_freq.get(word, 0)
def set_word_frequency(self, word, num):
"""
更新在样本中的词频
"""
self.check_detector_initialized()
self.word_freq[word] = num
return self.word_freq
@staticmethod
def _check_contain_error(maybe_err, maybe_errors):
"""
检测错误集合(maybe_errors)是否已经包含该错误位置(maybe_err)
:param maybe_err: [error_word, begin_pos, end_pos, error_type]
:param maybe_errors:
:return:
"""
error_word_idx = 0
begin_idx = 1
end_idx = 2
for err in maybe_errors:
if maybe_err[error_word_idx] in err[error_word_idx] and maybe_err[begin_idx] >= err[begin_idx] and \
maybe_err[end_idx] <= err[end_idx]:
return True
return False
def _add_maybe_error_item(self, maybe_err, maybe_errors):
"""
新增错误
:param maybe_err:
:param maybe_errors:
:return:
"""
if maybe_err not in maybe_errors and not self._check_contain_error(maybe_err, maybe_errors):
maybe_errors.append(maybe_err)
@staticmethod
def _get_maybe_error_index(scores, ratio=0.6745, threshold=2):
"""
取疑似错字的位置,通过平均绝对离差(MAD)
:param scores: np.array
:param ratio: 正态分布表参数
:param threshold: 阈值越小,得到疑似错别字越多
:return: 全部疑似错误字的index: list
"""
result = []
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.abs(scores - median).flatten() # deviation from the median
# 平均绝对离差值
med_abs_deviation = np.median(margin_median)
if med_abs_deviation == 0:
return result
y_score = ratio * margin_median / med_abs_deviation
# 打平
scores = scores.flatten()
maybe_error_indices = np.where((y_score > threshold) & (scores < median))
# 取全部疑似错误字的index
result = list(maybe_error_indices[0])
return result
@staticmethod
def _get_maybe_error_index_by_stddev(scores, n=2):
"""
取疑似错字的位置,通过平均值上下n倍标准差之间属于正常点
:param scores: list, float
:param n: n倍
:return: 全部疑似错误字的index: list
"""
std = np.std(scores, ddof=1)
mean = np.mean(scores)
down_limit = mean - n * std
upper_limit = mean + n * std
maybe_error_indices = np.where((scores > upper_limit) | (scores < down_limit))
# 取全部疑似错误字的index
result = list(maybe_error_indices[0])
return result
@staticmethod
def is_filter_token(token):
result = False
# pass blank
if not token.strip():
result = True
# pass num
if token.isdigit():
result = True
# pass alpha
if is_alphabet_string(token.lower()):
result = True
# pass not chinese
if not is_chinese_string(token):
result = True
return result
@staticmethod
def split_2_short_text(text, include_symbol=False):
"""
长句切分为短句
:param text: str
:param include_symbol: bool
:return: (sentence, idx)
"""
result = []
blocks = re_han.split(text)
start_idx = 0
for blk in blocks:
if not blk:
continue
if include_symbol:
result.append((blk, start_idx))
else:
if re_han.match(blk):
result.append((blk, start_idx))
start_idx += len(blk)
return result
def detect(self, text):
maybe_errors = []
if not text.strip():
return maybe_errors
# 初始化
self.check_detector_initialized()
# 编码统一,utf-8 to unicode
text = convert_to_unicode(text)
# 文本归一化
text = uniform(text)
# 长句切分为短句
blocks = self.split_2_short_text(text)
for blk, idx in blocks:
maybe_errors += self.detect_short(blk, idx)
return maybe_errors
def detect_short(self, sentence, start_idx=0):
"""
检测句子中的疑似错误信息,包括[词、位置、错误类型]
:param sentence:
:param start_idx:
:return: list[list], [error_word, begin_pos, end_pos, error_type]
"""
maybe_errors = []
# 初始化
self.check_detector_initialized()
# 自定义混淆集加入疑似错误词典
for confuse in self.custom_confusion:
idx = sentence.find(confuse)
if idx > -1:
maybe_err = [confuse, idx + start_idx, idx + len(confuse) + start_idx, ErrorType.confusion]
self._add_maybe_error_item(maybe_err, maybe_errors)
if self.is_word_error_detect:
# 切词
tokens = self.tokenizer.tokenize(sentence)
# 未登录词加入疑似错误词典
for token, begin_idx, end_idx in tokens:
# pass filter word
if self.is_filter_token(token):
continue
# pass in dict
if token in self.word_freq:
continue
maybe_err = [token, begin_idx + start_idx, end_idx + start_idx, ErrorType.word]
self._add_maybe_error_item(maybe_err, maybe_errors)
if self.is_char_error_detect:
# 语言模型检测疑似错误字
try:
ngram_avg_scores = []
for n in [2, 3]:
scores = []
for i in range(len(sentence) - n + 1):
word = sentence[i:i + n]
score = self.ngram_score(list(word))
scores.append(score)
if not scores:
continue
# 移动窗口补全得分
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)
if ngram_avg_scores:
# 取拼接后的n-gram平均得分
sent_scores = list(np.average(np.array(ngram_avg_scores), axis=0))
# 取疑似错字信息
for i in self._get_maybe_error_index(sent_scores):
token = sentence[i]
# pass filter word
if self.is_filter_token(token):
continue
# pass in stop word dict
if token in self.stopwords:
continue
# token, begin_idx, end_idx, error_type
maybe_err = [token, i + start_idx, i + start_idx + 1,
ErrorType.char]
self._add_maybe_error_item(maybe_err, maybe_errors)
except IndexError as ie:
logger.warn("index error, sentence:" + sentence + str(ie))
except Exception as e:
logger.warn("detect error, sentence:" + sentence + str(e))
return sorted(maybe_errors, key=lambda k: k[1], reverse=False)