generated from GT-ZhangAcer/AI-Studio-Template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathroastModule.py
245 lines (204 loc) · 8.73 KB
/
roastModule.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
import xlrd
# from paddlenlp.embeddings import TokenEmbedding
import paddlehub as hub
import numpy as np
concernKey1 = ['n', 'nr', 'nz', 'PER', 'ns', 'LOC', 's', 'nt', 'ORG', 'nw']
concernKey2 = ['v']
lac = hub.Module(name="lac")
# test_text = "还记得你之前跟我说,我能及格猪都能上树。可教练我拿到驾照了,猪不仅能上树还能上高速,我再给你来个倒车入库,压线了难受不?以前我不敢顶撞你,现在顶撞你可抗不住。今天是个好日子"
def filt(tagList, wordList):
nn = []
vv = []
for tag, word in zip(tagList, wordList):
if tag in concernKey1:
nn.append(word)
elif tag in concernKey2:
vv.append(word)
return nn, vv
def keyOutput(test_text):
nWord = []
vWord = []
result = lac.cut(text=[test_text], use_gpu=False, batch_size=1, return_tag=True)[0]
# print('results',results)
# print('result',result)
tagList = result['tag']
wordList = result['word']
nn, vv = filt(tagList, wordList)
# print(nn,'===',vv)
nWord = nn
vWord = vv
return nWord, vWord
# nnVector,vvVector=keyOutput(test_text)
# print('nnVector,',nnVector)
##
def read_excel_data(fpath):
ExcelFile = xlrd.open_workbook(fpath)
num = 1
personList = []
sentenceList = []
classList = []
nWordList = []
vWordList = []
try:
sheet = ExcelFile.sheet_by_name('Sheet' + str(num))
num += 1
except:
pass
# #获取整行或者整列的值
# #rows=sheet.row_values(2)#第三行内容
personList.extend(sheet.col_values(0)[1:])
sentenceList.extend(sheet.col_values(1)[1:])
classList.extend(sheet.col_values(2)[1:])
for sent in sentenceList:
nWord, vWord = keyOutput(sent)
nWordList.append(nWord)
vWordList.append(vWord)
return personList, sentenceList, classList, nWordList, vWordList
class WordVector():
def __init__(self, sim_thershold=0.8, fpath='resource/roast1.xls'):
# self.wordemb = hub.Module(name='w2v_weibo_target_word-word_dim300')
self.wordemb = None
if self.wordemb is not None:
self.personList, self.sentenceList, _, self.nWordList, self.vWordList = read_excel_data(fpath)
print('self.nWordList', len(self.nWordList))
self.sim_thershold = sim_thershold
self.UNK = self.wordemb.get_idx_from_word('UNK')
self.nWordIndexList = self.wordList2indexList(self.nWordList)
self.vWordIndexList = self.wordList2indexList(self.vWordList)
print('nWordIndexList', len(self.nWordIndexList), self.nWordIndexList[:5], self.nWordList[:5])
print('self.vWordIndexList', len(self.vWordIndexList), self.vWordIndexList[:5], self.vWordList[:5])
def index_cosine_sim(self, xIndex, yIndex):
x = self.wordemb.search(xIndex)
y = self.wordemb.search(yIndex)
return self.cosine_sim(x, y)
def cosine_sim(self, x, y):
chushu = (np.sqrt(np.dot(x, x.T)) * np.sqrt(np.dot(y, y.T)))
if chushu == 0:
sim = 0
else:
sim = np.dot(x, y.T) / chushu
sim = sim[0, 0]
# print('sim',sim)
# sim=self.wordemb.cosine_sim(x, y)
return sim
def get_vector(self, query):
#
if self.wordemb.get_idx_from_word(query) == self.UNK:
# not in vector dict use char vector
queryVector = self.wordemb.search(query[0])
if len(query) > 1:
for index in range(1, len(query)):
queryVector += self.wordemb.search(query[0])
else:
queryVector = self.wordemb.search(query)
return queryVector
def wordList2indexList(self, wordList):
indexList = []
for words in wordList:
indexs = []
for word in words:
indexs.append(self.word2index(word))
indexList.append(indexs)
##双层list
return indexList
def word2index(self, word):
wordIndex = []
if self.wordemb.get_idx_from_word(word) == self.UNK:
# not in vector dict use char vector
for index in range(0, len(word)):
wordIndex.append(self.wordemb.get_idx_from_word(word[index]))
else:
wordIndex.append(self.wordemb.get_idx_from_word(word))
return wordIndex
def index2vector(self, aIndexs):
# 所有lac后单词组合的wordvec
vectorList = []
for ai in aIndexs:
vectorList.append(self.wordemb.search(ai))
return vectorList
def indexList2vectorList(self, indexsList):
vectorsList = []
for indexs in indexsList:
vectorsList.append(self.index2vector(indexs))
return vectorsList
def cosine_sim_lac(self, vectorList1, vectorList2):
combineVector1 = np.sum(np.array(vectorList1), 0)
combineVector2 = np.sum(np.array(vectorList2), 0)
return self.cosine_sim(combineVector1, combineVector2)
def vectorList2sentenceVector(self, sVectorList):
wordVectorList = []
for vl in sVectorList: # vl 每个word的 vector list
# print(type(vl))
# print(vl)
wordVectorList.append(np.sum(np.array(vl), 0))
return np.sum(np.array(wordVectorList), 0)
def cosine_sim_sentence(self, sVectorList, qVectorList):
storeVector = self.vectorList2sentenceVector(sVectorList)
queryVector = self.vectorList2sentenceVector(qVectorList)
return self.cosine_sim(storeVector, queryVector)
def cal_simList(self, queryIndexList, storeIndexList):
simList = []
sentenceSimList = []
charSimList = []
# query中每个key word的indexlist,n个key word或char组成wordIndexList
for sIndexList in storeIndexList: # sIndexList-》每一句吐槽= [[],[]]
simValue = 0
sVectorList = self.indexList2vectorList(sIndexList)
qVectorList = self.indexList2vectorList(queryIndexList)
# print('sVectorList', len(sVectorList[0]), 'qVectorList', len(qVectorList[0]))
sentenceSimValue = self.cosine_sim_sentence(sVectorList, qVectorList)
# print('sentenceSimValue', sentenceSimValue)
if sentenceSimValue < self.sim_thershold:
for qvs in qVectorList: # 每一个word的vector list
for svs in sVectorList: # 每一句吐槽中的每个字的vector list
# 先计算
temp = self.cosine_sim_lac(qvs, svs)
if temp > simValue:
simValue = temp
#
sentenceSimList.append(sentenceSimValue)
charSimList.append(simValue)
# print('simList',len(charSimList))
return sentenceSimList, charSimList
def query2index(self, query):
queryIndexList = []
for word in query:
queryIndexList.append(self.word2index(word))
return queryIndexList
def simSentenceIndex(self, qnWord, qvWord, vWordMatch=False):
if self.wordemb is None: return ''
index = -1
qnIndexList = []
qnIndexList = self.query2index(qnWord)
# print('qnIndex',qnIndexList)
nSenSimList, nCharSimList = self.cal_simList(qnIndexList, self.nWordIndexList)
if max(nSenSimList) >= self.sim_thershold: # caculate n word combine sentence
index = nSenSimList.index(max(nSenSimList))
elif max(nCharSimList) >= self.sim_thershold: # caculate n word
index = nCharSimList.index(max(nCharSimList))
else:
if vWordMatch:
##caculate the v word
vnIndexList = self.query2index(qvWord)
vSenSimList, vCharSimList = self.cal_simList(vnIndexList, self.vWordIndexList)
if max(vSenSimList) >= self.sim_thershold:
index = vSenSimList.index(max(vSenSimList))
elif max(vCharSimList) >= self.sim_thershold:
index = vCharSimList.index(max(vCharSimList))
return index
def simSentence(self, query):
try:
qnWord, qvWord = keyOutput(query)
# try:
index = self.simSentenceIndex(qnWord, qvWord)
if index >= 0:
return self.sentenceList[index]
except:
pass
return ''
# except Exception as e:
# print(e)
# return ''
wv = WordVector()
ans = wv.simSentence('娘娘腔,不会唱歌不会跳舞')
print(ans)