Question
Below is the question I asked a few days ago and the answer I got for it. Now I need some extra features in the
Below is the question I asked a few days ago and the answer I got for it. Now I need some extra features in the code. For example, I want to divide the frequency of positive words by 8, then I want to divide the frequency of those same words by 9, then I want to compare between them and print the lines containing those words with labeling them as positive and negative. For example, if line.count(word)/8= .1, .2 , 3 and line.count(word)/9 = .2, .1, .2, when line.count(word)/8 > line.count(word)/9 (like in the second and third position where .2>.1 and .3>.2), I want to print the line containing the word and say it's positive / negative, in the same way when line.count(word)/8 < line.count(word)/9 (like in the first position where .1<.2), I want to print the line containing the word and say it's positive / negative. I hope I could explain.
Using Python 3, open a text file containing several lines of sentences expressing sentiments like the movie was good, the product was bad, etc. Create two bags of words, one containing positive words, the other one containing negative words, then look for those words with their frequency in the text file, and print them, like excellent: 3. good:2, etc.
file = open("test.txt", "r") # opening file with test.txt
line = file.read().replace(" ", " ") # storing file as string
file.close() # closing the file
positive_words = ['excellent', 'good', 'nice', 'great', 'best']
negative_words = ['bad', 'rubbish', 'worst']
# print word count after checking word from list exist in line or not
print("Positive Word Count:")
print("======================================")
for word in positive_words:
if word in line:
print(f"{word}: {line.count(word)}")
print("======================================")
print("Negative Word Count:")
print("======================================")
for word in negative_words:
if word in line:
print(f"{word}: {line.count(word)}")
print("======================================")
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