Commit 7016a1fd by Carsten James Scholle

### Incomplete testing, but program working.

parent ef084774
 ... ... @@ -3,9 +3,159 @@ from __future__ import print_function, division from builtins import input review_list = [] review_scores = {} word_list = [] def high_low(word_list): high_average = 0 low_average = 6 high = '' low = '' for word in word_list: word = str(word).replace("['", '').replace("']", '') if word in review_scores: score = average_rating(word) if score < low_average: low_average = score low = word if score > high_average: high_average = score high = word print('The most positive word is "{high}" with a score of {high_average}\nThe most negative word is "{low}" with a score of {low_average}'.format(high=high, high_average=high_average, low=low, low_average=low_average)) def collection_average(word_list, user_file): sentiment = 'neutral' total = 0 counter = 0 for word in word_list: word = str(word).replace("['", '').replace("']", '') if str(word) in review_scores: total += average_rating(word) counter += 1 if counter == 0: average = None else: average = total/counter if average > 2.01: sentiment = 'positive' if average < 2.0: sentiment = 'negative' print('The average score of words in {file} is {average}.\nThe overall sentiment of {file} is {sentiment}.'.format(file=user_file, average=average, sentiment=sentiment)) def get_ratings(word): if word in review_scores: return len(review_scores[word]) else: return 0 def average_rating(word): total = 0 counter = 0 if word in review_scores: values = review_scores[word] for value in values: number = str(value).replace("'", '').replace('[','').replace(']', '') total += int(number) counter += 1 average = total/counter return average else: return None def main(): pass try: with open('sentiment.txt') as reviews: for line in reviews: review_list.append(line.strip().split(' ')) except IOError: print('File not found.') return reviews.close() for review in review_list: line = str(review).replace("['", '').replace('["', '') line = line.split(' ') score = line[0] words = line[1:] for word in words: if word in review_scores: review_scores[word].append(list(score)) else: review_scores[word] = list(score) while True: choices = ('1', '2', '3', '4', '5') print(""""Enter your choice: 1: Get the score of a word 2: Get the average score of words in a file (one word per line) 3: Find the highest/lowest scoring words in a file 4: Sort the words from a file into positive.txt and negative.txt 5: Exit the program""") choice = raw_input("Enter a number 1-5: ") if choice not in choices: print('Invalid choice\n') if choice == '1': user_word = raw_input("Enter a word: ") print('The word "{word}" appears {count} times with an average sentiment of {average}\n'.format(word=user_word, count=str(get_ratings(user_word)), average=str(average_rating(user_word)))) if choice == '2': user_file = raw_input("Enter the file name: ") try: with open(user_file) as word_collection: for line in word_collection: word_list.append(line.strip().split(' ')) except IOError: print('File not found\n') collection_average(word_list, user_file) if choice == '3': user_file = raw_input('Enter the file name: ') try: with open(user_file) as word_collection: for line in word_collection: word_list.append(line.strip().split(' ')) except IOError: print('File not found\n') high_low(word_list) if choice == '4': user_file = raw_input('Enter the file name') try: with open(user_file) as word_collection: for line in word_collection: word_list.append(line.strip().split(' ')) except IOError: print('File not found') positive = open('positives.txt', 'w+') negative = open('negatives.txt', 'w+') for word in word_list: score = average_rating(word) if score > 2.01: positive.write(str(word) + "\n") if score < 1.99: negative.write(str(word) + '\n') if choice == '5': print ("Exiting program...") return #print(len(review_list)) #print(review_scores['arty']) #print(get_ratings('arty')) #print(average_rating('arty')) if __name__ == '__main__': main()
test_words 0 → 100644
 This movie has a very bad plot and terrible ending
tests.py 0 → 100644
 import unittest import sentiment with open('sentiment.txt') as reviews: for line in reviews: sentiment.review_list.append(line.strip().split(' ')) reviews.close() for review in sentiment.review_list: line = str(review).replace("['", '').replace('["', '') line = line.split(' ') score = line[0] words = line[1:] for word in words: if word in sentiment.review_scores: sentiment.review_scores[word].append(list(score)) else: sentiment.review_scores[word] = list(score) with open('test_words') as word_collection: for line in word_collection: sentiment.word_list.append(line.strip().split(' ')) word_collection.close() class TestRatings(unittest.TestCase): def test_arty(self): self.assertEqual(sentiment.review_scores['arty'], ['3', ['1'], ['1'], ['0']]) def test_get_ratings(self): self.assertEqual(sentiment.get_ratings('arty'), 4) def test_average(self): self.assertEqual(sentiment.average_rating('arty'), 1.25) def test_average_none(self): self.assertEqual(sentiment.average_rating('scramble'), None) def test_collection_average(self): self.assertEqual(sentiment.collection_average(sentiment.word_list, 'test_words'), '''The average score of words in test_words is 1.77937803957. The overall sentiment of test_words is negative.''')
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!