diff --git a/examples.py b/examples.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..36ff45240508920a37c464aa5928731d3214f026 100644 --- a/examples.py +++ b/examples.py @@ -0,0 +1,8 @@ +from __future__ import print_function +from __future__ import division +from builtins import input +print(4) +print(4/5) +print(2223/223.5) +x = input() +print(x) diff --git a/negative_test.txt b/negative_test.txt new file mode 100644 index 0000000000000000000000000000000000000000..c32e4d13777d76f1a5f256d0cc9b0cd060f4e4e8 --- /dev/null +++ b/negative_test.txt @@ -0,0 +1,11 @@ + +This +movie +has +a +very +bad +plot +and +terrible +ending diff --git a/sentiment.py b/sentiment.py index 98bba406f1dfa712d3cf83b114d77903668f605c..c1acbd0bd072884da45768147d30c6ecc0c92da3 100644 --- a/sentiment.py +++ b/sentiment.py @@ -1,10 +1,162 @@ from __future__ import print_function, division - from builtins import input + def main(): - pass + average = list() + reviews=list() + + + try: + with open('sentiment.txt') as s: + reviews = list(s) + print(len(reviews)) + except OSError: + print('Does not exist') + sentiments={} + for review in reviews: + words = review.split() + for word in words: + if word != review[0]: + if word in sentiments.keys(): + sentiments[word].append(int(review[0])) + else: + sentiments[word] = list() + sentiments[word].append(int(review[0])) + print(sentiments['arty']) + print(len(sentiments['arty'])) + # x = input("Enter a word") + # if x in sentiments: + # + # output = 'The word "{y}" appears "{num}" times with an average sentiment of {av}. ' .format( + # y=x, num = len(sentiments[x]), av = sum(sentiments[x])/len(sentiments[x])) + # + # else : + # output = 'The word "{y}" appears "{num}" times with an average sentiment of {av}. '.format( + # y=x, num=0, av='None' + # ) + # print(output) + y = None + + while True: + print('Enter your choice: \n' + '1: Get the score of a word\n' + '2: Get the average score of words in a file (one word per line)\n' + '3: Find the highest/lowest scoring words in a file\n' + '4: Sort words from a file into positive.txt and negative.txt\n' + '5: Exit the program') + try: + y = int(input('Enter a number 1-5:')) + + except ValueError: + print('Invalid input') + if y>5: + print ('Invalid input') + + if y ==5: + print('Goodbye') + break + if y==1: + x = input("Enter a word") + if x in sentiments: + + output = 'The word "{y}" appears "{num}" times with an average sentiment of {av}. ' .format( + y=x, num = len(sentiments[x]), av = sum(sentiments[x])/len(sentiments[x])) + + else: + output = 'The word "{y}" appears "{num}" times with an average sentiment of {av}. '.format( + y=x, num=0, av='None' + ) + print(output) + if y==2 or y==3: + file=input('Enter a file') + try: + with open(file) as file_words: + user_file = file_words.read().split() + for word_in_file in user_file: + if word_in_file in sentiments.keys(): + average.append(sum(sentiments[word])/len(sentiments[word])) + + + if y==2: + average.append(sum(sentiments[word_in_file])/len(sentiments[word_in_file])) + if not average: + print('The average score of words in {y} is None '.format( y= file)) + else: + average_words = sum(average)/len(average) + + if average_words >= 2.01: + print('The average score of words in {y} is {av}'.format(y=file, av=average_words)) + print('The overall sentiment of {y} is positive'.format(y=file)) + if average_words <= 1.99: + print('The average score of words in {y} is {av}'.format(y=file, av=average_words)) + print('The overall sentiment of {y} is negative'.format(y=file)) + else: + print ('The average score of words in {y} is {av}'.format(y=file, av=average_words)) + print('The overall sentiment of {y} is neutral'.format(y=file)) + except IOError: + print('File could not be found') + + + if y==3: + rating = sum(sentiments[x])/len(sentiments[x]) + highest_score = max(average) + lowest_score = min(average) + + + for file_word, rate in rating.items(): + if rate == highest_score: + j = file_word + if rate == lowest_score: + y == file_word + print ('The most positive word is {phrase} with a score of {j}'.format(phrase=j, j=highest_score)) + print('The most negative word is {phrase} with a score of {min}'.format(phrase=y, min=lowest_score)) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + if __name__ == '__main__':