diff --git a/negative_test.txt b/negative_test.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/sentiment.py b/sentiment.py index 0f71ee22149b0496d23a61ba63bc6a59c8647068..2a7e08a7704cadeb2904f94a07d438630c917261 100644 --- a/sentiment.py +++ b/sentiment.py @@ -4,42 +4,77 @@ from builtins import input import sys -def main(): - 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\n') +def sentiment(): + try: + file = open('sentiment.txt').read() + lines = file.splitlines() + i = input("Enter a word: ") + count = 0 + ratings = 0 + for line in lines: + words = line.split(" ") + for word in words: + if (i == word): + count += 1 + ratings += float(words[0]) + if count == 0: + print("The word \"" + i + "\" appears " + str(count) + " times with an average sentiment of None.") + else: + print("The word \"" + i + "\" appears " + str(count) + " times with an average sentiment of " + str( + ratings / float(count)) + ".") + + except IOError: + print("Cannot read from that file") + + +pass + -choice = raw_input('Enter a number 1-5: ') +def collection_average(): + file = open('negative_test.txt').read() + lines = file.splitlines() + i = input("Enter a filename: ") + count = 0 + ratings = 0 + for line in lines: + words = line.split(" ") + for word in words: + if (i == word): + count += 1 + ratings += float(words[0]) + if count == 0: + print("The word \"" + i + "\" appears " + str(count) + " times with an average sentiment of None.") + else: + print("The word \"" + i + "\" appears " + str(count) + " times with an average sentiment of " + str( + ratings / float(count)) + ".") -if choice == 5: - print('Goodbye!') - ans = [] - sentiments = dict() - total = 0 +pass + + +def main(): + 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") + + choice = input("Enter a number 1-5: ") + try: - with open('sentiment.txt', 'r') as f: - lines = f.read().splitlines() # split lines - user_search_value = raw_input('Please enter a word: ') - for line in f.readlines(): - line = line.strip().lower().split() - for words in line: - if words.find(user_search_value): - total += 1 - print(total) - for l in lines: - words = l.split() # split each string in line - ans.append(words) # appending to final ans - print(len(lines)) - var = 'The word ' + str(user_search_value) + ' appears ' + str(total) + ' times with an average sentiment of ' + str(sentiments) - print (var) + if choice == '1': + sentiment() + elif choice == '2': + collection_average() + elif choice == '5': + print('Goodbye!') + sys.exit(0) + elif choice > '5' or not choice > '0': + raise "This input does not work!" except IOError: - print("Error: can\'t find file or read data") + print('That is not a valid input!') -pass if __name__ == '__main__': main()