質問編集履歴
2
質問削除の取り消し
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,3 +1,369 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
+
ngramのプログラムを実行したところ、結果に不自然な点が生まれました。下に結果を載せますのでどなたか理由を教えていただけると幸いです。(カンマで区切っています)
|
4
|
+
|
5
|
+
(1)1行おきに結果が出ないところがある点
|
6
|
+
|
7
|
+
(2)空行が発生
|
8
|
+
|
9
|
+
なおJsonでURLファイルを作り実行しています。
|
10
|
+
|
11
|
+
### 発生している問題(3gramの場合)
|
12
|
+
|
13
|
+
```
|
14
|
+
|
15
|
+
, , 130867
|
16
|
+
|
17
|
+
, , 3515
|
18
|
+
|
19
|
+
, , 3132
|
20
|
+
|
21
|
+
名,古,屋 2219
|
22
|
+
|
23
|
+
, ,
|
24
|
+
|
25
|
+
1912
|
26
|
+
|
27
|
+
0,0,0 1754
|
28
|
+
|
29
|
+
,,0,0 1584
|
30
|
+
|
31
|
+
0,0,
|
32
|
+
|
33
|
+
1531
|
34
|
+
|
35
|
+
ぐ,る,な 1509
|
36
|
+
|
37
|
+
る,な,び 1508
|
38
|
+
|
39
|
+
0,0,円 1503
|
40
|
+
|
41
|
+
コ,ー,ス 1435
|
42
|
+
|
43
|
+
,
|
44
|
+
|
45
|
+
, 1432
|
46
|
+
|
47
|
+
,名,
|
48
|
+
|
49
|
+
1394
|
50
|
+
|
51
|
+
ニ,ュ,ー 1196
|
52
|
+
|
53
|
+
-,-,- 1186
|
54
|
+
|
55
|
+
名,
|
56
|
+
|
57
|
+
,3 1155
|
58
|
+
|
59
|
+
名,
|
60
|
+
|
61
|
+
,4 1155
|
62
|
+
|
63
|
+
名,
|
64
|
+
|
65
|
+
,5 1155
|
66
|
+
|
67
|
+
名,
|
68
|
+
|
69
|
+
,6 1155
|
70
|
+
|
71
|
+
名,
|
72
|
+
|
73
|
+
,7 1155
|
74
|
+
|
75
|
+
名,
|
76
|
+
|
77
|
+
,8 1155
|
78
|
+
|
79
|
+
名,
|
80
|
+
|
81
|
+
,9 1155
|
82
|
+
|
83
|
+
名,
|
84
|
+
|
85
|
+
,1 1155
|
86
|
+
|
87
|
+
名,
|
88
|
+
|
89
|
+
,2 1141
|
90
|
+
|
91
|
+
古,屋,駅 1118
|
92
|
+
|
93
|
+
メ,ニ,ュ 1038
|
94
|
+
|
95
|
+
0,円,
|
96
|
+
|
97
|
+
1011
|
98
|
+
|
99
|
+
ラ,ン,チ 947
|
100
|
+
|
101
|
+
1,名,
|
102
|
+
|
103
|
+
910
|
104
|
+
|
105
|
+
2,名,
|
106
|
+
|
107
|
+
910
|
108
|
+
|
109
|
+
3,名,
|
110
|
+
|
111
|
+
910
|
112
|
+
|
113
|
+
4,名,
|
114
|
+
|
115
|
+
910
|
116
|
+
|
117
|
+
5,名,
|
118
|
+
|
119
|
+
910
|
120
|
+
|
121
|
+
6,名,
|
122
|
+
|
123
|
+
910
|
124
|
+
|
125
|
+
7,名,
|
126
|
+
|
127
|
+
910
|
128
|
+
|
129
|
+
8,名,
|
130
|
+
|
131
|
+
910
|
132
|
+
|
133
|
+
9,名,
|
134
|
+
|
135
|
+
910
|
136
|
+
|
137
|
+
す,る,
|
138
|
+
|
139
|
+
898
|
140
|
+
|
141
|
+
す,。,
|
142
|
+
|
143
|
+
888
|
144
|
+
|
145
|
+
の,お,店 880
|
146
|
+
|
147
|
+
飲,み,放 865
|
148
|
+
|
149
|
+
み,放,題 865
|
150
|
+
|
151
|
+
ま,す,。 853
|
152
|
+
|
153
|
+
,
|
154
|
+
|
155
|
+
,
|
156
|
+
|
157
|
+
853
|
158
|
+
|
159
|
+
,名,古 847
|
160
|
+
|
161
|
+
0,名,
|
162
|
+
|
163
|
+
821
|
164
|
+
|
165
|
+
:,0,0 766
|
166
|
+
|
167
|
+
お,す,す 754
|
168
|
+
|
169
|
+
す,す,め 754
|
170
|
+
|
171
|
+
ス,ト,ラ 741
|
172
|
+
|
173
|
+
特,集,
|
174
|
+
|
175
|
+
739
|
176
|
+
|
177
|
+
あ,り,
|
178
|
+
|
179
|
+
736
|
180
|
+
|
181
|
+
レ,ス,ト 733
|
182
|
+
|
183
|
+
ト,ラ,ン 730
|
184
|
+
|
185
|
+
ネ,ッ,ト 723
|
186
|
+
|
187
|
+
,ぐ,る 721
|
188
|
+
|
189
|
+
居,酒,屋 711
|
190
|
+
|
191
|
+
ポ,イ,ン 694
|
192
|
+
|
193
|
+
イ,ン,ト 694
|
194
|
+
|
195
|
+
:,3,0 640
|
196
|
+
|
197
|
+
見,る,
|
198
|
+
|
199
|
+
625
|
200
|
+
|
201
|
+
3,0,
|
202
|
+
|
203
|
+
613
|
204
|
+
|
205
|
+
```
|
206
|
+
|
207
|
+
### 該当のソースコード
|
208
|
+
|
209
|
+
```python
|
210
|
+
|
211
|
+
# coding: utf-8
|
212
|
+
|
213
|
+
import sys
|
214
|
+
|
215
|
+
import json
|
216
|
+
|
217
|
+
import MeCab
|
218
|
+
|
219
|
+
import urllib.request, urllib.error, urllib.parse
|
220
|
+
|
221
|
+
from collections import defaultdict
|
222
|
+
|
223
|
+
from operator import itemgetter
|
224
|
+
|
225
|
+
from bs4 import BeautifulSoup
|
226
|
+
|
227
|
+
from bs4 import NavigableString
|
228
|
+
|
229
|
+
from bs4 import Comment, Declaration
|
230
|
+
|
231
|
+
class Ngram():
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
def __init__(self, N=3):
|
236
|
+
|
237
|
+
self.N = N
|
238
|
+
|
239
|
+
self.tagger = MeCab.Tagger("-Owakati")
|
240
|
+
|
241
|
+
#print (self.tagger)
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
def get(self, seq, ngram=None):
|
246
|
+
|
247
|
+
#seq = self.tagger.parse(text).split()
|
248
|
+
|
249
|
+
#print (seq)
|
250
|
+
|
251
|
+
if ngram is None:
|
252
|
+
|
253
|
+
ngram = [defaultdict(int) for x in range((self.N + 1))]
|
254
|
+
|
255
|
+
ngram[0] = None
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
for i in range(len(seq)):
|
260
|
+
|
261
|
+
for n in range(1, self.N + 1):
|
262
|
+
|
3
|
-
aaa
|
263
|
+
idx = i - n + 1 # check ngram is valid range
|
264
|
+
|
265
|
+
if idx >= 0:
|
266
|
+
|
267
|
+
key_words = []
|
268
|
+
|
269
|
+
for j in range(idx, i+1):
|
270
|
+
|
271
|
+
key_words.append(seq[j])
|
272
|
+
|
273
|
+
key = ','.join(key_words)
|
274
|
+
|
275
|
+
ngram[n][key] += 1
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
return ngram
|
280
|
+
|
281
|
+
class HTMLParser():
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
def get(self, url):
|
286
|
+
|
287
|
+
try:
|
288
|
+
|
289
|
+
c = urllib.request.urlopen(url)
|
290
|
+
|
291
|
+
except:
|
292
|
+
|
293
|
+
print("Could not open %s" % url)
|
294
|
+
|
295
|
+
return ""
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
soup = BeautifulSoup(c.read(), "lxml")
|
300
|
+
|
301
|
+
text = '\n'.join(self.__getNavigableStrings(soup))
|
302
|
+
|
303
|
+
return text
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
def __getNavigableStrings(self, soup):
|
308
|
+
|
309
|
+
if isinstance(soup, NavigableString):
|
310
|
+
|
311
|
+
if type(soup) not in (Comment, Declaration) and soup.strip():
|
312
|
+
|
313
|
+
yield soup
|
314
|
+
|
315
|
+
elif soup.name not in ('script', 'style'):
|
316
|
+
|
317
|
+
for c in soup.contents:
|
318
|
+
|
319
|
+
for g in self.__getNavigableStrings(c):
|
320
|
+
|
321
|
+
yield g
|
322
|
+
|
323
|
+
if __name__ == "__main__":
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
f = open("urls.json", "r")
|
328
|
+
|
329
|
+
urls = json.load(f)
|
330
|
+
|
331
|
+
f.close()
|
332
|
+
|
333
|
+
print("Count of urls : " + str(len(urls)))
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
N = 10
|
338
|
+
|
339
|
+
hp = HTMLParser()
|
340
|
+
|
341
|
+
ng = Ngram(N)
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
ngram = None
|
346
|
+
|
347
|
+
for url in urls:
|
348
|
+
|
349
|
+
text = hp.get(url)
|
350
|
+
|
351
|
+
ngram = ng.get(text, ngram)
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
for n in range(1, (N + 1)):
|
356
|
+
|
357
|
+
f = open('outputs/{:02d}.tsv'.format(n), 'w')
|
358
|
+
|
359
|
+
out = ""
|
360
|
+
|
361
|
+
for k, v in sorted(list(ngram[n].items()), key=itemgetter(1), reverse=True):
|
362
|
+
|
363
|
+
out += "{}\t{}\n".format(k, v)
|
364
|
+
|
365
|
+
f.write(out)
|
366
|
+
|
367
|
+
f.close()
|
368
|
+
|
369
|
+
```
|
1
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,403 +1,3 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
-
ngramのプログラムを実行したところ、結果に不自然な点が生まれました。下に結果を載せますのでどなたか理由を教えていただけると幸いです。(カンマで区切っています)
|
4
|
-
|
5
|
-
(1)1行おきに結果が出ないところがある点
|
6
|
-
|
7
|
-
(2)空行が発生
|
8
|
-
|
9
|
-
なおJsonでURLファイルを作り実行しています。
|
10
|
-
|
11
|
-
### 発生している問題(3gramの場合)
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
```
|
16
|
-
|
17
|
-
, , 130867
|
18
|
-
|
19
|
-
, , 3515
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
, , 3132
|
24
|
-
|
25
|
-
名,古,屋 2219
|
26
|
-
|
27
|
-
, ,
|
28
|
-
|
29
|
-
1912
|
30
|
-
|
31
|
-
0,0,0 1754
|
32
|
-
|
33
|
-
,,0,0 1584
|
34
|
-
|
35
|
-
0,0,
|
36
|
-
|
37
|
-
1531
|
38
|
-
|
39
|
-
ぐ,る,な 1509
|
40
|
-
|
41
|
-
る,な,び 1508
|
42
|
-
|
43
|
-
0,0,円 1503
|
44
|
-
|
45
|
-
コ,ー,ス 1435
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
,
|
50
|
-
|
51
|
-
, 1432
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
,名,
|
56
|
-
|
57
|
-
1394
|
58
|
-
|
59
|
-
ニ,ュ,ー 1196
|
60
|
-
|
61
|
-
-,-,- 1186
|
62
|
-
|
63
|
-
名,
|
64
|
-
|
65
|
-
,3 1155
|
66
|
-
|
67
|
-
名,
|
68
|
-
|
69
|
-
,4 1155
|
70
|
-
|
71
|
-
名,
|
72
|
-
|
73
|
-
,5 1155
|
74
|
-
|
75
|
-
名,
|
76
|
-
|
77
|
-
,6 1155
|
78
|
-
|
79
|
-
名,
|
80
|
-
|
81
|
-
,7 1155
|
82
|
-
|
83
|
-
名,
|
84
|
-
|
85
|
-
,8 1155
|
86
|
-
|
87
|
-
名,
|
88
|
-
|
89
|
-
,9 1155
|
90
|
-
|
91
|
-
名,
|
92
|
-
|
93
|
-
,1 1155
|
94
|
-
|
95
|
-
名,
|
96
|
-
|
97
|
-
,2 1141
|
98
|
-
|
99
|
-
古,屋,駅 1118
|
100
|
-
|
101
|
-
メ,ニ,ュ 1038
|
102
|
-
|
103
|
-
0,円,
|
104
|
-
|
105
|
-
1011
|
106
|
-
|
107
|
-
ラ,ン,チ 947
|
108
|
-
|
109
|
-
1,名,
|
110
|
-
|
111
|
-
910
|
112
|
-
|
113
|
-
2,名,
|
114
|
-
|
115
|
-
910
|
116
|
-
|
117
|
-
3,名,
|
118
|
-
|
119
|
-
910
|
120
|
-
|
121
|
-
4,名,
|
122
|
-
|
123
|
-
910
|
124
|
-
|
125
|
-
5,名,
|
126
|
-
|
127
|
-
910
|
128
|
-
|
129
|
-
6,名,
|
130
|
-
|
131
|
-
910
|
132
|
-
|
133
|
-
7,名,
|
134
|
-
|
135
|
-
910
|
136
|
-
|
137
|
-
8,名,
|
138
|
-
|
139
|
-
910
|
140
|
-
|
141
|
-
9,名,
|
142
|
-
|
143
|
-
910
|
144
|
-
|
145
|
-
す,る,
|
146
|
-
|
147
|
-
898
|
148
|
-
|
149
|
-
す,。,
|
150
|
-
|
151
|
-
888
|
152
|
-
|
153
|
-
の,お,店 880
|
154
|
-
|
155
|
-
飲,み,放 865
|
156
|
-
|
157
|
-
み,放,題 865
|
158
|
-
|
159
|
-
ま,す,。 853
|
160
|
-
|
161
|
-
,
|
162
|
-
|
163
|
-
,
|
164
|
-
|
165
|
-
853
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
,名,古 847
|
170
|
-
|
171
|
-
0,名,
|
172
|
-
|
173
|
-
821
|
174
|
-
|
175
|
-
:,0,0 766
|
176
|
-
|
177
|
-
お,す,す 754
|
178
|
-
|
179
|
-
す,す,め 754
|
180
|
-
|
181
|
-
ス,ト,ラ 741
|
182
|
-
|
183
|
-
特,集,
|
184
|
-
|
185
|
-
739
|
186
|
-
|
187
|
-
あ,り,
|
188
|
-
|
189
|
-
736
|
190
|
-
|
191
|
-
レ,ス,ト 733
|
192
|
-
|
193
|
-
ト,ラ,ン 730
|
194
|
-
|
195
|
-
ネ,ッ,ト 723
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
,ぐ,る 721
|
200
|
-
|
201
|
-
居,酒,屋 711
|
202
|
-
|
203
|
-
ポ,イ,ン 694
|
204
|
-
|
205
|
-
イ,ン,ト 694
|
206
|
-
|
207
|
-
:,3,0 640
|
208
|
-
|
209
|
-
見,る,
|
210
|
-
|
211
|
-
625
|
212
|
-
|
213
|
-
3,0,
|
214
|
-
|
215
|
-
613
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
```
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
### 該当のソースコード
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
```python
|
228
|
-
|
229
|
-
# coding: utf-8
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
import sys
|
234
|
-
|
235
|
-
import json
|
236
|
-
|
237
|
-
import MeCab
|
238
|
-
|
239
|
-
import urllib.request, urllib.error, urllib.parse
|
240
|
-
|
241
|
-
from collections import defaultdict
|
242
|
-
|
243
|
-
from operator import itemgetter
|
244
|
-
|
245
|
-
from bs4 import BeautifulSoup
|
246
|
-
|
247
|
-
from bs4 import NavigableString
|
248
|
-
|
249
|
-
from bs4 import Comment, Declaration
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
class Ngram():
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
def __init__(self, N=3):
|
258
|
-
|
259
|
-
self.N = N
|
260
|
-
|
261
|
-
|
3
|
+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
262
|
-
|
263
|
-
#print (self.tagger)
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
def get(self, seq, ngram=None):
|
268
|
-
|
269
|
-
#seq = self.tagger.parse(text).split()
|
270
|
-
|
271
|
-
#print (seq)
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
if ngram is None:
|
276
|
-
|
277
|
-
ngram = [defaultdict(int) for x in range((self.N + 1))]
|
278
|
-
|
279
|
-
ngram[0] = None
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
for i in range(len(seq)):
|
284
|
-
|
285
|
-
for n in range(1, self.N + 1):
|
286
|
-
|
287
|
-
idx = i - n + 1 # check ngram is valid range
|
288
|
-
|
289
|
-
if idx >= 0:
|
290
|
-
|
291
|
-
key_words = []
|
292
|
-
|
293
|
-
for j in range(idx, i+1):
|
294
|
-
|
295
|
-
key_words.append(seq[j])
|
296
|
-
|
297
|
-
key = ','.join(key_words)
|
298
|
-
|
299
|
-
ngram[n][key] += 1
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
return ngram
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
class HTMLParser():
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
def get(self, url):
|
314
|
-
|
315
|
-
try:
|
316
|
-
|
317
|
-
c = urllib.request.urlopen(url)
|
318
|
-
|
319
|
-
except:
|
320
|
-
|
321
|
-
print("Could not open %s" % url)
|
322
|
-
|
323
|
-
return ""
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
soup = BeautifulSoup(c.read(), "lxml")
|
328
|
-
|
329
|
-
text = '\n'.join(self.__getNavigableStrings(soup))
|
330
|
-
|
331
|
-
return text
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
def __getNavigableStrings(self, soup):
|
336
|
-
|
337
|
-
if isinstance(soup, NavigableString):
|
338
|
-
|
339
|
-
if type(soup) not in (Comment, Declaration) and soup.strip():
|
340
|
-
|
341
|
-
yield soup
|
342
|
-
|
343
|
-
elif soup.name not in ('script', 'style'):
|
344
|
-
|
345
|
-
for c in soup.contents:
|
346
|
-
|
347
|
-
for g in self.__getNavigableStrings(c):
|
348
|
-
|
349
|
-
yield g
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
if __name__ == "__main__":
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
f = open("urls.json", "r")
|
360
|
-
|
361
|
-
urls = json.load(f)
|
362
|
-
|
363
|
-
f.close()
|
364
|
-
|
365
|
-
print("Count of urls : " + str(len(urls)))
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
N = 10
|
370
|
-
|
371
|
-
hp = HTMLParser()
|
372
|
-
|
373
|
-
ng = Ngram(N)
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
ngram = None
|
378
|
-
|
379
|
-
for url in urls:
|
380
|
-
|
381
|
-
text = hp.get(url)
|
382
|
-
|
383
|
-
ngram = ng.get(text, ngram)
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
for n in range(1, (N + 1)):
|
388
|
-
|
389
|
-
f = open('outputs/{:02d}.tsv'.format(n), 'w')
|
390
|
-
|
391
|
-
out = ""
|
392
|
-
|
393
|
-
for k, v in sorted(list(ngram[n].items()), key=itemgetter(1), reverse=True):
|
394
|
-
|
395
|
-
out += "{}\t{}\n".format(k, v)
|
396
|
-
|
397
|
-
f.write(out)
|
398
|
-
|
399
|
-
f.close()
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
```
|