回答編集履歴
3
修正
test
CHANGED
@@ -120,7 +120,11 @@
|
|
120
120
|
|
121
121
|
吾輩は猫である。
|
122
122
|
|
123
|
-
名前はまだ
|
123
|
+
名前はまだない。
|
124
|
+
|
125
|
+
にゃーん。
|
126
|
+
|
127
|
+
|
124
128
|
|
125
129
|
```
|
126
130
|
|
2
修正
test
CHANGED
@@ -70,7 +70,7 @@
|
|
70
70
|
|
71
71
|
|
72
72
|
|
73
|
-
print(words
|
73
|
+
print(words)
|
74
74
|
|
75
75
|
|
76
76
|
|
@@ -123,3 +123,17 @@
|
|
123
123
|
名前はまだ無い。
|
124
124
|
|
125
125
|
```
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
**結果**
|
130
|
+
|
131
|
+
```
|
132
|
+
|
133
|
+
Dictionary(4 unique tokens: ['猫', 'ー', '名前', '吾輩'])
|
134
|
+
|
135
|
+
{'猫': 1, 'ー': 3, '名前': 2, '吾輩': 0}
|
136
|
+
|
137
|
+
[(0, 1), (1, 1)]
|
138
|
+
|
139
|
+
```
|
1
追記
test
CHANGED
@@ -1,19 +1,125 @@
|
|
1
|
-
|
1
|
+
以下のコードで動作を確認しました。ご確認ください。
|
2
|
-
|
3
|
-
> document (list of str) – Input document.
|
4
|
-
|
5
|
-
>
|
6
|
-
|
7
|
-
> [gensim: corpora.dictionary – Construct word<->id mappings](https://radimrehurek.com/gensim/corpora/dictionary.html#gensim.corpora.dictionary.Dictionary.doc2bow)
|
8
2
|
|
9
3
|
|
10
4
|
|
5
|
+
|
6
|
+
|
7
|
+
```python
|
8
|
+
|
9
|
+
import MeCab
|
10
|
+
|
11
|
+
from gensim.corpora.dictionary import Dictionary
|
12
|
+
|
11
|
-
|
13
|
+
from gensim.models import LdaModel
|
14
|
+
|
15
|
+
from gensim.models import HdpModel
|
16
|
+
|
17
|
+
from collections import defaultdict
|
12
18
|
|
13
19
|
|
14
20
|
|
21
|
+
# MeCabオブジェクトの生成
|
22
|
+
|
15
|
-
|
23
|
+
mt = MeCab.Tagger('')
|
16
24
|
|
17
25
|
|
18
26
|
|
27
|
+
mt.parse('')
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
# トピック数の設定
|
32
|
+
|
33
|
+
NUM_TOPICS = 3
|
34
|
+
|
35
|
+
hdp_num_topics = 10
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
if __name__ == "__main__":
|
40
|
+
|
41
|
+
# トレーニングデータの読み込み
|
42
|
+
|
43
|
+
# train_texts は二次元のリスト
|
44
|
+
|
45
|
+
# テキストデータを一件ずつ分かち書き(名詞、動詞、形容詞に限定)して train_texts に格納するだけ
|
46
|
+
|
47
|
+
train_texts = []
|
48
|
+
|
49
|
+
with open('data.txt', 'r',encoding='utf-8') as f:
|
50
|
+
|
51
|
+
for line in f:
|
52
|
+
|
53
|
+
text = []
|
54
|
+
|
55
|
+
node = mt.parseToNode(line.strip())
|
56
|
+
|
57
|
+
while node:
|
58
|
+
|
59
|
+
fields = node.feature.split(",")
|
60
|
+
|
61
|
+
if fields[0] == '名詞':
|
62
|
+
|
63
|
+
text.append(node.surface)
|
64
|
+
|
65
|
+
node = node.next
|
66
|
+
|
67
|
+
train_texts.append(text)
|
68
|
+
|
69
|
+
words = Dictionary(train_texts)
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
print(words[0])
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
from gensim import corpora
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
# words はさっきの単語リスト
|
82
|
+
|
83
|
+
dictionary = corpora.Dictionary(train_texts)
|
84
|
+
|
85
|
+
print(dictionary.token2id)
|
86
|
+
|
87
|
+
|
88
|
+
|
19
|
-
|
89
|
+
# no_above: 使われてる文章の割合がno_above以上の場合無視
|
90
|
+
|
91
|
+
# コメントアウト
|
92
|
+
|
93
|
+
# dictionary.filter_extremes(no_below=20, no_above=0.3)
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
dictionary.save_as_text('train.txt')
|
98
|
+
|
99
|
+
words = bytes('words', 'UTF-8')
|
100
|
+
|
101
|
+
dictionary = corpora.Dictionary.load_from_text('train.txt')
|
102
|
+
|
103
|
+
type(words)
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
vec = dictionary.doc2bow(train_texts[0])
|
108
|
+
|
109
|
+
print(vec)
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
```
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
**data.txt**
|
118
|
+
|
119
|
+
```plain
|
120
|
+
|
121
|
+
吾輩は猫である。
|
122
|
+
|
123
|
+
名前はまだ無い。
|
124
|
+
|
125
|
+
```
|