# 살인의 추억 텍스트 불러오기
filename = './data/movie_memories_of_murder_2003.txt'
with open(filename, 'r') as f:
texts = f.read()
texts[:500]
from txtutil import txtnoun
skips = {'두만':'박두만', '태윤':'서태윤', '용구':'조용구', '귀옥':'권귀옥',
'희봉':'구희봉', '동철':'신동철', '광호':'백광호', '병순':'조병순',
'해일':'박해일', '광호의':'백광호', '백광호의':'백광호'}
%time texts = txtnoun(filename, skip=skips, tags=['Noun'])
texts[:500]
# 명사 Token 작업된 자료를 ssResport.txt 로 저장
texts_file = './data/mom_noun_script.txt'
with open(texts_file, 'w', encoding='utf-8') as file:
file.write(texts)
# ! cat ./data/ssResport.txt | head -n 10
# ! pip3 install gensim
%%time
texts_file = './data/mom_noun_script.txt'
from gensim.models import word2vec
data = word2vec.LineSentence(texts_file)
model = word2vec.Word2Vec(data, size=30, window=2, min_count=10, hs=1,
workers=4, iter=100, sg=1)
model.save("./data/mom_script.model")
print("model saved.")
%reset
%who
from gensim.models import word2vec
model = word2vec.Word2Vec.load('./data/mom_script.model')
len(model.wv.vocab.keys())
list(model.wv.index2word)
model.wv.most_similar(positive=['범인'])
model.wv.most_similar(negative=['범인'])
model.wv.most_similar(positive=['피해자'])
model.wv.most_similar(negative=['피해자'])
model.wv.most_similar(positive=['박두만'])
model.wv.most_similar(positive=['서태윤'])
model.wv.most_similar(positive=['조용구'])
model.wv.most_similar(positive=['박두만','서태윤'])
model.wv.most_similar(positive=['박두만','서태윤'],
negative=['피해자'])
model.wv.most_similar(positive=['박두만','서태윤'],
negative=['박해일'])
model.wv.most_similar(positive=['박두만','서태윤'],
negative=['범인'])
list(model.wv.vocab.keys())[:10]
# model.wv.vocab : { word: object of numeric vector }
vocab = list(model.wv.vocab)
X = model[vocab]
X.shape
%%time
from sklearn.manifold import TSNE
tsne = TSNE(n_components= 2)
X_tsne = tsne.fit_transform(X)
import pandas as pd
df = pd.DataFrame(X_tsne,
index = vocab,
columns=['x', 'y'])
df.head()
%matplotlib inline
from matplotlib import rc
rc('font', family=['NanumGothic','Malgun Gothic'])
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(12,12))
ax = fig.add_subplot(1, 1, 1)
ax.scatter(df['x'], df['y'])
for word, pos in df.iterrows():
ax.annotate(word, pos, fontsize=15)
plt.grid(True)