In CIS 443 we learned about Python object oriented programming. The most exciting project I did in that class was performing sentiment analysis on restaurant Yelp reviews.
Code:
import subprocess
cmd = [‘python3′,’-m’,’textblob.download_corpora’]
subprocess.run(cmd)
print(“Working”)
import nltk
nltk.download(‘punkt’)
from textblob import TextBlob
pos_review = “””My go-to burger joint in the city! The seasoning for the fries is also killer- you really can’t go wrong with anything there. Super friendly staff and a cozy vibe inside AND on the patio!”””
neg_review = “””My dad passed away a week ago, and when I informed the waitress she told me “have a beer”. A beer doesn’t solve my father’s passing. The hole in my heart is gaping. Shortly after the owners kicked me out for crying, they begged me to return to try their “homemade ketchup”. I don’t know who to believe. All I know is that this place has poor service and the food smelled bad.”””
posblob = TextBlob(pos_review)
negblob = TextBlob(neg_review)
possent = posblob.sentences
negsent = negblob.sentences
print(f’Full Positive Review:\n{posblob}’)
print(f’Total Sentiment:\n{posblob.sentiment}\n’)
print(‘Here is how each sentence in the positive review scored:\n’)
for sentence in possent:
print(sentence)
print(f'{sentence.sentiment}\n’)
print(f’Full Negative Review:\n{negblob}’)
print(f’Total Sentiment:\n{negblob.sentiment}\n’)
print(‘Here is how each sentence in the negative review scored:\n’)
for sentence in negsent:
print(sentence)
print(f'{sentence.sentiment}\n’)

Leave a comment