-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxiaoMiNegCommentPreProcessor.py
55 lines (44 loc) · 1.18 KB
/
xiaoMiNegCommentPreProcessor.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
"""小米消极评论观点抽取"""
import pymongo
import json
import baiduAip
client = pymongo.MongoClient("localhost", 27017)
db = client.taobao
miNegComments = []
fileName = 'json/xiaoMiNegativeComment.json'
with open(fileName, encoding='utf-8') as f_obj:
miNegComments = json.load(f_obj)
start, step = 0, 1
length = len(miNegComments)
print(length)
options = {'type': 13}
nlpClient = baiduAip.getNlpClient()
while True:
end = start + step
key = str(start) + '--' + str(end - 1)
txtList = miNegComments[start:end]
txt = '。'.join(txtList)
result = nlpClient.commentTag(txt, options)
try:
if 'items' not in result:
db.tagErr2.insert_many([{
'index': key,
'result': result,
'status': 'notIn',
}])
else:
db.tags2.insert_many([{
'index': key,
'result': result['items'],
'status': 'OK',
}])
except Exception:
db.tagErr2.insert_many([{
'index': key,
'result': result,
'status': 'err'
}])
start = end
if end >= length:
print(end)
break