Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
codebasics
GitHub Repository: codebasics/deep-learning-keras-tf-tutorial
Path: blob/master/42_word2vec_gensim/42_word2vec_gensim.ipynb
1141 views
Kernel: Python 3
# !pip install gensim # !pip install python-Levenshtein
import gensim import pandas as pd

Reading and Exploring the Dataset

The dataset we are using here is a subset of Amazon reviews from the Cell Phones & Accessories category. The data is stored as a JSON file and can be read using pandas.

Link to the Dataset: http://snap.stanford.edu/data/amazon/productGraph/categoryFiles/reviews_Cell_Phones_and_Accessories_5.json.gz

df = pd.read_json("Cell_Phones_and_Accessories_5.json", lines=True) df
df.shape
(194439, 9)

Simple Preprocessing & Tokenization

The first thing to do for any data science task is to clean the data. For NLP, we apply various processing like converting all the words to lower case, trimming spaces, removing punctuations. This is something we will do over here too.

Additionally, we can also remove stop words like 'and', 'or', 'is', 'the', 'a', 'an' and convert words to their root forms like 'running' to 'run'.

review_text = df.reviewText.apply(gensim.utils.simple_preprocess)
review_text
0 [they, look, good, and, stick, good, just, don... 1 [these, stickers, work, like, the, review, say... 2 [these, are, awesome, and, make, my, phone, lo... 3 [item, arrived, in, great, time, and, was, in,... 4 [awesome, stays, on, and, looks, great, can, b... ... 194434 [works, great, just, like, my, original, one, ... 194435 [great, product, great, packaging, high, quali... 194436 [this, is, great, cable, just, as, good, as, t... 194437 [really, like, it, becasue, it, works, well, w... 194438 [product, as, described, have, wasted, lot, of... Name: reviewText, Length: 194439, dtype: object
review_text.loc[0]
['they', 'look', 'good', 'and', 'stick', 'good', 'just', 'don', 'like', 'the', 'rounded', 'shape', 'because', 'was', 'always', 'bumping', 'it', 'and', 'siri', 'kept', 'popping', 'up', 'and', 'it', 'was', 'irritating', 'just', 'won', 'buy', 'product', 'like', 'this', 'again']
df.reviewText.loc[0]
"They look good and stick good! I just don't like the rounded shape because I was always bumping it and Siri kept popping up and it was irritating. I just won't buy a product like this again"

Training the Word2Vec Model

Train the model for reviews. Use a window of size 10 i.e. 10 words before the present word and 10 words ahead. A sentence with at least 2 words should only be considered, configure this using min_count parameter.

Workers define how many CPU threads to be used.

Initialize the model

model = gensim.models.Word2Vec( window=10, min_count=2, workers=4, )

Build Vocabulary

model.build_vocab(review_text, progress_per=1000)

Train the Word2Vec Model

model.train(review_text, total_examples=model.corpus_count, epochs=model.epochs)
(61506764, 83868975)

Save the Model

Save the model so that it can be reused in other applications

model.save("./word2vec-amazon-cell-accessories-reviews-short.model")

Finding Similar Words and Similarity between words

https://radimrehurek.com/gensim/models/word2vec.html

model.wv.most_similar("bad")
[('terrible', 0.6617082357406616), ('horrible', 0.6136840581893921), ('crappy', 0.5805919170379639), ('good', 0.5770503878593445), ('shabby', 0.5749340653419495), ('awful', 0.5492298007011414), ('ok', 0.5294141173362732), ('cheap', 0.5288074612617493), ('legit', 0.5199155807495117), ('okay', 0.5171135663986206)]
model.wv.similarity(w1="cheap", w2="inexpensive")
0.52680796
model.wv.similarity(w1="great", w2="good")
0.7714366

Further Reading

You can read about gensim more at https://radimrehurek.com/gensim/models/word2vec.html

Explore other Datasets related to Amazon Reviews: http://jmcauley.ucsd.edu/data/amazon/

Exercise

Train a word2vec model on the Sports & Outdoors Reviews Dataset Once you train a model on this, find the words most similar to 'awful' and find similarities between the following word tuples: ('good', 'great'), ('slow','steady')

Click here for solution.