Python – ChatGPT taught me about sentiment using inputs.

I wanted to make this post to show how using ChatGPT in a method to help you learn and not just get answers is super valuable. I was wanting to improve a chat python script ‘endLessChat‘ and asked ChatGPT to help me improve, it offered some good advice and mentioned way to add more custom answers as one of the suggestions. This was something I had planned but since I was logged into ChatGPT why not get some examples and do it now?

So the first example was using a python module textblob, for some reason I was having errors and I believe it to be the install or my systems config. So then it offered me other module alternative like ‘vadersentiment‘ and this one worked on first try with the example code I will post below. This allows for creating custom responses depending on the sentiment of the input, so if someone enters ‘this is stupid’ that would trigger a negative response. The vader sentiment module can do much more than this too.

The little sample code below can very easily be added to your project, depending on sentiment found then call some random responses for each type.

Note you will most likely need to install the vadersentiment module, CAUTION I found another version vader-sentiment (note the dash-version is old) and this seems to be older so use an install command like below.

pip install vadersentiment

Homebrew install command (macOS users):

python3 -m pip install vadersentiment

from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

chat = input("What you wanna chat about? ")

# Create a SentimentIntensityAnalyzer object
vader = SentimentIntensityAnalyzer()

# Get the sentiment score of the text
sentiment = vader.polarity_scores(chat)["compound"]

if sentiment > 0:
    print("Positive sentiment")
elif sentiment == 0:
    print("Neutral sentiment")
    print("Negative sentiment")

Leave a Comment