Question
Using python to solve the question.Thank you. In this part of the homework, you will implement a very rough sentiment analysis tool. While the real
Using python to solve the question.Thank you.
In this part of the homework, you will implement a very rough sentiment analysis tool. While the real tools use natural language processing, they all use word counts similar to the one we use here. Understanding the sentiment in messages is a crucial part of a lot of artificial intelligence tools. Write a program that will ask the user for a string containing a sentence. The program will then compute the happiness and sadness level of the sentence is using two functions described below. If the happiness level is higher than sadness level, then the tone of the sentence is happy. If otherwise the sadness level is higher, then the tone of the sentence is sad. Otherwise, it is neutral. Find and print the tone of the sentence. Here is an example run of this program:
Enter a sentence => Dr. Horrible's Sing-Along Blog is an excellent show.
Dr. Horrible's Sing-Along Blog is an excellent show.
Percentages. happy: 0.125 sad: 0.125
This is a neutral sentence To accomplish this you will write a function called percentage_happy(sentence) which returns the percentage of words in a given string called sentence that are happy. To do this, find the total count of the following 5 words: laugh happiness love excellent good and divide this by the total number of words in the sentence. Here is an example run of this function: >>> percentage_happy("I laughed and laughed at her excellent joke.") 0.375 This is because the count of happy words is 3 (laugh is repeated twice) and total number of words is 8. So, 3/8 = 0.375. How do you find the number of words? You can count spaces and assume there is only one space between words for now. Your code should work even if there are upper and lower case words and extra spaces in the beginning and end of the sentence. >>> percentage_happy(" Happiness is the state of a student who started homework early. ") 0.09090909090909091 Next, write a second function called percentage_sad(sentence) that works the same way but instead counts the percentage of the following 5 sad words in English: bad sad terrible horrible problem (there are sadder words for sure, but no reason to depress ourselves). >>> percentage_sad("Dr. Horrible's Sing-Along Blog is an excellent show.") 0.125 >>> percentage_sad("Alexander and the Terrible, Horrible, No Good, Very Bad Day") 0.3 Of courses, there are more than 5 words of each category. We will see how to feed them using a file and use lists to process them in future classes.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started