質問編集履歴
1
回答を参考に直してみました。 でもエラー文が出てきて分かりません。教えて下さい
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,18 +1,90 @@
|
|
1
1
|
PCで名詞のみかつリスト化された文章から分散表現取得
|
2
2
|
|
3
|
+
|
4
|
+
|
3
5
|
gensim のword2vecを使用し、分散表現はPC にファイルとして保存したいです。
|
6
|
+
|
7
|
+
mecabを使い名詞のみリスト化された文章を使いたいです。
|
8
|
+
|
9
|
+
|
4
10
|
|
5
11
|
|
6
12
|
|
7
13
|
```python3
|
8
14
|
|
15
|
+
from pymongo import MongoClient
|
16
|
+
|
17
|
+
from bs4 import BeautifulSoup
|
18
|
+
|
19
|
+
import MeCab
|
20
|
+
|
21
|
+
mecab = MeCab.Tagger ('/usr/local/lib/mecab/dic/mecab-ipadic-neologd')
|
22
|
+
|
23
|
+
def main():
|
24
|
+
|
25
|
+
recipes = []
|
26
|
+
|
27
|
+
client = MongoClient('localhost', 27017)
|
28
|
+
|
29
|
+
db = client.html.cookpad_html
|
30
|
+
|
31
|
+
collection = db.test_collection
|
32
|
+
|
33
|
+
htmls = list(db.find().limit(1))
|
34
|
+
|
35
|
+
recipes = []
|
36
|
+
|
37
|
+
for num, html in enumerate(htmls):
|
38
|
+
|
39
|
+
soup = BeautifulSoup(html["html"], 'lxml')
|
40
|
+
|
41
|
+
for steps in soup.find_all(attrs={"class": "step_text"}):
|
42
|
+
|
43
|
+
node = mecab.parseToNode(steps.get_text())
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
while node:
|
48
|
+
|
49
|
+
if node.feature.split(",")[0] == '名詞':
|
50
|
+
|
51
|
+
recipes.append(node.feature.split(",")[6])
|
52
|
+
|
53
|
+
node = node.next
|
54
|
+
|
55
|
+
recipes = list(set(recipes))
|
56
|
+
|
57
|
+
print(recipes)
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
if __name__ == '__main__':
|
62
|
+
|
63
|
+
main()
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
text = 'main()'
|
68
|
+
|
69
|
+
file = open('text_file_name.txt', 'w')
|
70
|
+
|
71
|
+
file.write(text)
|
72
|
+
|
73
|
+
file.close()
|
74
|
+
|
75
|
+
|
76
|
+
|
9
77
|
from janome.tokenizer import Tokenizer
|
10
78
|
|
11
79
|
from gensim.models import word2vec
|
12
80
|
|
13
|
-
|
81
|
+
# 単語の分かち書き&スペースで区切る
|
14
82
|
|
83
|
+
|
84
|
+
|
15
|
-
|
85
|
+
import codecs
|
86
|
+
|
87
|
+
|
16
88
|
|
17
89
|
text_space = ""
|
18
90
|
|
@@ -28,15 +100,11 @@
|
|
28
100
|
|
29
101
|
text_space += " "
|
30
102
|
|
31
|
-
|
32
|
-
|
33
103
|
# ファイル書き込み
|
34
104
|
|
35
105
|
with codecs.open('wakachigaki_file_name.txt', 'w', 'utf-8') as file:
|
36
106
|
|
37
107
|
file.write(text_space)
|
38
|
-
|
39
|
-
|
40
108
|
|
41
109
|
# Word2vecのモデルの作成
|
42
110
|
|
@@ -58,92 +126,28 @@
|
|
58
126
|
|
59
127
|
model.save('model_name.model')
|
60
128
|
|
61
|
-
|
62
|
-
|
63
129
|
# モデルの読み込みと類義語の計算
|
64
130
|
|
65
131
|
model = word2vec.Word2Vec.load("model_name.model")
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
|
66
138
|
|
67
139
|
model.most_similar(positive="単語", topn=10)
|
68
140
|
|
69
141
|
```
|
70
142
|
|
71
|
-
|
72
|
-
|
73
|
-
|
143
|
+
"word '単語' not in vocabulary" とエラー文が出たきました。
|
74
|
-
|
75
|
-
下にリスト化した文章があります。
|
76
|
-
|
77
|
-
これを使いたいです。
|
78
|
-
|
79
|
-
```python3
|
80
|
-
|
81
|
-
from pymongo import MongoClient
|
82
|
-
|
83
|
-
from bs4 import BeautifulSoup
|
84
|
-
|
85
|
-
import MeCab
|
86
|
-
|
87
|
-
mecab = MeCab.Tagger ('/usr/local/lib/mecab/dic/mecab-ipadic-neologd')
|
88
144
|
|
89
145
|
|
90
146
|
|
91
|
-
def main():
|
92
|
-
|
93
|
-
recipes = []
|
94
|
-
|
95
|
-
client = MongoClient('localhost', 27017)
|
96
|
-
|
97
|
-
db = client.html.cookpad_html
|
98
|
-
|
99
|
-
collection = db.test_collection
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
htmls = list(db.find().limit(1))
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
recipes = []
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
for num, html in enumerate(htmls):
|
112
|
-
|
113
|
-
|
147
|
+
https://lib-arts.hatenablog.com/entry/nlp_tutorial3
|
114
|
-
|
115
|
-
for steps in soup.find_all(attrs={"class": "step_text"}):
|
116
|
-
|
117
|
-
node = mecab.parseToNode(steps.get_text())
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
while node:
|
122
|
-
|
123
|
-
if node.feature.split(",")[0] == '名詞':
|
124
|
-
|
125
|
-
recipes.append(node.feature.split(",")[6])
|
126
|
-
|
127
|
-
node = node.next
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
recipes = list(set(recipes))
|
134
|
-
|
135
|
-
print(recipes)
|
136
148
|
|
137
149
|
|
138
150
|
|
139
|
-
|
151
|
+
このサイトを参考にしました。
|
140
152
|
|
141
|
-
main()
|
142
|
-
|
143
|
-
```
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
どう
|
153
|
+
どうすればいいのか分からないので教えて下さい。
|
148
|
-
|
149
|
-
急ぎで誰か教えてくれると助かります。
|