Question: I have do two things for this code but I need someone to help me with the coding: Complete a program that asks the user
I have do two things for this code but I need someone to help me with the coding:
Complete a program that asks the user for a word and, using a list of positive words and a list of negative words, returns whether the word is positive, negative or neutral. If the word is positive, the program should print out a smiley face :) If the word is negative, print out a sad face :( If it's neutral (the word is not in the positive nor the negative list), print out a neutral face :|The positivity/negativity of a word is determined using two text files that you are provided in your starter file zip: - positive_words.txt - negative_words.txt Note that both files have a comments section at the top of the file (all these lines begin with a semicolon ";"), followed by a blank space. These lines should be skipped by your program. The rest of the file provides you with a list of words. Use these words within your program to generate a list of positive and negative words.
from typing import List,
def get_words(filename: str) -> List[str]:
"""
Given the name of a file which contains many words
(one word per line), return a list of all these words.
The file may have comments and a blank space at the beginning
of the file, which should be ignored. All comment lines start with
a semicolon ';'.
"""
def analyze_word(word: str, pos_words: List[str], neg_words: List[str]) -> int:
"""
Given a word, a list of positive words (all in lowercase),
and a list of negative words (all in lowercase), return whether
or not the word is positive, negative or neutral.
For a positive word, return 1.
For a negative word, return -1.
For a neutral word (one that does not appear in either the
negative words list nor the positive words list), return 0.
>>> ("happy", ['happy', 'love'], ['sad', 'angry'])
1
>>> ("angry", ['happy', 'love', 'joy'], ['sad', 'angry', 'bad'])
-1
>>> ("LOVE", ['happy', 'love', 'joy'], ['sad', 'angry', 'bad'])
1
>>> ("sAd", ['happy', 'love', 'joy'], ['sad'])
-1
>>> ("okay", ['happy', 'love', 'joy'], ['sad', 'angry'])
0
"""
Pass
if __name__ == "__main__":
positives = "positive_words.txt"
negatives = "negative_words.txt"
# Use the above filenames and the get_words function to generate two lists
# - that is, make a list of positive words, and a list of negative words
pos_words = get_words(positives)
neg_words = get_words(negatives)
# Then, in an infinite while loop, ask the user for a word and
# calculate its sentiment level. THIS CODE IS ALREADY DONE FOR YOU.
while True:
s = input("Enter a word or phrase: ")
score = analyze_word(s, pos_words, neg_words)
if score < 0:
print(":(")
elif score > 0:
print(":)")
else:
print(":|")
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
