Python Sentiment Analysis Tutorial: Randomly Generating “Positive” and “Negative” Words
As machine learning continues to excel with its presence in the field of technology, one essential subtopic of machine learning is sentiment analysis.
Sentiment analysis is the application of classifying tools that will determine the “sentiment” of a piece of text––may also be non-literary text in some cases.
The Importance of Sentiment Analysis
Sentiment analysis has large importance for businesses to monitor customer feedback, for artists to check responses, and for politicians to understand citizen responses online, etc.
Additionally, sentiment analysis is an essential factor for social media marketing campaigns. By scrapping data from Facebook, Twitter, Instagram, and other social media platforms, those running such campaigns can deduce how individuals are responding to such products or services. Thus, social media campaigns will modify based upon said responses to better conform to the wants of the many.
Sentiment Analysis Example
There are various usages of sentiment analysis. One could be based upon customer and client responses. For instance, “I really like this product!” would produce a “positive sentiment.” Conversely, “This product is horrible” would produce a “negative sentiment.” Another form of sentiment would be a “neutral sentiment.” For instance, “I can not decide if I like or hate this product.”
Within this program, we will not be testing for a “neutral sentiment.” That is due to the fact that most words by default are neutral. For instance, book, house, car, etc.
Analysis Using Jupyter Notebooks
- The first step within this analysis would be to import and download the “random-word” library.
- Next, the “Natural Language Toolkit” needs to be imported and applied.
- The last library needed is “Vader sentiment” which is used to test for the sentiment of a word or phrase.
To start off the notebook––which can also be ran on a normal console with some reorganization––the user will need to download “random-word” if they have not already.
pip install random-word
Next, RandomWords need to be imported, and can be seen by the following.
from random_word import RandomWords
Next, we have to do the same with the “Natural Language ToolKit” (nltk).
pip install nltk
Similarly, now we must import the toolkit.
The method in which words may be randomly generated is as follows:
r = RandomWords()randomOne = r.get_random_word()
randomTwo = r.get_random_word()
randomThree = r.get_random_word()print(randomOne, randomTwo, randomThree + ".")
Within this example, the print will return "None caloric genty.”
Next, we need to install “Vader sentiment.” This will be done in a similar fashion to the former two.
pip install vadersentiment
Then, we need to import “Vader sentiment.”
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
Within this program, we will prompt the users if they would like to randomly generate a word with positive or negative sentiment.
These two functions are “positiveRandom” and “negativeRandom.”
They are written as shown in the following.
while (running == "True"):
randomValue = r.get_random_word() print(randomValue) sentence = randomValue vs = SentimentIntensityAnalyzer().polarity_scores(sentence) for i in vs: if (i == "pos"): if (vs[i] > 0): print("\nPositive Word: " + sentence)
return(sentence) running = "False"
while(running == "True"):
randomValue = r.get_random_word() print(randomValue) sentence = randomValue vs = SentimentIntensityAnalyzer().polarity_scores(sentence) for i in vs: if (i == "neg"): if (vs[i] > 0): print("\nNegative Word: " + sentence)
running = "False"
In the above two functions, the structure of both are similar.
The purpose of the while loop is that it will continue to cycle until a positive or negative word is found––based upon the function.
The initialization of the word “vs” will be used to hold the sentiment of the randomly generated word.
Within the initialization, “vs” will hold more than one value. Thus, we need to find the “pos” or “neg” in accordance to what we are searching for.
By default, “pos” and “neg” are equal to 0. Thus, if the word would return a value larger than 0, the sentiment can be determined as negative or positive.
- When found, then the while loop will stop running and then the word will be returned.
#Take it out step further, randomly generate words until it's positive or negative based on the user's choice!value = ""running = "True"userInput = input("Would you rather generate a word with positive or negative sentiment? Type 'P' or 'N'. ")
if (userInput.lower() == "p"):
value = positiveRandom("True")
elif (userInput.lower() == "n"):
value = negativeRandom("True")
The function above is used to ask the user if they would like to generate a “positive” or “negative” word.
Within this article the application of a sentiment analysis method was applied. As can be seen, sentiment analysis has a variety of applications. Yet, within this article, “Vader sentiment” was used and did not require a “train” and “test” dataset. Normally to do a sentiment analysis function, one would need both. However, as can be seen, while not ideal, “Vader sentiment” was an applicable shortcut.
The link to the GitHub file is as follows: