diff --git a/Test.py b/Test.py deleted file mode 100644 index 450c17078419c4166e4fc920bf2f55033110c2f7..0000000000000000000000000000000000000000 --- a/Test.py +++ /dev/null @@ -1,10 +0,0 @@ -import unittest - - -class MyTestCase(unittest.TestCase): - def test_sentim(self): - self.assertEqual(method_11) - - -if __name__ == '__main__': - unittest.main() diff --git a/sentiment.py b/sentiment.py index 3ffb8ac46cbe2fc938463c398b2c8146dd680faf..37dff73fc8befbd5b417ed52143d254b581dcd94 100644 --- a/sentiment.py +++ b/sentiment.py @@ -2,21 +2,18 @@ from __future__ import print_function, division from builtins import input - def main(): - - reviews=list() - + reviews = list() try: with open('sentiment.txt') as s: reviews = list(s) - # print(len(reviews)) + # print(len(reviews)) except OSError: print('Does not exist') - sentiments={} + sentiments = {} word = method_11(reviews, sentiments) - #print(len(sentiments['arty'])) + # print(len(sentiments['arty'])) # x = input("Enter a word") # if x in sentiments: # @@ -32,88 +29,80 @@ def main(): 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') + '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('Invalid input') - if y ==5: + if y == 5: print('Goodbye') break - if y==1: + 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])) + 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' - ) + y=x, num=0, av='None' + ) print(output) average = list() - if y==2 or y==3: - file=input('Enter a file') + 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])) - + average.append(sum(sentiments[word]) / len(sentiments[word])) - if y==2: - average.append(sum(sentiments[word_in_file])/len(sentiments[word_in_file])) + 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)) + print('The average score of words in {y} is None '.format(y=file)) else: - average_words = sum(average)/len(average) + 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: + elif 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 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)) - - - if y==3: - #rating = sum(sentiments[x])/len(sentiments[x]) - split_file= file.split() + if y == 3: + # rating = sum(sentiments[x])/len(sentiments[x]) + split_file = file.split() # highest_score = max(average) lowest_score = min(average) - p= average.append(sum_word) + p = average.append(sum_word) for check_word in split_file: if check_word in sentiments: method_11(check_word) - sum_word = sum(method_11(check_word))/len(method_11(check_word)) - - - - - + sum_word = sum(method_11(check_word)) / len(method_11(check_word)) for file_word, rate in sum_word.items(): if rate == highest_score: j = file_word if rate == lowest_score: - j== file_word - print ('The most positive word is {phrase} with a score of {j}'.format(phrase=j, j=highest_score)) + j == 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=j, min=lowest_score)) except IOError: print('File could not be found') @@ -129,7 +118,7 @@ def method_11(reviews, sentiments): else: sentiments[word] = list() sentiments[word].append(int(review[0])) - # print(sentiments['arty']) + #print(sentiments['arty']) return word diff --git a/test_method_11.py b/test_method_11.py index e0c1744eb6a1d93770668ecc3559cbd6117cf50e..523c3c2a2cfa9f5507b4b5c29b1b740a2b2e22ca 100644 --- a/test_method_11.py +++ b/test_method_11.py @@ -1,6 +1,10 @@ from unittest import TestCase +from sentiment import method_11 class TestMethod_11(TestCase): - def test_method_11(self,reviews,sentiments): - self.assertEqual(['arty'], ) + def test_method_11(self): + reviews = ['John', "jack "] + sentiments = [2,2] + self.assertEqual(method_11(reviews,sentiments)[3,0,1,1], ['arty']) +