-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_qa.py
192 lines (162 loc) · 5.56 KB
/
upload_qa.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
# -*-coding:utf-8 -*-
from elasticsearch import Elasticsearch
from openpyxl import load_workbook
from bs4 import BeautifulSoup
import urllib.request
import time, json
es = Elasticsearch(hosts='127.0.0.1')
questions = [];
answers = [];
wb = load_workbook('./excels/dt.xlsx')
sheet_names = wb.get_sheet_names()
ws = wb[sheet_names[0]]
def delete_es():
es.indices.delete(index='qa_demo')
def create_mapping():
es.indices.create('qa_demo', body={
'mappings': {
'qa': {
'properties': {
'question': {
'type': 'text',
'analyzer': 'ik_syno'
},
'answer': {
'type': 'text',
'analyzer': 'ik_syno'
},
'title': {
'type': 'text'
},
'href': {
'type': 'text'
},
'period': {
'type': 'text'
}
}
}
},
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1,
"analysis": {
"filter": {
"my_synonym_filter": {
"type": "synonym",
"synonyms_path": "analysis/synonym.txt"
}
},
"analyzer": {
"ik_syno": {
"type": "custom",
"tokenizer": "ik_max_word",
"filter": "my_synonym_filter"
},
"ik_syno_smart": {
"type": "custom",
"tokenizer": "ik_smart",
"filter": "my_synonym_filter"
}
}
}
}
})
def upload(question, answer, title, href, period):
print(locals())
print(question.strip())
res = es.index(index='qa_demo', doc_type='qa', body={
'question': question.strip(),
'answer': answer.strip(),
'title': title.strip(),
'href': href,
'period': period
})
return res
def checkSame(question, answer):
noSame = True
query_all = {
"query": {
"dis_max": {
"queries": [
{"match_phrase": {"question": question}},
{"match_phrase": {"answer": answer}}
]
}
}
}
res = es.search(index="qa_demo", body=json.dumps(query_all, ensure_ascii=False))
if len(res['hits']['hits']) > 0:
noSame = False
return noSame
def getTitle(source):
response = urllib.request.urlopen(source)
time.sleep(1)
soup = BeautifulSoup(response.read(), 'lxml')
title = u"《" + soup.select('h2.rich_media_title')[0].text.strip() + u"》"
return title
def read_xls_1(ws):
for i in range(7, 758):
question = ws.cell(row=i, column=10).value # start question
answer = ws.cell(row=i, column=11).value # start answer
source = ws.cell(row=i, column=12).value # start hyperlink
period = processPeriod(ws.cell(row=i, column=1).value)
if ws.cell(row=i, column=12).hyperlink == None:
soup = BeautifulSoup(source, "lxml")
if soup.a == None:
href = None
title = source[source.index(u'《'): source.index(u'》') + 1]
# print title
else:
href = soup.a['href']
origin_title = soup.a.text
title = origin_title[origin_title.index(u'《'): origin_title.index(u'》') + 1]
else:
href = ws.cell(row=i, column=12).hyperlink.display
origin_title = ws.cell(row=i, column=12).value
title = origin_title[origin_title.index(u'《'): origin_title.index(u'》') + 1]
# True- no same, False -same
if checkSame(question, answer):
upload(question, answer, title, href, period)
# new data from 758
def read_xls_2(ws):
for i in range(758, ws.max_row + 1):
question = ws.cell(row=i, column=10).value # start question
answer = ws.cell(row=i, column=11).value # start answer
source = ws.cell(row=i, column=12).value # start hyperlink
period = processPeriod(ws.cell(row=i, column=1).value)
if question == None:
break;
if source != None:
title = getTitle(source)
# True- no same, False -same
if checkSame(question, answer):
upload(question, answer, title, source, period)
def processPeriod(period_origin):
period = ""
# 无
if period_origin == None or "避孕" in period_origin or len(period_origin) == 0:
period = "无"
# 备孕
# 备孕期
# 孕前
elif "备孕期" in period_origin or "孕前" in period_origin:
period = "备孕"
# 孕期
# 孕早期 / 孕中期 / 孕晚期
elif "孕早期" in period_origin or "孕中期" in period_origin or "孕晚期" in period_origin:
period = "孕期"
# 产后
# 临产
# 新生儿
# 流产
elif "产后" in period_origin or "临产" in period_origin \
or "新生儿" in period_origin or "流产" in period_origin:
period = period_origin
return period
if __name__ == '__main__':
# confirm = input("运行后会删除知识库,会造成先前log无法使用")
# delete_es()
# create_mapping()
# read_xls_1(ws)
read_xls_2(ws)