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

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

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.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

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.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.