How to find phrase and sentence similarity using Skip Gram model?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to find phrase and sentence similarity using Skip Gram model?

How to find phrase and sentence similarity using Skip Gram model?

This recipe helps you find phrase and sentence similarity using Skip Gram model

0

Recipe Objective

How to find phrase and sentence similarity using Skip Gram model?

As we have discussed earlier Skip Gram, so it predicts the surrounding context words within a specific window given the current word. The input layer contains the current word and the output layer contains the setting words. The layer which is hidden contains the number of measurements wherein we need to do the representation of the current word present at the input layer.

Sentence and phrase similarity So these are terms in which we are going to detect the similarity between two sentences or phrases. In the event that equivalent words exist in both the sentences or expressions in the same request then we can say there is a sentence or phrase similarity, yet in the event that the words are in various order, at that point it should not completely consider a similar sentence or phrase. Let's understand this with practical implementation.

Step 1 - Import the necessary libraries

from nltk.tokenize import sent_tokenize, word_tokenize import warnings warnings.filterwarnings(action = 'ignore') import gensim from gensim.models import Word2Vec

Here we have imported the necessary packages along with the warnings and kept it as ignore because we know that there might be some warnings comming up when we run our program, but that can be ignored.

Step 2 - load the sample data

Sample = open("/content/alice_in_wonderland.txt","r") s = Sample.read()

Step 3 - Replace the escape character with spaces

f = s.replace("\n", " ")

Step 4 - Iterate and tokenize

import nltk nltk.download('punkt')
[nltk_data] Downloading package punkt to /root/nltk_data...
[nltk_data]   Unzipping tokenizers/punkt.zip.
True
data = [] for i in sent_tokenize(f): temp = [] for j in word_tokenize(i): temp.append(j.lower()) data.append(temp)

Here we are taking a list as variable named data which is initially empty, after that we are going take a for loop which will iterate through each sentences present in the text file, and the second for loop will tokenize the sentences into words.

Step 5 - Create a Skip Gram model

model2 = gensim.models.Word2Vec(data, min_count = 1, size = 100, window = 5, sg = 1)

Step 6 - Print the result of Skip Gram model

print("Similarity between 'alice' " + "and 'girl' - Skip Gram : ", model2.similarity('alice', 'girl'),'\n') print("Similarity between 'alice' " + "and 'boy' - Skip Gram : ", model2.similarity('alice', 'boy'), '\n') print("Similarity between 'alice' " + "and 'rabbit' - Skip Gram : ",
Similarity between 'alice' and 'girl' - Skip Gram :  0.951415 

Similarity between 'alice' and 'boy' - Skip Gram :  0.94738436 

Similarity between 'alice' and 'rabbit' - Skip Gram :  0.97160363 

Similarity between 'girl' and 'rabbit' - Skip Gram :  0.98851126 

Similarity between 'girl' and 'cat' - Skip Gram :  0.9877583

Here from the above we can see the similarity between the words, we have used the words which are repeatedly used in the Sample text. So we can see the similarity between them by using skip gram model.

Relevant Projects

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.