回答編集履歴
3
修正
answer
CHANGED
@@ -59,7 +59,9 @@
|
|
59
59
|
**data.txt**
|
60
60
|
```plain
|
61
61
|
吾輩は猫である。
|
62
|
-
名前はまだ
|
62
|
+
名前はまだない。
|
63
|
+
にゃーん。
|
64
|
+
|
63
65
|
```
|
64
66
|
|
65
67
|
**結果**
|
2
修正
answer
CHANGED
@@ -34,7 +34,7 @@
|
|
34
34
|
train_texts.append(text)
|
35
35
|
words = Dictionary(train_texts)
|
36
36
|
|
37
|
-
print(words
|
37
|
+
print(words)
|
38
38
|
|
39
39
|
from gensim import corpora
|
40
40
|
|
@@ -60,4 +60,11 @@
|
|
60
60
|
```plain
|
61
61
|
吾輩は猫である。
|
62
62
|
名前はまだ無い。
|
63
|
+
```
|
64
|
+
|
65
|
+
**結果**
|
66
|
+
```
|
67
|
+
Dictionary(4 unique tokens: ['猫', 'ー', '名前', '吾輩'])
|
68
|
+
{'猫': 1, 'ー': 3, '名前': 2, '吾輩': 0}
|
69
|
+
[(0, 1), (1, 1)]
|
63
70
|
```
|
1
追記
answer
CHANGED
@@ -1,10 +1,63 @@
|
|
1
|
-
|
1
|
+
以下のコードで動作を確認しました。ご確認ください。
|
2
|
-
> document (list of str) – Input document.
|
3
|
-
>
|
4
|
-
> [gensim: corpora.dictionary – Construct word<->id mappings](https://radimrehurek.com/gensim/corpora/dictionary.html#gensim.corpora.dictionary.Dictionary.doc2bow)
|
5
2
|
|
6
|
-
文字列のリストを渡せ、ということなので、従う必要があります。
|
7
3
|
|
4
|
+
```python
|
5
|
+
import MeCab
|
8
|
-
|
6
|
+
from gensim.corpora.dictionary import Dictionary
|
7
|
+
from gensim.models import LdaModel
|
8
|
+
from gensim.models import HdpModel
|
9
|
+
from collections import defaultdict
|
9
10
|
|
11
|
+
# MeCabオブジェクトの生成
|
12
|
+
mt = MeCab.Tagger('')
|
13
|
+
|
14
|
+
mt.parse('')
|
15
|
+
|
16
|
+
# トピック数の設定
|
17
|
+
NUM_TOPICS = 3
|
18
|
+
hdp_num_topics = 10
|
19
|
+
|
20
|
+
if __name__ == "__main__":
|
21
|
+
# トレーニングデータの読み込み
|
22
|
+
# train_texts は二次元のリスト
|
23
|
+
# テキストデータを一件ずつ分かち書き(名詞、動詞、形容詞に限定)して train_texts に格納するだけ
|
24
|
+
train_texts = []
|
25
|
+
with open('data.txt', 'r',encoding='utf-8') as f:
|
26
|
+
for line in f:
|
27
|
+
text = []
|
28
|
+
node = mt.parseToNode(line.strip())
|
29
|
+
while node:
|
30
|
+
fields = node.feature.split(",")
|
31
|
+
if fields[0] == '名詞':
|
32
|
+
text.append(node.surface)
|
33
|
+
node = node.next
|
34
|
+
train_texts.append(text)
|
35
|
+
words = Dictionary(train_texts)
|
36
|
+
|
37
|
+
print(words[0])
|
38
|
+
|
39
|
+
from gensim import corpora
|
40
|
+
|
41
|
+
# words はさっきの単語リスト
|
42
|
+
dictionary = corpora.Dictionary(train_texts)
|
43
|
+
print(dictionary.token2id)
|
44
|
+
|
10
|
-
|
45
|
+
# no_above: 使われてる文章の割合がno_above以上の場合無視
|
46
|
+
# コメントアウト
|
47
|
+
# dictionary.filter_extremes(no_below=20, no_above=0.3)
|
48
|
+
|
49
|
+
dictionary.save_as_text('train.txt')
|
50
|
+
words = bytes('words', 'UTF-8')
|
51
|
+
dictionary = corpora.Dictionary.load_from_text('train.txt')
|
52
|
+
type(words)
|
53
|
+
|
54
|
+
vec = dictionary.doc2bow(train_texts[0])
|
55
|
+
print(vec)
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
**data.txt**
|
60
|
+
```plain
|
61
|
+
吾輩は猫である。
|
62
|
+
名前はまだ無い。
|
63
|
+
```
|