-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPMI.py
355 lines (316 loc) · 12.3 KB
/
PMI.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
# coding=utf-8
import jieba
import jieba.analyse
import jieba.posseg as pseg
import os
import re
import codecs
class PMI_train:
def __init__(self, tokenizer,
stop_word_file_path, source_path):
self.tokenizer=tokenizer
self.stopwords = self.stopwordslist(stop_word_file_path)
self.source_path = source_path
self.document = self.make_document(self.source_path)
self.miniprobability = float(1.0) / self.document.__len__()
self.minitogether = float(0.0) / self.document.__len__()
self.set_word = self.getset_word()
self.dict_frq_word = self.get_dict_frq_word()
def __getstate__(self):
return self.__dict__
def make_document(self, path):
documents = []
f = open(path, 'r', encoding="utf-8")
data = f.readlines()
if data is not None:
for i, sentences in enumerate(data):
extractwords = []
wordlist = self.CutWithPartOfSpeech(sentences)
# print("wordlist",wordlist) #
words = self.ExtractWord(wordlist)
# print("wordswords", words)
for word in words:
extractwords.append(word)
documents.append(set(extractwords))
if i % 10000 == 0:
print("运行至 %d 行" % i)
print("数据加载完毕")
return documents
def calcularprobability(self, document, wordlist):
"""
:param document:
:param wordlist:
:function : 计算单词的document frequency
:return: document frequency
"""
total = document.__len__()
number = 0
for doc in document:
if set(wordlist).issubset(doc):
number += 1
percent = float(number) / total
return percent
def togetherprobablity(self, document, wordlist1, wordlist2):
"""
:param document:
:param wordlist1:
:param wordlist2:
:function: 计算单词的共现概率
:return:共现概率
"""
joinwordlist = wordlist1 + wordlist2
percent = self.calcularprobability(document, joinwordlist)
return percent
def getset_word(self):
"""
:function: 得到document中的词语词典
:return: 词语词典
"""
list_word = []
for doc in self.document:
list_word = list_word + list(doc)
set_word = []
for w in list_word:
if set_word.count(w) == 0:
set_word.append(w)
# print("set_word",set_word)
return set_word
def get_dict_frq_word(self):
"""
:function: 对词典进行剪枝,剪去出现频率较少的单词
:return: 剪枝后的词典
"""
dict_frq_word = {}
for i in range(0, self.set_word.__len__(), 1):
list_word = []
# print("self.set_word[i]",self.set_word[i])
list_word.append(self.set_word[i])
probability = self.calcularprobability(self.document, list_word)
# dict_frq_word[self.set_word[i]] = probability
if probability > self.miniprobability:
# print("self.miniprobability",self.miniprobability)
dict_frq_word[self.set_word[i]] = probability
# print("dict_frq_word",dict_frq_word)
return dict_frq_word
def calculate_nmi(self, joinpercent, wordpercent1, wordpercent2):
"""
function: 计算词语共现的nmi值
:param joinpercent:
:param wordpercent1:
:param wordpercent2:
:return:nmi
"""
return (joinpercent) / (wordpercent1 * wordpercent2)
# def get_pmi(self):
# """
# function:返回符合阈值的pmi列表
# :return:pmi列表
# """
# dict_pmi = {}
# # print ("dict_frq_word",dict_frq_word)
# for word1 in self.dict_frq_word:
# wordpercent1 = self.dict_frq_word[word1]
# for word2 in self.dict_frq_word:
# if word1 == word2:
# continue
# wordpercent2 = self.dict_frq_word[word2]
# list_together = []
# list_together.append(word1)
# list_together.append(word2)
# together_probability = self.calcularprobability(self.document, list_together)
# if together_probability > self.minitogether:
# string = word1 + ',' + word2
# dict_pmi[string] = self.calculate_nmi(together_probability, wordpercent1, wordpercent2)
# return dict_pmi
def calculate_lis(self, word1, word2):
if word1 != word2:
wordpercent1 = 1
wordpercent2 = 1
if word1 in self.dict_frq_word:
wordpercent1 = self.dict_frq_word[word1]
if word2 in self.dict_frq_word:
wordpercent2 = self.dict_frq_word[word2]
list_together = []
list_together.append(word1)
list_together.append(word2)
together_probability = self.calcularprobability(self.document, list_together)
return self.calculate_nmi(together_probability, wordpercent1, wordpercent2)
else:
return 0
def calculate_word_lis(self, word1, list):
calculate_score = []
for word2 in list:
if word1 != word2:
wordpercent1 = 1
wordpercent2 = 1
if word1 in self.dict_frq_word:
wordpercent1 = self.dict_frq_word[word1]
if word2 in self.dict_frq_word:
wordpercent2 = self.dict_frq_word[word2]
list_together = []
list_together.append(word1)
list_together.append(word2)
together_probability = self.calcularprobability(self.document, list_together)
calculate_score.append(self.calculate_nmi(together_probability, wordpercent1, wordpercent2))
calculate_score = sum(calculate_score) / len(calculate_score) # 取平均值
return calculate_score
# 下面全是数据获取
def removeEmoji(self, sentence):
return re.sub('\[.*?\]', '', sentence)
def CutWithPartOfSpeech(self, sentence):
sentence = self.removeEmoji(sentence)
words = self.tokenizer.tokenize_lis(sentence.strip())
outlis = []
for word in words:
if word not in outlis: # word not in self.stopwords:
outlis.append(word)
return list(set(outlis))
def segment(self, list):
querylist = []
outstr = ''
for word in list:
if word == ' ':
continue
# 加停用词
if word not in self.stopwords:
outstr += word
outstr += "$"
querylist.append(outstr.split('$'))
return querylist
def stopwordslist(self, filepath):
stopwords = [line.strip() for line in open(filepath, 'r', encoding='utf-8').readlines()]
return stopwords
def ExtractWord(self, wordlist):
sentence = ','.join(wordlist)
# words = jieba.analyse.extract_tags(sentence,4)
words = self.tokenizer.model.analyse.extract_tags(sentence) # 默认前20个词
wordlist = []
for w in words:
wordlist.append(w)
return wordlist
def RemoveStopWord(self, wordlist):
stopWords = self.GetStopWords()
keywords = []
for word in wordlist:
if word not in stopWords:
keywords.append(word)
return keywords
class PMI_test:
def __init__(self,tokenizer,stopwords, document, miniprobability, minitogether, set_word,dict_frq_word):
self.tokenizer=tokenizer
self.stopwords =stopwords
self.document =document
self.miniprobability =miniprobability
self.minitogether =minitogether
self.set_word =set_word
self.dict_frq_word = dict_frq_word
def calcularprobability(self, document, wordlists):
"""
:param document:
:param wordlist:
:function : 计算单词的document frequency
:return: document frequency
"""
total = document.__len__()
number = 0
for doc in document:
if set(wordlists).issubset(doc):
number += 1
percent = float(number) / total
return percent
def togetherprobablity(self, document, wordlist1, wordlist2):
"""
:param document:
:param wordlist1:
:param wordlist2:
:function: 计算单词的共现概率
:return:共现概率
"""
joinwordlist = wordlist1 + wordlist2
percent = self.calcularprobability(document, joinwordlist)
return percent
def calculate_nmi(self, joinpercent, wordpercent1, wordpercent2):
"""
function: 计算词语共现的nmi值
:param joinpercent:
:param wordpercent1:
:param wordpercent2:
:return:nmi
"""
return (joinpercent) / (wordpercent1 * wordpercent2)
def calculate_lis(self, word1, word2):
if word1 != word2:
wordpercent1 = 1
wordpercent2 = 1
if word1 in self.dict_frq_word:
wordpercent1 = self.dict_frq_word[word1]
# print(wordpercent1)
if word2 in self.dict_frq_word:
wordpercent2 = self.dict_frq_word[word2]
# print(wordpercent2)
list_together = []
list_together.append(word1)
list_together.append(word2)
together_probability = self.calcularprobability(self.document, list_together)
return self.calculate_nmi(together_probability, wordpercent1, wordpercent2)
else:
return 0
def calculate_word_lis(self, word,wordpercent, Key):
if len(Key)==0 or wordpercent == 0.0 or len(word.strip()) == 0 :
return 0
calculate_score = []
for word2 in Key.keys():
wordpercent2 = Key[word2]
list_together = []
list_together.append(word)
list_together.append(word2)
together_probability = self.calcularprobability(self.document, list_together) # 同时出现的概率
# print("word", word, together_probability)
if together_probability > (self.miniprobability):
# print(together_probability, wordpercent, wordpercent2)
calculate_score.append(self.calculate_nmi(together_probability, wordpercent, wordpercent2))
# print("self.calculate_nmi",list_together,together_probability,wordpercent, wordpercent2,self.calculate_nmi(together_probability, wordpercent, wordpercent2))
# else:
# print("no",list_together,together_probability)
calculate_score = sum(calculate_score) / (len(calculate_score) if len(calculate_score) != 0 else 1) # 取平均值
return calculate_score
# 下面全是数据获取
def removeEmoji(self, sentence):
return re.sub('\[.*?\]', '', sentence)
def CutWithPartOfSpeech(self, sentence):
sentence = self.removeEmoji(sentence)
words = self.tokenizer.tokenize_lis(sentence.strip())
outstr = ""
for word in words:
if word not in self.stopwords:
outstr += word
outstr += "$"
return list(set(outstr.split('$')))
def segment_with_stopwords(self, list):
outwords = []
for word in list:
if word == ' ':
continue
# 加停用词
if word not in self.stopwords and word not in outwords:
outwords.append(word)
return outwords
def segment(self, list):
outwords = []
for word in list:
if word == ' ' or word not in self.dict_frq_word:
continue
# 加停用词
if word not in outwords:
outwords.append(word)
return outwords
def stopwordslist(self, filepath):
stopwords = [line.strip() for line in open(filepath, 'r', encoding='utf-8').readlines()]
return stopwords
def RemoveStopWord(self, wordlist):
stopWords = self.GetStopWords()
keywords = []
for word in wordlist:
if word not in stopWords:
keywords.append(word)
return keywords