-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeDictData.py
263 lines (215 loc) · 7.63 KB
/
makeDictData.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
# -*- coding: utf-8 -*-
import json
from myknputils import *
from bs4 import BeautifulSoup
import re
import copy
from pyknp import Jumanpp
from urllib.parse import unquote
#propDictName = "propDict.dict"
#predDictName = "predDict.dict"
#urlListName = "url.list"
#DBName = "database"
propDict = None
predDict = None
class dicMaker:
def __init__(self):
self.DB = []
self.genre = "NEWGAME!"
self.uniques = []
self.knp = my_knp_utils()
self.jumanpp = Jumanpp()
self.symbolReg = re.compile(r'^[!-~]+$')
def getRepName(self, tag, tags):
f = tag.features
if "Wikipediaエントリ" in f:
return f["Wikipediaエントリ"]
elif re.split('[/?+]', f["正規化代表表記"])[1] in ["もの","こと","じ","ぶつ","とき","しゃ"]:
rpn = re.split('[/?+]', f["正規化代表表記"])[0]
if rpn == "物":
rpn = "もの"
elif rpn == "者":
rpn = "もの"
elif rpn == "事":
rpn = "こと"
return tags[tag.tag_id-1].get_surface() + rpn
else:
return re.split('[/?+]', f["正規化代表表記"])[0]
def gatherUnique(self, soup):
for _a in soup.find_all("a"):
i = unquote(_a.get("href").split("/")[-1])
if self.symbolReg.match(i):
continue
r = self.knp.get_knp_result(i)
if "固有" in r.spec():
self.uniques.append(i)
print(self.uniques)
def checkUnique(self, text):
for u in self.uniques:
if len(text) > 1:
if text in u:
return u
return False
def replaceUniques(self, text):
rText = ""
result = self.jumanpp.analysis(text)
for mrph in result.mrph_list():
u = self.checkUnique(mrph.midasi)
if u:
rText += u
else:
rText += mrph.midasi
return rText
def processData(self, result, _topic):
__no_k = None
_wo_k = None
_ni_k = None
_no_k = None
_ga_k = None
_adject = None
wo_k = None
ni_k = None
no_k = None
ga_k = None
topic = None
adject = None
ts = result.tag_list()
for i in range(len(ts)):
f = ts[i].features
# 形容詞
if "用言" in f and "形" in f["用言"] and "ID" in f and"形" in f["ID"]:
adject = ts[i]
else:
adject = None
# ノ格
if "係" in f and "ノ格" in f["係"]:
if "固有" in ts[i].spec():
no_k = ts[i]
else:
no_k = None
else:
no_k = None
# 主題の有無 / ガ格
if "解析格" in f and "ガ" in f["解析格"]:
if "ハ" in f and "固有" in ts[i].spec():
topic = ts[i]
ga_k = None
#elif self.checkUnique(ts[i].repname):
# topic = ts[i]
# ga_k = None
else:
ga_k = ts[i]
else:
ga_k = None
if "係" in f and"文末" in f["係"] or "格解析結果" in f:
topic = None
# ニ格
if "二" in f:
ni_k = ts[i]
else:
ni_k = None
if "ヲ" in f:
wo_k = ts[i]
else:
wo_k = None
# 解析結果
if _adject and "体言" in f:
if topic:
self.DB.append([self.genre] + [ self.getRepName(x, ts) for x in [topic, ts[i], _adject]])
else:
self.DB.append([self.genre, _topic] + [ self.getRepName(x, ts) for x in [ts[i], _adject]])
elif __no_k:
if _ga_k or _ni_k or _wo_k :
if "体言" in f:
self.DB.append([self.genre] + [ self.getRepName(x, ts) for x in [__no_k, _ga_k or _ni_k or _wo_k , ts[i]]])
elif adject:
self.DB.append([self.genre] + [ self.getRepName(x, ts) for x in [__no_k, _ga_k or _ni_k or _wo_k , ts[i]]])
elif _ga_k or _ni_k or _wo_k :
if "体言" in f:
if topic:
self.DB.append([self.genre] + [ self.getRepName(x, ts) for x in [topic, _ga_k or _ni_k or _wo_k , ts[i]]])
else:
self.DB.append([self.genre, _topic] + [ self.getRepName(x, ts) for x in [_ga_k or _ni_k or _wo_k , ts[i]]])
elif "用言" in f and "形" in f["用言"] and ("ID" not in f or not "形" in f["ID"]):
if topic:
self.DB.append([self.genre] +[ self.getRepName(x, ts) for x in [topic, _ga_k or _ni_k or _wo_k , ts[i]]])
else:
self.DB.append([self.genre, _topic] + [ self.getRepName(x, ts) for x in [_ga_k or _ni_k or _wo_k , ts[i]]])
__no_k = _no_k
_no_k = no_k
_ga_k = ga_k
_adject = adjecta
def main(self, genreName):
#with open(propDictName) as propfd:
# propDict = json.load(propfd)
#with open(predDictName) as predfd:
# predDict = json.load(predfd)
#with open(urlListName) as urllfd:
# urlList = json.load(urllfd)
#self.genre = genreName
urlList = [
"https://dic.pixiv.net/a/"+genreName
]
knp = my_knp_utils()
dm = dicMaker()
for url in urlList:
print("=== url: " + url + " ===")
r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')
for htags in soup.find_all("h1"):
htags.extract()
for htags in soup.find_all("h2"):
htags.extract()
for htags in soup.find_all("h3"):
htags.extract()
for htags in soup.find_all("h4"):
htags.extract()
for htags in soup.find_all("h5"):
htags.extract()
for br in soup.find_all("br"):
br.extract()
dm.gatherUnique(soup.find(id="main"))
print("=== page ===")
_strings = soup.find(id="main").get_text().split("\n")
strings = []
for t in _strings:
# 前処理
t = t.strip()
t = t.replace("(","(")
t = t.replace(")",")")
if len(t.split("。")) > 2:
for __t in t.split("。")[:-1]:
if len(__t) < 150:
strings.append(dm.replaceUniques(__t))
else:
ts = __t.split("、")
tmp = ""
for _t in ts:
tmp += _t
if len(tmp) > 100:
strings.append(dm.replaceUniques(tmp))
tmp = ""
if tmp != "":
strings.append(dm.replaceUniques(tmp))
elif t == '':
continue
elif len(t) > 150:
ts = t.split("、")
print (ts)
tmp = ""
for _t in ts:
tmp += _t
if len(tmp) > 100:
strings.append(dm.replaceUniques(tmp))
tmp = ""
if tmp != "":
strings.append(dm.replaceUniques(tmp))
else:
strings.append(dm.replaceUniques(t))
results = knp.get_knp_results(strings)
print("=== knp done ===")
for x in results:
dm.processData(x, url.split("/")[-1])
print (dm.DB)
with open("./" + dm.genre + ".dct", "w") as f:
json.dump(dm.DB, f)