From 76efd1332af21d29302786dd5cf69c10b6f993ce Mon Sep 17 00:00:00 2001 From: Rbasak101 <ronshekhar.basak@gmail.com> Date: Sun, 14 Nov 2021 16:14:41 -0600 Subject: [PATCH] sentimental analysis --- analysis.py | 56 ++++++++++++++++++++ analysis_experiment.py | 114 +++++++++++++++++++++++++++++++++++++++++ negative.txt | 30 +++++++++++ positive.txt | 30 +++++++++++ 4 files changed, 230 insertions(+) create mode 100644 analysis.py create mode 100644 analysis_experiment.py create mode 100644 negative.txt create mode 100644 positive.txt diff --git a/analysis.py b/analysis.py new file mode 100644 index 0000000..b07b5c3 --- /dev/null +++ b/analysis.py @@ -0,0 +1,56 @@ +from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer + +#analyzer = SentimentIntensityAnalyzer() + +def overall_sentiment(text): + v = SentimentIntensityAnalyzer() + percent_sentiment = v.polarity_scores(text) + print("Text is:", text) + print("Breakdown: ", percent_sentiment) + print("Text was rated as ", percent_sentiment['neg']*100, "% Negative") + print("Text was rated as ", percent_sentiment['neu']*100, "% Neutral") + print("Text was rated as ", percent_sentiment['pos']*100, "% Positive") + + print("Sentence overall rated As", end = " ") + + if percent_sentiment['neg'] < 0.1: + if percent_sentiment['pos']-percent_sentiment['neg'] > 0 or percent_sentiment['compound'] >= 0.5: + print("Positive") + return "Positive" + + elif percent_sentiment['pos'] < 0.1: + if percent_sentiment['pos']-percent_sentiment['neg'] <= 0 or percent_sentiment['compound'] <= -0.5: # The "=" makes it better + print("Negative") + return "Negative" + + else : + print("Neutral") + return "Neutral" + + +pos_count = 0 +pos_correct = 0 + +file_positive = open("positive.txt","r") +file_positive_lines = file_positive.readlines() +for line in file_positive_lines: + sentiment = overall_sentiment(line) + if sentiment == "Positive": + pos_correct += 1 + pos_count += 1 + +neg_count = 0 +neg_correct = 0 + +file_negative = open("negative.txt","r") +file_negative_lines = file_negative.readlines() +for line in file_negative_lines: + sentiment = overall_sentiment(line) + if sentiment == "Negative": + neg_correct += 1 + neg_count += 1 + + +print("Positive accuracy using vader= {}% via {} samples".format(pos_correct/pos_count*100.0, pos_count)) +print("Negative accuracy using vader= {}% via {} samples".format(neg_correct/neg_count*100.0, neg_count)) +print(" ") \ No newline at end of file diff --git a/analysis_experiment.py b/analysis_experiment.py new file mode 100644 index 0000000..0edc560 --- /dev/null +++ b/analysis_experiment.py @@ -0,0 +1,114 @@ +from typing import Text +from textblob import TextBlob +from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer + +# textblob gives polarity(sentiment ranging from -1 to 1) and subjectivity (ranging 0 to 1) values. +# text1 = "The food at Bankok Thai is good" +# text2 = "Thanksgiving is comming soon" +# text3 = "The ball is blue" + +# complex_text = "This does not taste good" # inaccurate when not is used +# blob1 = TextBlob(text1) +# blob2 = TextBlob(text2) +# blob3 = TextBlob(text3) + +# blob_complex_text = TextBlob(complex_text) +# print(blob1.sentiment) +# print(blob2.sentiment) +# print(blob3.sentiment) +# print(blob_complex_text.sentiment) + + +pos_count = 0 +pos_correct = 0 + +file_positive = open("positive.txt","r") +file_positive_lines = file_positive.readlines() +for line in file_positive_lines: + analysis = TextBlob(line) + + # if analysis.sentiment.subjectivity >= 0.0001: # find optimal threshold + if analysis.sentiment.polarity > 0: # find optimal threshold + pos_correct += 1 + pos_count +=1 + + +neg_count = 0 +neg_correct = 0 + +file_negative = open("negative.txt","r") +file_negative_lines = file_negative.readlines() +for line in file_negative_lines: + analysis = TextBlob(line) + + #if analysis.sentiment.subjectivity > 0.0001: # find optimal threshold + if analysis.sentiment.polarity <= 0: # find optimal threshold + neg_correct += 1 + neg_count +=1 + +print("Positive accuracy using textBlob= {}% via {} samples".format(pos_correct/pos_count*100.0, pos_count)) +print("Negative accuracy using textBlob= {}% via {} samples".format(neg_correct/neg_count*100.0, neg_count)) + +print("Using vaderSentiment") +analyzer = SentimentIntensityAnalyzer() + +# The Compound score is a metric that calculates the sum of all the lexicon ratings which have been normalized between -1(most extreme negative) and +1 (most extreme positive). +# positive sentiment : (compound score >= 0.05) +# neutral sentiment : (compound score > -0.05) and (compound score < 0.05) +# negative sentiment : (compound score <= -0.05) + +pos_count = 0 +pos_correct = 0 + +file_positive = open("positive.txt","r") +file_positive_lines = file_positive.readlines() +for line in file_positive_lines: + v = analyzer.polarity_scores(line) + if v['neg'] < 0.1: + if v['pos']-v['neg'] > 0 or v['compound'] >= 0.5: + pos_correct += 1 + pos_count += 1 + +neg_count = 0 +neg_correct = 0 + +file_negative = open("negative.txt","r") +file_negative_lines = file_negative.readlines() +for line in file_negative_lines: + v = analyzer.polarity_scores(line) + if v['pos'] < 0.1: + if v['pos']-v['neg'] <= 0 or v['compound'] <= -0.5: # The "=" makes it better + neg_correct += 1 + neg_count += 1 + + +print("Positive accuracy using vader= {}% via {} samples".format(pos_correct/pos_count*100.0, pos_count)) +print("Negative accuracy using vader= {}% via {} samples".format(neg_correct/neg_count*100.0, neg_count)) +print(" ") + + + +def overall_sentiment(text): + v = SentimentIntensityAnalyzer() + percent_sentiment = v.polarity_scores(text) + print("Text is:", text) + print("Breakdown: ", percent_sentiment) + print("Text was rated as ", percent_sentiment['neg']*100, "% Negative") + print("Text was rated as ", percent_sentiment['neu']*100, "% Neutral") + print("Text was rated as ", percent_sentiment['pos']*100, "% Positive") + + print("Sentence overall rated As", end = " ") + + if percent_sentiment['neg'] < 0.1: + if percent_sentiment['pos']-percent_sentiment['neg'] > 0 or percent_sentiment['compound'] >= 0.5: + print("Positive") + + elif percent_sentiment['pos'] < 0.1: + if percent_sentiment['pos']-percent_sentiment['neg'] <= 0 or percent_sentiment['compound'] <= -0.5: # The "=" makes it better + print("Negative") + + else : + print("Neutral") + print(" ") + +overall_sentiment("Yesterday was the worst day of my life.") \ No newline at end of file diff --git a/negative.txt b/negative.txt new file mode 100644 index 0000000..c132870 --- /dev/null +++ b/negative.txt @@ -0,0 +1,30 @@ +I cannot learn a new language. +I cannot stand him. +I am afraid I might fail. +People do not like me. +I never succeed at anything. +I don’t believe I will get the job. +I am not as good as him/her. +I can’t remember the next line! Ah, I don’t know if I can memorize this poem by the end of the week. +Before he makes any purchases, Billy likes to do his research; he’s so cheap. +The more you work with him, the more you’ll find he’s a very stubborn man. +He’s too lazy to clean the dust off the ceiling fan. +Our new boss is incredibly pushy. +She’s a very nosy child. +Let’s just say she’s dumb. +At work and at home, she’s very egotistical. +THe food is too salty. Get rid of it immediately. +Lots of people are dying from COVID. +Yesterday was the worst day of my life. +I'm going to fail some of exams this upcoming week. +I have no idea what's going on in this class. +I got fired from work today. +I have cancer. +I lost $400 from poker and blackjack last month in Nevada. +The process of manually generating sentences is very tedious. +The food is too expensive. Let's forget about this place. +My car broke down a week ago. Now I am going to be overwhelmed this upcoming week. +Becky was rude to Chad. +His high ego let to his own downfall. +I'm going to lose in this upcomming swim meet. +The fridge has rotten food and cockroaches in some containers. \ No newline at end of file diff --git a/positive.txt b/positive.txt new file mode 100644 index 0000000..af07a9f --- /dev/null +++ b/positive.txt @@ -0,0 +1,30 @@ +I can get this job and do it well. +Today is a wonderful day, when everything moves smoothly and harmoniously. +I always try to be optimistic and hope for the best. +I expect to get good grades. +I am doing my best to improve my habits and myself. +We expect everything to turn out well. +Today is going to be a wonderful day and everything is going to move smoothly and harmoniously. +I am full of energy. +I am sure there is good in this situation. +I trust myself to make the best decision. +I am enjoying the work that I am doing. +My dog always hopes for the best and expects the best. +I let go all my anger and frustrations and replace them with peace and hopes. +I can earn the money to provide a comfortable life for my family and for myself. +People love and respect me, and enjoy my company. +He always try to keep on open mind, without criticism. +Ben chooses to find hopeful and optimistic ways to look at the situation. +She feels confident and at ease in the company of any person. +I always find reasons to be happy and optimistic. +I have very good relations with my family, friends and coworkers. +Jane forgives people and treats them with respect. +I can achieve all my ambitions. +I can solve every problem and handle well every situation. +I can finish my work on time. +Everything in my life is improving and getting better. +You’re an amazing mother! +You have the best voice. I love hearing you sing! +You can do it, Chad! +Don’t give up! +This piano piece sounds excellent; you must have practiced a lot. Keep it up! \ No newline at end of file -- GitLab