The goal of the assignment is to use the provided data file
The goal of the assignment is to use the provided data file to develop an algorithm that will automatically score the sentiment of a new review that the user inputs. Before a user-input review can be scored, the program would need to execute a training phase based on the reviews that are provided in the data file. The training phase would process the file as follows: 1. Read in a review 2. Assign each word in the review the score attributed to the review. 3. Enter an object into a dictionary where each entry is keyed by the word. The corresponding total score and number of occurrences are grouped as the value (You can create a simple class to store the score and occurrences, or simply store them in a list). If a word already exists in the dictionary, the program would update the score and number of occurrences. For instance, if a review with a score of 3 contains the word \"amazing\" and the dictionary already contains an entry for that word with 32 as the total score and 8 as the number of occurrences, the dictionary entry would be updated to 35 and 9, respectively. 4. Repeat Step 1 until all data is entered Once the training phase is completed, the program should prompt the user to input a movie review, which it automatically scores based on the overall average score of the words in the review as determined by the training phase. The average score of each word is computed by dividing the total score for the entry by the number of occurrences. Your responsibility is to implement the training phase as detailed in steps 1-4 above. Your implementation must be based on the Python dictionary class. Example output: Enter a review Press return to exit A weak script that ends with a quick and boring finale The review has an average value of 1.79128 Negative Sentiment Enter a review Press return to exit Loved every minute of it The review has an average value of 2.39219 Positive Sentiment
Solution
\"\"\" Author : File Name : Date : \"\"\" class Review: def __init__(self): self.score = 1 self.occurrence = 1 def update_review(self): self.score += 1 self.occurrence += 1 if __name__ == \'__main__\': review = input(\"Enter a review -- Press return to exit\ \").split(\" \") sentiment_dict = {} for x in review: if x not in sentiment_dict: sentiment_dict[x] = Review() else: sentiment_dict[x].update_review() print(sentiment_dict)