-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathopinionExtraction.py
More file actions
224 lines (170 loc) · 6.61 KB
/
opinionExtraction.py
File metadata and controls
224 lines (170 loc) · 6.61 KB
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
import re
import json
import collections
from dependency import DependencyExtraction
from utils import readFile, similarity
class Opinion(object):
def __init__(self, sentence, opinion, keyword):
self.opinion = opinion
self.sentence = sentence
self.keyword = keyword
self.cluster = None
def updateCluster(self, cluster):
self.cluster = cluster
class OpinionCluster(object):
def __init__(self):
self._opinions = []
def addOpinion(self, opinion):
self._opinions.append(opinion)
opinion.updateCluster(self)
def getOpinions(self):
return self._opinions
def getSummary(self, freqStrLen):
opinionStrs = []
for op in self._opinions:
opinion = op.opinion
opinionStrs.append(opinion)
# 统计字频率
word_counter = collections.Counter(list("".join(opinionStrs))).most_common()
freqStr = ""
for item in word_counter:
if item[1] >= freqStrLen:
freqStr += item[0]
maxSim = -1
maxOpinion = ""
for opinion in opinionStrs:
sim = similarity(freqStr, opinion)
if sim > maxSim:
maxSim = sim
maxOpinion = opinion
return maxOpinion
class OpinionExtraction(object):
def __init__(self, sentences = [], sentenceFile = "", keywordFile = ""):
self.json_config = self.loadConfig()
if sentenceFile:
self.sentences = self.filterSentence(readFile(sentenceFile)[:self.json_config["dataLen"]])
else:
self.sentences = self.filterSentence(sentences[:self.json_config["dataLen"]])
self.keyword = readFile(keywordFile)
def loadConfig(self):
f = open("./config.json", "r", encoding='utf-8')
config = json.load(f)
return config
def filterSentence(self, sentences):
# 正则匹配字母数字连续出现超过7个,包括账号,电话,邮箱,银行卡号
newSentences = []
email_phone_re = re.compile('[A-Za-z0-9\d]{7}')
for sent in sentences:
# 长度太短
if len(sent) < 4:
continue
addFlag = True
sentLower = sent.lower()
# 关键字过滤
for exceptWord in self.json_config["exceptWordList"]:
if exceptWord in sentLower:
addFlag = False
break
if not addFlag:
continue
# 不过滤的关键字
for includeWord in self.json_config["includeWordList"]:
if includeWord in sentLower:
newSentences.append(sent)
addFlag = False
break
if not addFlag:
continue
# 重复过滤
if sent in newSentences:
continue
# 过滤正则
match = email_phone_re.findall(sentLower)
if match:
continue
if addFlag:
newSentences.append(sent)
return newSentences
def extractor(self):
de = DependencyExtraction()
opinionList = OpinionCluster()
for sent in self.sentences:
keyword = ""
if not self.keyword:
keyword = ""
else:
checkSent = []
for word in self.keyword:
if sent not in checkSent and word in sent:
keyword = word
checkSent.append(sent)
break
opinion = "".join(de.parseSentWithKey(sent, keyword))
if self.filterOpinion(opinion):
opinionList.addOpinion(Opinion(sent, opinion, keyword))
#这步跳过前面的依存分析,加快调试
# opinionList = self.getFirstCluster()
'''
这里设置两个阈值,先用小阈值把一个大数据切成小块,由于是小阈值,所以本身是一类的基本也能分到一类里面。
由于分成了许多小块,再对每个小块做聚类,聚类速度大大提升,[0.2, 0.6]比[0.6]速度高30倍左右。
但是[0.2, 0.6]和[0.6]最后的结果不是一样的,会把一些相同的观点拆开。
'''
thresholds = self.json_config["thresholds"]
clusters = [opinionList]
for threshold in thresholds:
newClusters = []
for cluster in clusters:
newClusters += self.clusterOpinion(cluster, threshold)
clusters = newClusters
resMaxLen = {}
for oc in clusters:
if len(oc.getOpinions()) >= self.json_config["minClusterLen"]:
summaryStr = oc.getSummary(self.json_config["freqStrLen"])
resMaxLen[summaryStr] = oc.getOpinions()
return self.sortRes(resMaxLen)
def sortRes(self, res):
return sorted(res.items(), key=lambda item:len(item[1]), reverse=True)
def getFirstCluster(self):
opinions = []
with open("./data/opinion.txt", "r", encoding="utf-8") as f:
for line in f:
lineSplit = line.strip().split(",")
opinions.append(lineSplit)
opinions = opinions[:self.json_config["dataLen"]]
firstCluster = OpinionCluster()
for op in opinions:
op = op + [""]
firstCluster.addOpinion(Opinion(*op))
return firstCluster
def filterOpinion(self, opinion):
check = True
if len(opinion) <= self.json_config["minOpinionLen"]:
check = False
elif opinion.isdigit():
check = False
return check
# 复杂度是O(n2),速度比较慢。
def clusterOpinion(self, cluster, threshold):
opinions = cluster.getOpinions()
num = len(opinions)
clusters = []
checked1 = []
for i in range(num):
oc = OpinionCluster()
opinion1 = opinions[i]
if opinion1 in checked1:
continue
if opinion1 not in oc.getOpinions():
oc.addOpinion(opinion1)
checked1.append(opinion1)
for j in range(i + 1, num):
opinion2 = opinions[j]
if opinion2 in checked1:
continue
sim = similarity(opinion1.opinion, opinion2.opinion)
if sim > threshold:
if opinion2 not in oc.getOpinions():
oc.addOpinion(opinion2)
checked1.append(opinion2)
clusters.append(oc)
return clusters