Geek Culture
Published in

Geek Culture

Python Sentiment Analysis Tutorial: Randomly Generating “Positive” and “Negative” Words

Image by Arek Socha from Pixabay

Sentiment Analysis

The Importance of Sentiment Analysis

Sentiment Analysis Example

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.
pip install random-word
from random_word import RandomWords
pip install nltk
import nltk
r = RandomWords()randomOne = r.get_random_word()
randomTwo = r.get_random_word()
randomThree = r.get_random_word()
print(randomOne, randomTwo, randomThree + ".")
pip install vadersentiment
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
def positiveRandom(running):

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"
def negativeRandom(running):

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)

return(sentence)

running = "False"
  • 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")

Conclusion

GitHub

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ali Fakhry

Reinforcement learning, artificial intelligence, and software. NYU.