質問編集履歴
6
文章の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -26,6 +26,54 @@
|
|
26
26
|
|
27
27
|
```ここに言語を入力
|
28
28
|
|
29
|
+
File "TrainNB2.py", line 27, in <module>
|
30
|
+
|
31
|
+
tokens = juman.analysis(s) #文sを形態素解析してトークンを得る
|
32
|
+
|
33
|
+
File "/home/nomi/.local/lib/python3.6/site-packages/pyknp/juman/juman.py", line 91, in analysis
|
34
|
+
|
35
|
+
return self.juman(input_str, juman_format)
|
36
|
+
|
37
|
+
File "/home/nomi/.local/lib/python3.6/site-packages/pyknp/juman/juman.py", line 78, in juman
|
38
|
+
|
39
|
+
result = MList(self.juman_lines(input_str), juman_format)
|
40
|
+
|
41
|
+
File "/home/nomi/.local/lib/python3.6/site-packages/pyknp/juman/mlist.py", line 29, in __init__
|
42
|
+
|
43
|
+
mrph = Morpheme(line, mid, juman_format)
|
44
|
+
|
45
|
+
File "/home/nomi/.local/lib/python3.6/site-packages/pyknp/juman/morpheme.py", line 80, in __init__
|
46
|
+
|
47
|
+
self._parse_spec(spec.strip("\n"))
|
48
|
+
|
49
|
+
File "/home/nomi/.local/lib/python3.6/site-packages/pyknp/juman/morpheme.py", line 143, in _parse_spec
|
50
|
+
|
51
|
+
self.hinsi_id = int(parts[4])
|
52
|
+
|
53
|
+
ValueError: invalid literal for int() with base 10: '特殊'
|
54
|
+
|
55
|
+
```
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
と文章の途中の文字でエラーとなる。
|
60
|
+
|
61
|
+
これは[リンク内容](https://ensekitt.hatenablog.com/entry/juman)こちらのサイトを参考にして
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
```python
|
66
|
+
|
67
|
+
cs = s.replace(' ', ' ')
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
を加えることで解決した
|
74
|
+
|
75
|
+
```ここに言語を入力
|
76
|
+
|
29
77
|
5735it [02:05, 51.24it/s]Traceback (most recent call last):
|
30
78
|
|
31
79
|
File "TrainNB2.py", line 27, in <module>
|
@@ -46,21 +94,9 @@
|
|
46
94
|
|
47
95
|
IndexError: list index out of range
|
48
96
|
|
49
|
-
```と文章の途中の文字でエラーとなる。
|
50
|
-
|
51
|
-
これは[リンク内容](https://ensekitt.hatenablog.com/entry/juman)こちらのサイトを参考にして
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
```python
|
56
|
-
|
57
|
-
cs = s.replace(' ', ' ')
|
58
|
-
|
59
|
-
```
|
97
|
+
```
|
60
|
-
|
61
|
-
|
62
|
-
|
98
|
+
|
63
|
-
|
99
|
+
次はIndexError: list index out of rangeとエラーが生じてしまった。
|
64
100
|
|
65
101
|
上記の状態では5735までしか読み込みません(全て読み込むと5819となります。)
|
66
102
|
|
5
文章の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -112,9 +112,9 @@
|
|
112
112
|
|
113
113
|
if __name__ == "__main__":
|
114
114
|
|
115
|
-
nc = Counter()
|
115
|
+
nc = Counter()
|
116
116
|
|
117
|
-
nwc = defaultdict(lambda: Counter())
|
117
|
+
nwc = defaultdict(lambda: Counter())
|
118
118
|
|
119
119
|
global_wordset = set()
|
120
120
|
|
@@ -124,31 +124,31 @@
|
|
124
124
|
|
125
125
|
json_obj = json.loads(line)
|
126
126
|
|
127
|
-
wordset = set()
|
127
|
+
wordset = set()
|
128
128
|
|
129
|
-
nc[json_obj['category']] += 1
|
129
|
+
nc[json_obj['category']] += 1
|
130
130
|
|
131
131
|
|
132
132
|
|
133
|
-
|
133
|
+
|
134
134
|
|
135
135
|
for s in json_obj['text']:
|
136
136
|
|
137
137
|
cs = s.replace(' ', ' ')
|
138
138
|
|
139
|
-
tokens = juman.analysis(cs)
|
139
|
+
tokens = juman.analysis(cs)
|
140
140
|
|
141
|
-
base_forms = [tk for tk in tokens]
|
141
|
+
base_forms = [tk for tk in tokens] る
|
142
142
|
|
143
|
-
wordset.update(base_forms)
|
143
|
+
wordset.update(base_forms)
|
144
144
|
|
145
|
-
|
145
|
+
|
146
146
|
|
147
147
|
for word in wordset:
|
148
148
|
|
149
149
|
nwc[json_obj['category']][word] += 1
|
150
150
|
|
151
|
-
|
151
|
+
|
152
152
|
|
153
153
|
global_wordset.update(wordset)
|
154
154
|
|
4
質問内容の補足
test
CHANGED
File without changes
|
test
CHANGED
@@ -83,3 +83,105 @@
|
|
83
83
|
|
84
84
|
|
85
85
|
どのように解決すれば宜しいでしょうか?
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
```python
|
92
|
+
|
93
|
+
import json
|
94
|
+
|
95
|
+
import fileinput
|
96
|
+
|
97
|
+
from tqdm import tqdm
|
98
|
+
|
99
|
+
from janome.tokenizer import Tokenizer
|
100
|
+
|
101
|
+
from collections import Counter, defaultdict
|
102
|
+
|
103
|
+
from pyknp import Juman
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
t = Tokenizer()
|
108
|
+
|
109
|
+
juman = Juman(jumanpp=False)
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
if __name__ == "__main__":
|
114
|
+
|
115
|
+
nc = Counter() #各カテゴリの記事数カウント
|
116
|
+
|
117
|
+
nwc = defaultdict(lambda: Counter()) #各単語における各カテゴリの記事数カウント
|
118
|
+
|
119
|
+
global_wordset = set()
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
for line in tqdm(fileinput.input('-')):
|
124
|
+
|
125
|
+
json_obj = json.loads(line)
|
126
|
+
|
127
|
+
wordset = set() #記事1つに出現する単語セット
|
128
|
+
|
129
|
+
nc[json_obj['category']] += 1 #カテゴリのカウント
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
#記事に含まれる単語を取得していく
|
134
|
+
|
135
|
+
for s in json_obj['text']:
|
136
|
+
|
137
|
+
cs = s.replace(' ', ' ')
|
138
|
+
|
139
|
+
tokens = juman.analysis(cs) #文sを形態素解析してトークンを得る
|
140
|
+
|
141
|
+
base_forms = [tk for tk in tokens] #トークンから基本形を得る
|
142
|
+
|
143
|
+
wordset.update(base_forms) #基本形を与えて単語セットを更新する
|
144
|
+
|
145
|
+
#記事にどんな単語が出現したかわかったので、該当する単語のカウントをする
|
146
|
+
|
147
|
+
for word in wordset:
|
148
|
+
|
149
|
+
nwc[json_obj['category']][word] += 1
|
150
|
+
|
151
|
+
#訓練データ全体の単語セットも更新しておく
|
152
|
+
|
153
|
+
global_wordset.update(wordset)
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
categories = list(nwc.keys()) #カテゴリリスト
|
158
|
+
|
159
|
+
print('単語\t{0}'.format('\t'.join(categories)))
|
160
|
+
|
161
|
+
print('__all__\t{0}'.format('\t'.join([str(nc[c]) for c in categories])))
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
for word in global_wordset:
|
166
|
+
|
167
|
+
if word == '\t':
|
168
|
+
|
169
|
+
word = '[tab]'
|
170
|
+
|
171
|
+
for c in categories:
|
172
|
+
|
173
|
+
nwc[c][word] = nwc[c]['\t']
|
174
|
+
|
175
|
+
print('{0}\t{1}'.format(
|
176
|
+
|
177
|
+
word,
|
178
|
+
|
179
|
+
'\t'.join([str(nwc[c][word]) for c in categories])
|
180
|
+
|
181
|
+
))
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
```
|
186
|
+
|
187
|
+
全文です
|
3
文章の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -26,9 +26,11 @@
|
|
26
26
|
|
27
27
|
```ここに言語を入力
|
28
28
|
|
29
|
-
|
29
|
+
5735it [02:05, 51.24it/s]Traceback (most recent call last):
|
30
30
|
|
31
|
+
File "TrainNB2.py", line 27, in <module>
|
32
|
+
|
31
|
-
tokens = juman.analysis(cs)
|
33
|
+
tokens = juman.analysis(cs) #文sを形態素解析してトークンを得る
|
32
34
|
|
33
35
|
File "/home/nomi/.local/lib/python3.6/site-packages/pyknp/juman/juman.py", line 91, in analysis
|
34
36
|
|
2
文章の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -24,7 +24,27 @@
|
|
24
24
|
|
25
25
|
|
26
26
|
|
27
|
+
```ここに言語を入力
|
28
|
+
|
27
|
-
|
29
|
+
File "TrainNB2.py", line 27, in <module>
|
30
|
+
|
31
|
+
tokens = juman.analysis(cs)
|
32
|
+
|
33
|
+
File "/home/nomi/.local/lib/python3.6/site-packages/pyknp/juman/juman.py", line 91, in analysis
|
34
|
+
|
35
|
+
return self.juman(input_str, juman_format)
|
36
|
+
|
37
|
+
File "/home/nomi/.local/lib/python3.6/site-packages/pyknp/juman/juman.py", line 78, in juman
|
38
|
+
|
39
|
+
result = MList(self.juman_lines(input_str), juman_format)
|
40
|
+
|
41
|
+
File "/home/nomi/.local/lib/python3.6/site-packages/pyknp/juman/mlist.py", line 26, in __init__
|
42
|
+
|
43
|
+
self._mrph[-1].push_doukei(Morpheme(line[2:], mid, juman_format))
|
44
|
+
|
45
|
+
IndexError: list index out of range
|
46
|
+
|
47
|
+
```と文章の途中の文字でエラーとなる。
|
28
48
|
|
29
49
|
これは[リンク内容](https://ensekitt.hatenablog.com/entry/juman)こちらのサイトを参考にして
|
30
50
|
|
1
文章の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,17 +4,13 @@
|
|
4
4
|
|
5
5
|
```python
|
6
6
|
|
7
|
-
|
7
|
+
for s in json_obj['text']:
|
8
|
-
|
9
|
-
|
10
8
|
|
11
9
|
tokens = juman.analysis(cs)
|
12
10
|
|
13
11
|
base_forms = [tk for tk in tokens]
|
14
12
|
|
15
13
|
wordset.update(base_forms)
|
16
|
-
|
17
|
-
|
18
14
|
|
19
15
|
```
|
20
16
|
|
@@ -30,7 +26,7 @@
|
|
30
26
|
|
31
27
|
ValueError: invalid literal for int() with base 10: '特殊' と文章の途中の文字でエラーとなる。
|
32
28
|
|
33
|
-
これはhttps://ensekitt.hatenablog.com/entry/jumanこちらのサイトを参考にして
|
29
|
+
これは[リンク内容](https://ensekitt.hatenablog.com/entry/juman)こちらのサイトを参考にして
|
34
30
|
|
35
31
|
|
36
32
|
|
@@ -48,4 +44,20 @@
|
|
48
44
|
|
49
45
|
|
50
46
|
|
47
|
+
```python
|
48
|
+
|
49
|
+
for s in json_obj['text']:
|
50
|
+
|
51
|
+
cs = s.replace(' ', ' ')
|
52
|
+
|
53
|
+
tokens = juman.analysis(cs)
|
54
|
+
|
55
|
+
base_forms = [tk for tk in tokens]
|
56
|
+
|
57
|
+
wordset.update(base_forms)
|
58
|
+
|
59
|
+
```
|
60
|
+
|
61
|
+
|
62
|
+
|
51
63
|
どのように解決すれば宜しいでしょうか?
|