-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotPractice.py
182 lines (96 loc) · 3.05 KB
/
plotPractice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# coding: utf-8
# In[64]:
from konlpy.tag import Kkma
from gensim.models import Word2Vec
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
import csv;
# In[65]:
import matplotlib.font_manager as fm
font_location = "/home/gwangjik/문서/hanyang corps/라이브러리/NanumBarunGothic.ttf"
font_name = fm.FontProperties(fname=font_location).get_name()
plt.rc('font', family=font_name)
# In[66]:
def num_there(s):
return not(any(i.isdigit() for i in s))
# In[67]:
def getString(filename):
fr = open(filename , 'r', encoding='utf-8')
reader = csv.reader(fr)
mystr = ""
for line in reader:
step = ""
for i in range(9, len(line)):
step += line[i]
step += ' '
# ingre_tokenizer = step.split()
mystr += ''.join(e for e in step if e == ' ' or e == '\n' or e.isalnum())
mystr += ' '
return mystr
# In[68]:
def tsne_plot(model):
#"Creates and TSNE model and plots it"
labels = []
tokens = []
for word in model.wv.vocab:
tokens.append(model[word])
labels.append(word)
tsne_model = TSNE(perplexity=40, n_components=2, init='pca', n_iter=2500, random_state=23)
new_values = tsne_model.fit_transform(tokens)
x = []
y = []
for value in new_values:
x.append(value[0])
y.append(value[1])
plt.figure(figsize=(16, 16))
for i in range(len(x)):
plt.scatter(x[i], y[i])
plt.annotate(labels[i],
xy=(x[i], y[i]),
xytext=(5, 2),
textcoords='offset points',
ha='right',
va='bottom')
plt.show()
# In[69]:
kkma = Kkma()
stopWord_Ingre = {"재료" , "계량법" , "안내" , "조금"}
# In[113]:
mystr = getString("/home/gwangjik/문서/hanyang corps/데이터/만개의레시피/Text/text_recipe10000_6879000_6880000")
mystr += getString("/home/gwangjik/문서/hanyang corps/데이터/만개의레시피/Text/text_recipe10000_6870000_6871000")
# In[ ]:
tokenized = kkma.pos(mystr)
# In[ ]:
token_filtered = list(filter(lambda mytoken: mytoken[1] == "NNG" or mytoken == "NNG" or mytoken == "NNB" and not mytoken[0] in stopWord_Ingre, tokenized))
# In[ ]:
embedding_model = Word2Vec(token_filtered , size=10, window = 3, min_count=0 , workers=3, iter=10, sg=1)
# In[ ]:
labels = []
tokens = []
# In[ ]:
for word in embedding_model.wv.vocab:
tokens.append(embedding_model.wv.word_vec(word))
labels.append(word)
# In[ ]:
tsne_model = TSNE(learning_rate=100)
# In[ ]:
new_values = tsne_model.fit_transform(tokens)
# In[ ]:
x = []
y = []
for value in new_values:
x.append(value[0])
y.append(value[1])
# In[ ]:
for i in range(len(x)):
plt.scatter(x[i], y[i])
plt.annotate(labels[i],
xy=(x[i], y[i]),
xytext=(5, 2),
textcoords='offset points',
ha='right',
va='bottom')
# In[ ]:
plt.show()
# In[ ]:
print(embedding_model.wv.similar_by_word("버섯"))