-
Notifications
You must be signed in to change notification settings - Fork 2
/
connect_mongdb_back
366 lines (326 loc) · 10.3 KB
/
connect_mongdb_back
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
#!/usr/bin/python3
import re
import json
import pymongo
from itertools import groupby
from operator import itemgetter
def check_contain_chinese(check_str):
for ch in check_str:
if u'\u4e00' <= ch <= u'\u9fff':
return True
return False
def search_keywords(content):
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['BaiKe4']
mycol = mydb["keyWordsContent"]
res = []
print(content)
for i in mycol.find({"title": re.compile(content)}):
tempMap= {'ID': i['ID'], 'title': i['title'], 'imgUrl':'', 'summarys': ''}
tempMap['imgUrl']=i['imgUrl'][0:-5]+"140.jpg"
for s in i['summary']:
tempMap['summarys']+=s
tempMap['summarys']=tempMap['summarys'][0:100]
tempMap['summarys']+="..."
res.append(tempMap)
if len(res)>=20:
break
res = json.dumps(res, ensure_ascii=False)
print(res)
myclient.close()
return res
def search_item(ID):
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['BaiKe4']
mycol = mydb["keyWordsContent"]
res=''
for i in mycol.find({"ID": int(ID)}):
temp=''
del (i['_id'])
for s in i['summary']:
temp+=s
i['summary']=temp
print(i)
res = i
res = json.dumps(res, ensure_ascii=False)
print(res)
myclient.close()
return res
def get_item():
record_ID = get_current_ID()
currentID = record_ID
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['BaiKe4']
mycol = mydb["keyWordsContent"]
while not [x for x in mycol.find({"ID": currentID})]:
currentID+=1
res=''
for i in mycol.find({"ID": currentID}):
del (i['_id'])
temp = ''
for s in i['summary']:
temp += s
i['summary'] = temp
res = i
currentID += 1
record_num(record_ID, currentID, res['title'])
res = json.dumps(res, ensure_ascii=False)
print(res)
myclient.close()
return res
def record_num(oldnum, num, title):
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['BaiKe4']
mycol = mydb["recordNum"]
#mycol.insert({"ID":1,"title":"深度学习","oldId":0})
mycol.update({"ID": oldnum}, {'$set': {"ID": num, "title": title, "oldId": oldnum}})
myclient.close()
def get_current_ID():
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['BaiKe4']
mycol = mydb["recordNum"]
col = mycol.find()
for C in col:
ID = C['ID']
myclient.close()
return ID
def relation_label(keys):
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['BaiKe4']
mycol = mydb["keyWordsContent"]
sentence=[]
count=0
summary=[]
for i in mycol.find():
summary.extend(i['summary'])
myclient.close()
for S in summary:
S.split('。')
sentence.append(S)
for S in sentence:
entityList=[]
for K in keys:
if K in S:
print(K)
entityList.append(K)
if entityList:
print(entityList)
recordEntity(S,entityList,count)
print("__________处理个数_"+str(count)+"_____________")
count += 1
def recordEntity(sentence,entity,count):
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['BaiKe4']
mycol = mydb["sentenceEntity"]
mycol.insert({"ID":count,"sentence":sentence,"entity":entity})
myclient.close()
def get_keywords():
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['BaiKe4']
mycol = mydb["keyWordsContent"]
keyWordsList = []
for i in mycol.find():
keys = i['title'].split("[")[0]
if len(keys)>1:
keyWordsList.append(keys)
myclient.close()
return keyWordsList
def recordLabel(entity,label,ID):
entity = entity.split("[")[0]
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['BaiKe4']
mycol = mydb["labeledEntity"]
mycol.insert({"ID": ID, "entity": entity, "label": label})
myclient.close()
f = open('label', 'a+')
f.write(entity+" "+label+"\n")
f.close()
res = {
"state":"success"
}
res = json.dumps(res)
return res
def getUpdateCount(flag):
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['labelEntityRelation']
mycol = mydb[flag]
col = mycol.find()
for C in col:
count = C['count']
mycol.update({"count": count}, {'$set': {"count": count+1}})
myclient.close()
return count
def getCount(flag):
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['labelEntityRelation']
mycol = mydb[flag]
col = mycol.find()
for C in col:
count = C['count']
myclient.close()
return count
def recordLabelEntity(entity, entityType, sentence):
ID = getUpdateCount("countEntity")
ID += 1
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['labelEntityRelation']
mycol = mydb["labelEntity"]
mycol.insert({"ID": ID, "entity":entity,"entityType":entityType, "sentence": sentence})
myclient.close()
return ID
def recordLabelRelation(entity1, entity2, relation, sentence):
ID = getUpdateCount("countRelation")
ID += 1
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['labelEntityRelation']
mycol = mydb["labelRelation"]
mycol.insert({"ID": ID, "entity1": entity1, "relation": relation, "entity2": entity2, "sentence": sentence})
myclient.close()
return ID
def createSentence(dataSetName, dataSetColName, id):
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['labelEntityRelation']
colName = "contents"+id
mycol = mydb[colName]
countColName = 'countContents' + id
mycolCount = mydb[countColName]
mycol.drop()
mycolCount.drop()
myclient.close()
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient[dataSetName]
mycol = mydb[dataSetColName]
col = mycol.find()
sentenceList = list()
for C in col:
sentence = C['sentence']
sentenceList.extend(sentence.split("。"))
myclient.close()
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['labelEntityRelation']
mycol = mydb[colName]
count = 1
for sentence in sentenceList:
if sentence != '':
mycol.insert({"ID": count, "sentence": sentence, "flag":0})
count += 1
myclient.close()
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['labelEntityRelation']
countColName='countContents'+id
mycol = mydb[countColName]
mycol.insert({"count": 0})
myclient.close()
print("重建数据集完成")
return "success"
def loadSentence():
ID = getUpdateCount("countContents")
ID += 1
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['labelEntityRelation']
mycol = mydb["contents"]
for i in mycol.find({"ID": ID}):
sentence = i['sentence']
ID = i['ID']
mycol.update({"ID": ID}, {'$set': {"flag": 1}})
myclient.close()
print(sentence)
res = {
"sentence": sentence,
"ID": ID
}
print(res)
res = json.dumps(res, ensure_ascii=False)
return res
def searchContext(content):
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['labelEntityRelation']
mycol = mydb["contents"]
count = 0
for j in mycol.find({"sentence": re.compile(content)}):
count += 1
currentNum = 0
sentence = ''
ID = None
for i in mycol.find({"sentence": re.compile(content)}):
currentNum += 1
flag = i['flag']
if flag == 0:
sentence = i['sentence']
ID = i['ID']
mycol.update({"ID": ID}, {'$set': {"flag": 1}})
break
print(sentence)
resMap = {
"sumCount": count,
"currentNum": currentNum,
"sentence": sentence,
"ID": ID
}
res = json.dumps(resMap, ensure_ascii=False)
myclient.close()
return res
def loadLabeledDataSet(dataSetId):
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['labelEntityRelation']
mycol = mydb["labelEntity"]
entityList = list()
categoriesList = list()
for i in mycol.find():
tempMap = {
"entity": i["entity"],
"entity_type": i["entityType"],
"sentence": i["sentence"]
}
entityList.append(tempMap)
categoriesList.append(i["entityType"])
myclient.close()
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['labelEntityRelation']
mycol = mydb["labelRelation"]
relationList = list()
for i in mycol.find():
tempMap = {
"entity1": i["entity1"],
"entity2": i["entity2"],
"relation": i["relation"],
"sentence": i["sentence"]
}
relationList.append(tempMap)
myclient.close()
entityList = distinct(entityList, "entity")
categories = list(set(categoriesList))
resultMap = {
"entityList": entityList,
"relationList": relationList,
"categories": categories
}
print(resultMap)
resultMap = json.dumps(resultMap, ensure_ascii=False)
return resultMap
def distinct(items, key):
key = itemgetter(key)
items = sorted(items, key=key)
return [next(v) for _, v in groupby(items, key=key)]
if __name__=="__main__":
createSentence("BaiKe4", "sentenceEntity", "1")
#createSentence()
# myclient = pymongo.MongoClient('mongodb://localhost:27017/')
# mydb = myclient['BaiduXueshu2']
# mycol = mydb["relatePaper"]
# col = mycol.find()
# sentenceList = list()
# for C in col:
# sentence = C['abstract']
# if sentence:
# sentenceList.extend(sentence.split("。"))
# myclient.close()
# myclient = pymongo.MongoClient('mongodb://localhost:27017/')
# mydb = myclient['BaiduXueshu']
# mycol = mydb["abstract"]
# count = 1
# for sentence in sentenceList:
# if sentence != '':
# mycol.insert({"ID": count, "sentence": sentence})
# count += 1
# myclient.close()