Streamlit and spaCy: Create an App to Predict Sentiment and Word Similarities with Minimal Domain Knowledge
By: Khuyen Tran
Tags: Python, Naturallanguageprocessing, Application, Streamlit, NLP

Streamlit and spaCy: Create an App to Predict Sentiment and Word Similarities with Minimal Domain Knowledge

Photo by freestocks on Unsplash

Motivation

Whether you have worked with natural language processing (NLP) or not, you probably feel amazed by some applications of NLP. If you want to start working with NLP, where should you start?

There’s good news. There are many amazing NLP tools out there that enable you to work on text with minimal domain knowledge. Even better news is that it is also easy to create an NLP app!

In this article, you will learn how to

Build an app to predict the sentiment

Build an app to find word similarities

Let’s get started!

Introduction to spaCy and Streamlit

spaCy makes it easy for you to process and analyze a text in several lines of code. Streamlit is a Python library that enables you to create web apps in minutes.

Since both of these libraries make complicated jobs easy, let’s combine them to make some amazing apps with minimal effort.

spaCy

Install spaCy using

pip install -U spacy

If the instruction above does not work with you, find more instructions to install spaCy here

Install language models

python -m spacy download en_core_web_sm

Install spaCyTextBlob to predict sentiment

pip install spacytextblob

Streamlit

Install Streamlit using

pip install streamlit

Build an App to Predict Sentiment

Our goal is to create an app to predict the sentiment of a paragraph like below

To predict the sentiment, we will use spaCyTextBlob, easy sentiment analysis for spaCy using TextBlob.

On line 5, we load the English language model and assign it to nlp

On line 6 and 7, we instantiate SpaCyTextBlob class and add it to our pipeline

class and add it to our pipeline On line 10, we feed nlp function with the text we want to analyze

The polarity score is a float within the range [-1.0, 1.0]. The subjectivity is a float within the range [0.0, 1.0] where 0.0 is very objective and 1.0 is very subjective.

The polarity of 0. 75 indicates that the text is quite positive. The subjectivity of 0.9 indicates that the text is much more subjective than objective.

Now we can add a couple more lines of code to turn this script into an app

On line 10, st.title is used to create the title

is used to create the title On line 11, st.text_input is used to take the input from the user. The output is saved as user_input

is used to take the input from the user. The output is saved as One line 14 and 15, st.write is used to write the output

Let’s see how the app looks like. If you save the script above as sentiment_app.py , run the code below on the terminal to view the app on your browser

streamlit run sentiment_app.py

Interact with the app. You will see the polarity and subjectivity changes when the text changes!

Build an App to Find Similarities Between Words

SpaCy compares the word vectors of different words using an algorithm like word2vec.

Our goal is to build an app that compares the similarities between different pairs of words and finds one pair of words whose similarity is highest.

spaCy enables us to easily find the similarities 2 words by using

>>> tokens = nlp('word1 word2 word3')

>>> print(token[0], tokens[0].similarity)

(word1, word2) <similarity_score>

Start with downloading a medium-sized language model

python -m spacy download en_core_web_md

We use itertools.combination to create a list of tuples which contain all possible combination of words in the list.

(shirt, jeans) 0.73892283

(shirt, apple) 0.22021988

(jeans, apple) 0.22157806

As we might guess, ‘shirt’ and ‘jeans’ are more similar compared to other pairs of words.

Save the most similar pair of words and their similarity

shirt and jeans are the most similar with a similarity of 73.89%

It’s time to turn our script into an app

If you save the script as find_similarity_app.py , run the code below on the terminal to view the app in the browser

streamlit run find_similarity_app.py

You should see something like this

Let’s add a DataFrame to view the similarities of all pairs of words. To show a DataFrame on our app, we really don’t need to add anything much except calling the DataFrame we have just created

Time to observe our complete app!

We have just created a table showing different pairs of words and their similarities. We can also change the text input.

When changing ‘apple’ to ‘shorts’, the combination of ‘jeans’ and ‘shorts’ becomes the most similar pair of words with a similarity of 75.38%. Isn’t it so cool?

Play with the app yourself and see if you will figure out something interesting.

Conclusion

Congratulations! You have just learned how to create two apps to predict sentiment and word similarities! I hope the article gives you the motivation to start creating a natural language processing app yourself to show off your skills.

Your friends or teammates will be happier when interacting with your language model in their browser instead of reading your code. Check out this article if you want to deploy your Streamlit app.

The source code of this article could be found here.

I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.

Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these: