forked from huangxiangzhou/nlpcc2016team1repl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocessing.py
323 lines (236 loc) · 9.94 KB
/
preprocessing.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
import sys
import codecs
import re
import json
def removeHFE(kbDict,hfe):
kbDictCopy = kbDict.copy()
for key in hfe:
del kbDictCopy[key]
return kbDictCopy
def removeHFEFromFile(kbDict,hfePath='hfe.utf8',encode='utf8'):
hfe = json.load(open(hfePath,'r',encoding=encode))
kbDictCopy = kbDict.copy()
for key in hfe:
del kbDictCopy[key]
return kbDictCopy
# 高频实体的定义:subject出现在training-set和testing-set中的总共次数在150次以上
def generateHighFreqEntityList(lKey,inputPath1 = 'nlpcc-iccpol-2016.kbqa.training-data',\
inputPath2 = 'nlpcc-iccpol-2016.kbqa.testing-data',\
outputPath = 'hfe',encode='utf8',threshold = 150):
fi1=open(inputPath1,'r',encoding=encode)
fi2=open(inputPath2,'r',encoding=encode)
fo=open(outputPath+'.'+encode,'w',encoding=encode)
fotxt=open(outputPath+'.txt','w',encoding=encode)
entityCountInQ = {}
listQ = []
hfe = {}
for line in fi1:
if line[1] == 'q':
listQ.append(line[line.index('\t')+1:].strip())
for line in fi2:
if line[1] == 'q':
listQ.append(line[line.index('\t')+1:].strip())
i = 0
for key in lKey:
for qStr in listQ:
if key in qStr:
if key in entityCountInQ:
entityCountInQ[key] += 1
else:
entityCountInQ[key] = 1
if key in entityCountInQ and entityCountInQ[key] > threshold:
hfe[key] = entityCountInQ[key]
i += 1
print(str(i),end='\r',flush=True)
json.dump(hfe,fo)
for key in hfe:
fotxt.write(key + ' ||| ' + str(hfe[key]) + '\n')
fi1.close()
fi2.close()
fo.close()
fotxt.close()
return hfe
def loadKB(path, encode = 'utf8'):
fi = open(path, 'r', encoding=encode)
prePattern = re.compile(r'[·•\-\s]|(\[[0-9]*\])')
kbDict={}
newEntityDic={}
i = 0
for line in fi:
i += 1
print('exporting the ' + str(i) + ' triple', end='\r', flush=True)
entityStr = line[:line.index(' |||')].strip()
tmp = line[line.index('||| ') + 4:]
relationStr = tmp[:tmp.index(' |||')].strip()
relationStr, num = prePattern.subn('', relationStr)
objectStr = tmp[tmp.index('||| ') + 4:].strip()
if relationStr == objectStr: #delete the triple if the predicate is the same as object
continue
if entityStr not in kbDict:
newEntityDic = {relationStr:objectStr}
kbDict[entityStr] = []
kbDict[entityStr].append(newEntityDic)
else:
kbDict[entityStr][len(kbDict[entityStr]) - 1][relationStr] = objectStr
fi.close()
return kbDict
def addAliasForKB(kbDictRaw):
pattern = re.compile(r'[·•\-\s]|(\[[0-9]*\])')
patternSub = re.compile(r'(\s*\(.*\)\s*)|(\s*(.*)\s*)') # subject需按照 subject (Description) || Predicate || Object 的方式抽取, 其中(Description)可选
patternBlank = re.compile(r'\s')
patternUpper = re.compile(r'[A-Z]')
patternMark = re.compile(r'《(.*)》')
kbDict = kbDictRaw.copy()
for key in list(kbDict):
if patternSub.search(key):
keyRe, num = patternSub.subn('', key)
if keyRe not in kbDict:
kbDict[keyRe] = kbDict[key]
else:
for kb in kbDict[key]:
kbDict[keyRe].append(kb)
for key in list(kbDict):
match = patternMark.search(key)
if match:
keyRe, num = patternMark.subn(r'\1', key)
if keyRe not in kbDict:
kbDict[keyRe] = kbDict[key]
else:
for kb in kbDict[key]:
kbDict[keyRe].append(kb)
for key in list(kbDict):
if patternUpper.search(key):
keyLower = key.lower()
if keyLower not in kbDict:
kbDict[keyLower] = kbDict[key]
else:
for kb in kbDict[key]:
kbDict[keyLower].append(kb)
for key in list(kbDict):
if patternBlank.search(key):
keyRe, num = patternBlank.subn('', key)
if keyRe not in kbDict:
kbDict[keyRe] = kbDict[key]
else:
for kb in kbDict[key]:
kbDict[keyRe].append(kb)
return kbDict
print('Cleaning kb......')
kbDictRaw = loadKB('nlpcc-iccpol-2016.kbqa.kb')
kbDict = addAliasForKB(kbDictRaw)
json.dump(kbDict, open('kbJson.cleanPre.alias.utf8','w',encoding='utf8'))
print('Removing HFE from kb......')
json.dump(removeHFE(kbDict,generateHighFreqEntityList(list(kbDict))),open('kbJson.cleanPre.alias.NHFE.utf8','w',encoding='utf8'))
print('\nDone!')
#把文本格式的word vector导出成Json格式供后续读入为Python的Dictionary
def convertToJson(inputPath='vectorsw300l20.all', outputPath='vectorJson.utf8'\
,encode = 'utf8'):
fi = open(inputPath,'r',encoding=encode)
ll = []
for line in fi:
ll.append(line.strip())
listTmp = []
embeddingDict = {}
for i in range(len(ll)-1):
lineTmp = ll[i+1]
listTmp = []
indexSpace = lineTmp.find(' ')
embeddingDict[lineTmp[:indexSpace]] = listTmp
lineTmp = lineTmp[indexSpace + 1:]
for j in range(300):
indexSpace = lineTmp.find(' ')
listTmp.append(float(lineTmp[:indexSpace]))
lineTmp = lineTmp[indexSpace + 1:]
print('Vector size is ' + str(len(listTmp)))
print('Dictionary size is ' + str(len(embeddingDict)))
json.dump(embeddingDict,open(outputPath,'w',encoding=encode))
print('Dumping word vector to Json format......')
convertToJson()
print('Done!')
#用训练数据训练答案模板
def getAnswerPatten(inputPath = 'nlpcc-iccpol-2016.kbqa.training-data', outputPath = 'outputAP'):
inputEncoding = 'utf8'
outputEncoding = 'utf8'
fi = open(inputPath, 'r', encoding=inputEncoding)
fo = open(outputPath, 'w', encoding=outputEncoding)
foCore = open(outputPath+'.core', 'w', encoding=outputEncoding)
qRaw = ''
p1 = re.compile(r'(啊|呀|(你知道)?吗|呢)?(?|\?)*$')
p2 = re.compile(r'来着')
p3 = re.compile(r'^呃(……)?')
p4 = re.compile(r'^请问(一下|你知道)?')
p5 = re.compile(r'^(那么|什么是|我想知道|我很好奇|有谁了解|问一下|请问你知道|谁能告诉我一下)')
p6 = re.compile(r'^((谁|(请|麻烦)?你|请|)?(能|告诉)?告诉我)')
p7 = re.compile(r'^((我想(问|请教)一下),?)')
p8 = re.compile(r'^((有人|谁|你|你们|有谁|大家)(记得|知道))')
lPattern = [p1,p2,p3,p4,p5,p6,p7,p8]
pattern = re.compile(r'[·•\-\s]|(\[[0-9]*\])') #pattern to clean predicate, in order to be consistent with KB clean method
APList = {}
APListCore = {}
for line in fi:
if line.find('<q') == 0: #question line
qRaw = line[line.index('>') + 2:].strip()
qRawCore = qRaw
for p in lPattern:
qRawCore, num = p.subn('', qRawCore)
continue
elif line.find('<t') == 0: #triple line
triple = line[line.index('>') + 2:]
s = triple[:triple.index(' |||')].strip()
triNS = triple[triple.index(' |||') + 5:]
p = triNS[:triNS.index(' |||')]
p, num = pattern.subn('', p)
if qRaw.find(s) != -1:
qRaw = qRaw.replace(s,'(SUB)', 1)
qRaw = qRaw.strip() + ' ||| ' + p
if qRawCore.find(s) != -1:
qRawCore = qRawCore.replace(s,'(SUB)', 1)
qRawCore = qRawCore.strip() + ' ||| ' + p
if qRaw in APList:
APList[qRaw] += 1
else:
APList[qRaw] = 1
if qRawCore in APListCore:
APListCore[qRawCore] += 1
else:
APListCore[qRawCore] = 1
else: continue
json.dump(APList, fo)
json.dump(APListCore, foCore)
fotxt = open(outputPath+'.txt', 'w', encoding=outputEncoding)
for key in APList:
fotxt.write(key + ' ' + str(APList[key]) + '\n')
fotxt.close()
fotxtCore = open(outputPath+'.core.txt', 'w', encoding=outputEncoding)
for key in APListCore:
fotxtCore.write(key + ' ' + str(APListCore[key]) + '\n')
fotxtCore.close()
fi.close()
fo.close()
print('Training answer pattern......')
getAnswerPatten()
print('Done!')
def getCoreQuestion(inputPath = 'nlpcc-iccpol-2016.kbqa.testing-data', encode ='utf8'):
fi = open(inputPath,'r',encoding=encode)
fo = open(inputPath+'.core', 'w',encoding=encode)
p1 = re.compile(r'(啊|呀|(你知道)?吗|呢)?(?|\?)*$')
p2 = re.compile(r'来着')
p3 = re.compile(r'^呃(……)?')
p4 = re.compile(r'^请问(一下|你知道)?')
p5 = re.compile(r'^(那么|什么是|我想知道|我很好奇|有谁了解|问一下|请问你知道|谁能告诉我一下)')
p6 = re.compile(r'^((谁|(请|麻烦)?你|请|)?(能|告诉)?告诉我)')
p7 = re.compile(r'^((我想(问|请教)一下),?)')
p8 = re.compile(r'^((有人|谁|你|你们|有谁|大家)(记得|知道))')
lPattern = [p1,p2,p3,p4,p5,p6,p7,p8]
for line in fi:
if line[1] == 'q':
qRaw = line[line.index('>') + 2:].strip()
qRawCore = qRaw
for p in lPattern:
qRawCore, num = p.subn('', qRawCore)
fo.write(line.replace(qRaw, qRawCore, 1))
else:
fo.write(line)
fi.close()
fo.close()
getCoreQuestion()