226k views
0 votes
Write a program that asks the user for a word. Next, open up the movie reviews.txt file and examine every review one at a time. If a review contains the desired word you should make a note of the review score in an accumulator variable. Finally, produce some output that tells your user how that word was used across all reviews as well as the classification for this word (any score of 2.0 or higher can be considered

User Jeff Dege
by
4.7k points

1 Answer

7 votes

Answer:

import numpy as np

word = input("Enter a word: ")

acc = []

with open("Downloads/record-collection.txt", "r") as file:

lines = file.readlines()

for line in lines:

if word in line:

line = line.strip()

acc.append(int(line[0]))

if np.mean(acc) >= 2:

print(f"The word {word} is a positive word")

print(f"{word} appeared {len(acc)} times")

print(f"the review of the word {word} is {round(np.mean(acc), 2)}")

else:

print(f"the word {word} is a negative word with review\

{round(np.mean(acc), 2)}")

Step-by-step explanation:

The python program gets the text from the review file, using the user input word to get the definition of reviews based on the word, whether positive or negative.

The program uses the 'with' keyword to open the file and created the acc variable to hold the reviews gotten. The mean of the acc is calculated with the numpy mean method and if the mean is equal to or greater than 2 it is a positive word, else negative.

User Sujewan
by
4.2k points