Let's write a better version of the basic sentiment analyzer in Python. Instead of using a string of words, this example will read a list of words positive and negative words from a file.
In fact, we've included two files for you so you don't have to come up with the positive and negative words! Just load the files and go! Of course if you want more positive and negative words you can always edit the files.
NYC3-pos.txt
NYC3-neg.txt
You will have to write a function called LoadWords(filename)
to read the words from the file and load them into a string.
Input (function arguments): filename
to read.
Output (function returns): a text
string of words as loaded from the file.
Algorithm:
open the filename for reading
read the entire file all at once, into text
return the text
In [ ]:
## Step 2: Write the LoadWords(filename) function
In [ ]:
## Quick test of your LoadWords() function
pos = LoadWords("NYC3-pos.txt")
neg = LoadWords("NYC3-neg.txt")
print("POSITIVE WORD LIST:",pos)
print("NEGATIVE WORD LIST", neg)
Now write a program which allows you to enter text and then analyzes the sentiment of the text by printing a score. The program should keep analyzing text input until you enter "quit".
Sample Run
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
Input:
Output:
Algorithm:
In [ ]:
## 3.b Write solution here, use Load Words to help you read in the pos and neg words.
Answer:
i love! a GOOD book
What is the score and why? how can this issue be fixed?Answer:
Answer:
Reflect upon your experience completing this assignment. This should be a personal narrative, in your own voice, and cite specifics relevant to the activity as to help the grader understand how you arrived at the code you submitted. Things to consider touching upon: Elaborate on the process itself. Did your original problem analysis work as designed? How many iterations did you go through before you arrived at the solution? Where did you struggle along the way and how did you overcome it? What did you learn from completing the assignment? What do you need to work on to get better? What was most valuable and least valuable about this exercise? Do you have any suggestions for improvements?
To make a good reflection, you should journal your thoughts, questions and comments while you complete the exercise.
Keep your response to between 100 and 250 words.
--== Write Your Reflection Below Here ==--