Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I can\'t figure out how to do this python problem. Can someone help me? Now You

ID: 3837437 • Letter: I

Question

I can't figure out how to do this python problem. Can someone help me?

Now You Code 3: Sentiment 1.0 Let's write a basic sentiment analyzer in Python. Sentiment analysis is the act of extracting mood from text. It has practical applications in analyzing reactions in social media, product opinions, movie reviews and much more. The 1.0 version of our sentiment analyzer will start with a string of positive and negative words. For any input text and the sentiment score will be calculated as follows: for each word in our tokenized text if word in positive_ text then increment sentiment score else if word in negative_ text then decrement sentiment score So for example, if: positive_ text = "happy glad like" negative_ text = "angry mad hate" input_ text = "Amazon makes me like so angry and mad" score = -1 [+1 for like, -1 for angry, -1 for mad] You want to write sentiment as a function: Function: sentiment Arguments: positive_ text, negative_ text, input_ text Returns: score (int) Then write a main program that executes like this: Sentiment Analyzer 1.0 Type 'quit' to exit. Enter Text: i love a good book from Amazon 2 positive. Enter Text: i hate Amazon their service makes me angry -2 negative. Enter Text: i love to hate Amazon 0 neutral. Enter Text: quit

Explanation / Answer

Code:

#!/usr/bin/python3

import sys;

def my_compare(positive_text,negative_text,input_text):

sentiment=0;

for word in input_text:
for pos in positive_text:
if word == pos:
sentiment += 1;
  
for neg in negative_text:
if word == neg:
sentiment -= 1;

mood="neutral";

if sentiment>0:
mood="positive"
elif sentiment<0:
mood="negative";

return (sentiment,mood);


if __name__ == '__main__':


positive_text=['love', 'like', 'good'];
negative_text=['hate', 'angry'];

print("Sentiment Analyzer 1.0");
print("Type 'quit' to exit");

while 1:
d=input("Enter text:");
if d=="quit":
break;
d=d.split();
(sentiment,mood)=my_compare(positive_text,negative_text,d);
print(str(sentiment) + " " + str(mood));

Output:

Sentiment Analyzer 1.0
Type 'quit' to exit
Enter text:i love a good book from amazon
2 positive
Enter text:i hate amazon their service makes me angry
-2 negative
Enter text:i love to hate amazon
0 neutral
Enter text:quit

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote