Note: This code is written in Python 3.6.1 ( Gensim 2.3.0)
Python implementation and application of word2vec with Gensim
Original paper: Mikolov, T., Chen, K., Corrado, G., & Dean, J. (2013). Efficient estimation of word representations in vector space. arXiv preprint arXiv:1301.3781.
import re import numpy as np from gensim.models import Word2Vec from nltk.corpus import gutenberg from multiprocessing import Pool from scipy import spatial
sentences = list(gutenberg.sents('shakespeare-hamlet.txt')) # import the corpus and convert into a list print('Type of corpus: ', type(sentences)) print('Length of corpus: ', len(sentences))
Type of corpus: class 'list'
Length of corpus: 3106
print(sentences[0]) # title, author, and year print(sentences[1]) print(sentences[10])
['[', 'The', 'Tragedie', 'of', 'Hamlet', 'by', 'William', 'Shakespeare', '1599', ']']
['Actus', 'Primus', '.']
['Fran', '.']
Preprocess data
for i in range(len(sentences)): sentences[i] = [word.lower() for word in sentences[i] if re.match('^[a-zA-Z] ', word)] print(sentences[0]) # title, author, and year print(sentences[1]) print(sentences[10])
['the', 'tragedie', 'of', 'hamlet', 'by', 'william', 'shakespeare']
['actus', 'primus']
['fran']
model = Word2Vec(sentences = sentences, size = 100, sg = 1, window = 3, min_count = 1, iter = 10, workers = Pool()._processes) model.init_sims(replace = True)
model.save('word2vec_model') model = Word2Vec.load('word2vec_model')
model.most_similar('hamlet')
[('horatio', 0.9978846311569214),
('queene', 0.9971947073936462),
('laertes', 0.9971820116043091),
('king', 0.9968599081039429),
('mother', 0.9966716170310974),
('where', 0.9966292381286621),
('deere', 0.9965540170669556),
('ophelia', 0.9964221715927124),
('very', 0.9963752627372742),
('oh', 0.9963476657867432)]
v1 = model['king'] v2 = model['queen'] # define a function that computes cosine similarity between two words def cosine_similarity(v1, v2): return 1 - spatial.distance.cosine(v1, v2) cosine_similarity(v1, v2)
0.99437165260314941
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3