-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcasp_helper.py
373 lines (311 loc) · 12.2 KB
/
mcasp_helper.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
import re
import numpy as np
from tqdm import tqdm
from collections import defaultdict
from math import log
import json
class Find_Words:
def __init__(self, min_count=10, max_count=10000000, min_pmi=0):
self.min_count = min_count
self.min_pmi = min_pmi
self.chars, self.pairs = defaultdict(int), defaultdict(int)
self.total = 0.
self.max_count = max_count
def text_filter(self, texts):
for a in tqdm(texts):
for t in re.split(u'[^\u4e00-\u9fa50-9a-zA-Z]+', ''.join(a)):
if t:
yield t
def count(self, texts):
mi_list = []
for text in self.text_filter(texts):
self.chars[text[0]] += 1
for i in range(len(text)-1):
self.chars[text[i+1]] += 1
self.pairs[text[i:i+2]] += 1
self.total += 1
self.chars = {i:j for i,j in self.chars.items() if 100 * self.max_count > j > self.min_count}
self.pairs = {i:j for i,j in self.pairs.items() if self.max_count > j > self.min_count}
self.strong_segments = set()
for i,j in self.pairs.items():
if i[0] in self.chars and i[1] in self.chars:
mi = log(self.total*j/(self.chars[i[0]]*self.chars[i[1]]))
mi_list.append(mi)
if mi >= self.min_pmi:
self.strong_segments.add(i)
print('min mi: %.4f' % min(mi_list))
print('max mi: %.4f' % max(mi_list))
print('remaining: %d / %d (%.4f)' % (len(self.strong_segments), len(mi_list), len(self.strong_segments)/len(mi_list)))
def find_words(self, texts, n):
self.words = defaultdict(int)
for text in self.text_filter(texts):
s = text[0]
for i in range(len(text)-1):
if text[i:i+2] in self.strong_segments:
s += text[i+1]
else:
self.words[s] += 1
s = text[i+1]
self.words = {i:j for i,j in self.words.items() if j > self.min_count and n+1 > len(i) > 0}
def read_tsv(file_path):
sentence_list = []
label_list = []
with open(file_path, 'r', encoding='utf8') as f:
lines = f.readlines()
sentence = []
labels = []
for line in lines:
line = line.strip()
if line == '':
if len(sentence) > 0:
sentence_list.append(sentence)
label_list.append(labels)
sentence = []
labels = []
continue
items = re.split('\\s+', line)
character = items[0]
label = items[-1]
sentence.append(character)
labels.append(label)
return sentence_list, label_list
def get_word2id(train_path):
word2id = {'<PAD>': 0, '<UNK>': 1}
word = ''
index = 2
with open(train_path, 'r', encoding='utf8') as f:
for line in tqdm(f.readlines()):
line = line.strip()
if len(line) == 0:
continue
splits = line.split('\t')
character = splits[0]
label = splits[-1][0]
word += character
if label in ['S', 'E']:
if word not in word2id:
word2id[word] = index
index += 1
word = ''
return word2id
def dlg(data_path_list, ngram_length, renew_freq):
all_sentences = []
for file_name in data_path_list:
sentences, _ = read_tsv(file_name)
all_sentences.extend(sentences)
n_gram_dict = extract_ngram(all_sentences, 0, ngram_length)
corpus_size = 0
for gram, count in n_gram_dict.items():
if len(gram) == 1:
corpus_size += count
min_dlg = np.inf
max_dlg = -np.inf
min_dlg_2 = np.inf
max_dlg_2 = -np.inf
n_gram_dlg_dict = {}
num_small_dlg = 0
skip_num = 0
for gram, c_gram in tqdm(n_gram_dict.items()):
if len(gram) == 1 or c_gram < 2:
skip_num += 1
continue
new_corpus_size = corpus_size - c_gram * (len(gram) - 1) + len(gram) + 1
dlg = c_gram * np.log10(c_gram) + corpus_size * np.log10(corpus_size) - new_corpus_size * np.log10(new_corpus_size)
if dlg > max_dlg_2:
max_dlg_2 = dlg
if dlg < min_dlg_2:
min_dlg_2 = dlg
# if dlg > 200000:
# print('%s %d' % (gram, c_gram))
char_in_gram = list(set(gram))
for character in char_in_gram:
c_character = n_gram_dict[character]
new_c_character = c_character - (c_gram - 1) * gram.count(character)
# if not new_c_character > 0:
# print('gram: %s' % gram)
# print('# of new c character: %d' % new_c_character)
# raise ValueError()
new_character_item = new_c_character * np.log10(new_c_character) if new_c_character > 0 else 0
dlg += new_character_item - c_character * np.log10(c_character)
adlg = dlg / c_gram
if dlg > 0:
n_gram_dlg_dict[gram] = dlg / c_gram
else:
num_small_dlg += 1
if adlg > max_dlg:
max_dlg = adlg
if adlg < min_dlg:
min_dlg = adlg
new_dlg_dict = {}
new_all_sentences = []
for sen in all_sentences:
str_sen = ''.join(sen)
new_sen = re.split(u'[^\u4e00-\u9fa50-9a-zA-Z]+', str_sen)
for s in new_sen:
if len(s) > 0:
new_all_sentences.append(s)
for sentence in tqdm(new_all_sentences):
n_gram_list = vitbi(sentence, n_gram_dlg_dict)
for gram in n_gram_list:
if gram not in new_dlg_dict:
new_dlg_dict[gram] = 1
else:
new_dlg_dict[gram] += 1
new_dlg_dict_2 = {gram: c for gram, c in new_dlg_dict.items() if c >= renew_freq and len(gram) < ngram_length+1}
new_dlg_dict_2 = renew_ngram_by_freq(all_sentences, new_dlg_dict_2, renew_freq, ngram_length)
return new_dlg_dict_2
def vitbi(sentence, ngram_dict):
score = [0 for i in range(len(sentence))]
n_gram = [[] for i in range(len(sentence))]
word = sentence[0]
n_gram[0].append(word)
for i in range(1, len(score)):
tmp_score_list = [score[i-1], -1, -1, -1, -1]
for n in range(2, 6):
if i - n < -1:
break
word = ''.join(sentence[i - n + 1: i + 1])
if word in ngram_dict:
tmp_score_list[n-1] = score[i-n] + ngram_dict[word] if i-n >= 0 else ngram_dict[word]
max_score = max(tmp_score_list)
max_score_index = tmp_score_list.index(max(tmp_score_list))
word = ''.join(sentence[i-max_score_index: i+1])
score[i] = max_score
if i-(max_score_index+1) >= 0:
n_gram[i].extend(n_gram[i - (max_score_index + 1)])
n_gram[i].append(word)
return n_gram[-1]
def pmi(data_path_list, ngram_length, renew_freq):
all_sentences = []
for file_name in data_path_list:
sentences, _ = read_tsv(file_name)
all_sentences.extend(sentences)
fw = Find_Words(0, 1000000000000, 0)
fw.count(all_sentences)
fw.find_words(all_sentences, ngram_length)
words = fw.words
words = renew_ngram_by_freq(all_sentences, words, renew_freq, ngram_length)
return words
def av(data_path_list, av_threshold, ngram_length, renew_freq):
all_sentences = []
for file_name in data_path_list:
sentences, _ = read_tsv(file_name)
all_sentences.extend(sentences)
n_gram_dict = {}
new_all_sentences = []
ngram2av = {}
for sen in all_sentences:
str_sen = ''.join(sen)
new_sen = re.split(u'[^\u4e00-\u9fa50-9a-zA-Z]+', str_sen)
for s in new_sen:
if len(s) > 0:
new_all_sentences.append(s)
for sentence in tqdm(new_all_sentences):
for i in range(len(sentence)):
for n in range(1, ngram_length+1):
if i + n > len(sentence):
break
left_index = i - 1
right_index = i + n
n_gram = ''.join(sentence[i: i + n])
if n_gram not in n_gram_dict:
n_gram_dict[n_gram] = 1
ngram2av[n_gram] = {'l': {}, 'r': {}}
else:
n_gram_dict[n_gram] += 1
if left_index >= 0:
ngram2av[n_gram]['l'][sentence[left_index]] = 1
if right_index < len(sentence):
ngram2av[n_gram]['r'][sentence[right_index]] = 1
remaining_ngram = {}
for ngram, av_dict in ngram2av.items():
avl = len(av_dict['l'])
avr = len(av_dict['r'])
av = min(avl, avr)
if av > av_threshold and n_gram_dict[ngram] > 0:
remaining_ngram[ngram] = n_gram_dict[ngram]
remaining_ngram = renew_ngram_by_freq(all_sentences, remaining_ngram, renew_freq, ngram_length)
return remaining_ngram
def extract_ngram(all_sentences, min_feq=0, ngram_len=10):
n_gram_dict = {}
new_all_sentences = []
for sen in all_sentences:
str_sen = ''.join(sen)
new_sen = re.split(u'[^\u4e00-\u9fa50-9a-zA-Z]+', str_sen)
for s in new_sen:
if len(s) > 0:
new_all_sentences.append(s)
for sentence in new_all_sentences:
for i in range(len(sentence)):
for n in range(1, ngram_len+1):
if i + n > len(sentence):
break
n_gram = ''.join(sentence[i: i + n])
if n_gram not in n_gram_dict:
n_gram_dict[n_gram] = 1
else:
n_gram_dict[n_gram] += 1
new_ngram_dict = {gram: c for gram, c in n_gram_dict.items() if c > min_feq}
return new_ngram_dict
def renew_ngram_by_freq(all_sentences, ngram2count, min_feq, ngram_len=10):
new_ngram2count = {}
new_all_sentences = []
for sen in all_sentences:
str_sen = ''.join(sen)
new_sen = re.split(u'[^\u4e00-\u9fa50-9a-zA-Z]+', str_sen)
for s in new_sen:
if len(s) > 0:
new_all_sentences.append(s)
for sentence in new_all_sentences:
for i in range(len(sentence)):
for n in range(1, ngram_len+1):
if i + n > len(sentence):
break
n_gram = ''.join(sentence[i: i + n])
if n_gram not in ngram2count:
continue
if n_gram not in new_ngram2count:
new_ngram2count[n_gram] = 1
else:
new_ngram2count[n_gram] += 1
new_ngram_dict = {gram: c for gram, c in new_ngram2count.items() if c >= min_feq}
return new_ngram_dict
def get_gram2id(data_path_list, ngram_type, ngram_len, av_threshold, ngram_threshold):
if ngram_type == 'pmi':
word2count = pmi(data_path_list=data_path_list, ngram_length=ngram_len, renew_freq=ngram_threshold)
elif ngram_type == 'av':
word2count = av(data_path_list=data_path_list, av_threshold=av_threshold,
ngram_length=ngram_len, renew_freq=ngram_threshold)
elif ngram_type == 'dlg':
word2count = dlg(data_path_list=data_path_list, ngram_length=ngram_len, renew_freq=ngram_threshold)
else:
raise ValueError()
gram2id = {'<PAD>': 0}
index = 1
for word, count in word2count.items():
# if count > threshold and count < upper_threshold:
gram2id[word] = index
index += 1
return gram2id, word2count
def load_json(file_path):
with open(file_path, 'r', encoding='utf8') as f:
line = f.readline()
return json.loads(line)
def save_json(file_path, data):
with open(file_path, 'w', encoding='utf8') as f:
json.dump(data, f)
f.write('\n')
def get_labels(train_path):
label_list = ['<PAD>', '<UNK>']
with open(train_path, 'r', encoding='utf8') as f:
lines = f.readlines()
for line in lines:
line = line.strip()
if len(line) == 0:
continue
splits = line.split()
joint_label = splits[1]
if joint_label not in label_list:
label_list.append(joint_label)
label_list.extend(['[CLS]', '[SEP]'])
return label_list