From 489d8a48b44bc1635cd6b07b5919cec85cc53eb4 Mon Sep 17 00:00:00 2001 From: Christopher <chrislnk12@gmail.com> Date: Mon, 20 Feb 2017 09:29:32 -0600 Subject: [PATCH] Organized code and added comments --- sentiment.py | 70 ++++++++++++++++++++++++++++-------------------- testSentiment.py | 8 ++++++ 2 files changed, 49 insertions(+), 29 deletions(-) diff --git a/sentiment.py b/sentiment.py index 3ad84e8..63feaf3 100644 --- a/sentiment.py +++ b/sentiment.py @@ -62,16 +62,27 @@ def main(): '4: Sort words from a file into positive.txt and negative.txt\n' '5: Exit the program') selection = verify_input_for_selection() - if selection == 1: - search_for_numbers(rating_numbers) - elif selection == 2: - get_average_for_file(rating_numbers) - elif selection == 3: - get_highest_and_lowest_words(rating_numbers) - elif selection == 4: - sort_words_into_file(rating_numbers) - elif selection == 5: - end_program() + get_input(selection, rating_numbers) + + +def get_input(selection, rating_numbers): + output = '' + if selection == 1: + search = input('Enter a word to search for: ') + output = search_for_numbers(rating_numbers, search) + elif selection == 2: + search = input("Enter a file name to search for: ") + output = get_average_for_file(rating_numbers, search) + elif selection == 3: + search = input("Enter a file name to search for: ") + output = get_highest_and_lowest_words(rating_numbers, search) + elif selection == 4: + search = input("Enter a file name to search for: ") + sort_words_into_file(rating_numbers, search) + output = 'Files created and written to.' + elif selection == 5: + end_program() + print_sentence(output) # Verify proper input of an int 1-5 @@ -108,22 +119,20 @@ def get_amount(word, rating_numbers): # Selection 1: Get the score of a word -def search_for_numbers(rating_numbers): - search = input('Enter a word to search for: ') +def search_for_numbers(rating_numbers, search): amount = get_amount(search, rating_numbers) average = get_average(search, rating_numbers) - print('The word "' + str(search) + '" appears ' + str(amount) + ' times, with an average ' - 'sentiment of ' + str(average) + '.') + sentence = 'The word "' + str(search) + '" appears ' + str(amount) + \ + ' times, with an average sentiment of ' + str(average) + '.' + return sentence # Selection 2: Get the average score of words in a file (one word per line) -def get_average_for_file(rating_numbers): +def get_average_for_file(rating_numbers, search): valid_entry = False search_list = [] - search = '' while not valid_entry: try: - search = input("Enter a file name to search for: ") with open(search) as data: search_list = data.read().splitlines() valid_entry = True @@ -145,21 +154,21 @@ def get_average_for_file(rating_numbers): summary = "negative" else: summary = "neutral" - print("The overall sentiment of " + search + " is " + summary + ".") + output = ("The overall sentiment of " + search + " is " + summary + ".") + return output else: - print("None of the words in this file are documented, sorry!") - print() + output = "None of the words in this file are documented, sorry!" + return output # Selection 3: Find the highest/lowest scoring words in a file -def get_highest_and_lowest_words(rating_numbers): +def get_highest_and_lowest_words(rating_numbers, search): valid_entry = False search_list = [] highest_rating = '' lowest_rating = '' while not valid_entry: try: - search = input("Enter a file name to search for: ") with open(search) as data: search_list = data.read().splitlines() valid_entry = True @@ -183,20 +192,19 @@ def get_highest_and_lowest_words(rating_numbers): lowest_rating = min_max_list[-1] except IndexError: print("None of the words in this file are documented, sorry!") - print('The most positive word is "' + highest_rating + - '" with a score of ' + str(get_average(highest_rating, rating_numbers)) + '.') - print('The most negative word is "' + lowest_rating + - '" with a score of ' + str(get_average(lowest_rating, rating_numbers)) + ".") - print() + output = 'The most positive word is "' + highest_rating + \ + '" with a score of ' + str(get_average(highest_rating, rating_numbers)) + '.\n' + \ + 'The most negative word is "' + lowest_rating + \ + '" with a score of ' + str(get_average(lowest_rating, rating_numbers)) + ".\n" + return output # Selection 4: Sort words from a file into positive.txt and negative.txt -def sort_words_into_file(rating_numbers): +def sort_words_into_file(rating_numbers, search): valid_entry = False search_list = [] while not valid_entry: try: - search = input("Enter a file name to search for: ") with open(search) as data: search_list = data.read().splitlines() valid_entry = True @@ -218,6 +226,10 @@ def sort_words_into_file(rating_numbers): print() +def print_sentence(sentence): + print(sentence) + + # Selection 5: Exit the program def end_program(): print('Thank you, for using the sentiment program!') diff --git a/testSentiment.py b/testSentiment.py index 670160e..1699ae6 100644 --- a/testSentiment.py +++ b/testSentiment.py @@ -63,3 +63,11 @@ class Test(TestCase): expected = 0 actual = sentiment.get_amount('scramble', dict_2) self.assertEquals(expected, actual) + + def test_search_for_numbers_1(self): + dict_1 = dict() + dict_2 = dict() + sentiment.setup(dict_1, dict_2) + actual = sentiment.search_for_numbers(dict_2, 'arty') + expected = 'The word "arty" appears 4 times, with an average sentiment of 1.25.' + self.assertEquals(expected, actual) -- GitLab