Translator - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Translator (/thread-8249.html) Pages:
1
2
|
Translator - Zatoichi - Feb-11-2018 def load_dictionary(filename): dictionary = {} with open('Hmong.txt', 'r') as input_file: for lines in input_file: info = lines.strip('\n').split(',') dictionary[info[2]] = info[1] return dictionary def user_input(): sentence = input('Type your English sentence:') return sentence def translate(sentence, dictionary): words = sentence.split() for word in words: if word in dictionary: print (dictionary[word], sep=' ', end=' ') else: print('?') # Unknown word def print_word_frequency(sentence): counters = {} words = sentence.split() for word in words: if word not in counters: counters [word] = 1 #First occurrence of a word starts counter else: counters [word] += 1 #if repeat word add to existing counter print ('Word' + ' ' + 'Frequency') for word, count in counters.items(): print (word, count) def main(): dictionary = load_dictionary('Hmong.txt') sentence = user_input() translate(sentence, dictionary) done = False while not done: print() another_translation = input('Another translation (Y/N):') if another_translation == 'Y' or another_translation == 'y': main() elif another_translation == 'N' or another_translation == 'n': print_word_frequency(sentence) done = True main()So I pretty much got my code finished but I have ran into a few issues. I need to integrate my counter in another way so it keeps track of all words typed not just the last one. Because I have been ending up with this. and also I can not for the life of me figure out how to align the numbers from the count under the F in frequency.
RE: Translator - Larz60+ - Feb-12-2018 Here's how to count and align: sentence = 'This is a random sentence, if a random sentence is what you want' slist = sentence.split() counts = {i:slist.count(i) for i in slist} for key, value in counts.items(): print('{:>10} {:>10}'.format(key, value))results: Or for left aligned text:sentence = 'This is a random sentence, if a random sentence is what you want' slist = sentence.split() counts = {i:slist.count(i) for i in slist} for key, value in counts.items(): print('{:<10} {:<10}'.format(key, value))
RE: Translator - Zatoichi - Feb-12-2018 will that counting method keep track of all words entered in each input though? and not just the last input the user did before they quit the program. RE: Translator - Larz60+ - Feb-12-2018 No, you have to accumulate RE: Translator - Zatoichi - Feb-12-2018 How do I go about doing that? Making a global counter? RE: Translator - Larz60+ - Feb-13-2018 You have to do it a bit differently if you want to accumulate: counts = {} def add_sentence(sentence): slist = sentence.split() for word in slist: word = word.strip() if word in counts: counts[word] += 1 else: counts[word] = 1 def print_counts(): for key, value in counts.items(): print('{:<10} {:<10}'.format(key, value)) print('\n-------------------\n') def main(): sentence1 = 'This is a random sentence, if a random sentence is what you want' sentence2 = 'This is another sentence like the first' add_sentence(sentence1) print_counts() add_sentence(sentence2) print_counts() main()result:
RE: Translator - Zatoichi - Feb-13-2018 That works okay if I know how many times the user will use the translation before quitting but if I have no idea it really does not work. I guess I will have to keep searching how to do it. RE: Translator - Larz60+ - Feb-13-2018 Please, you should be able to figure out how to do that. It's quite simple, all the work is done! RE: Translator - Larz60+ - Feb-13-2018 loop... while not quit input sentence call add_sentence ... call print_counts RE: Translator - Zatoichi - Feb-13-2018 Sorry, my fault for not realizing it sooner. Thanks! counts = {} def load_dictionary(filename): #load the dictionary text file English to Hmong dictionary = {} with open('Hmong.txt', 'r') as input_file: for lines in input_file: info = lines.strip('\n').split(',') dictionary[info[2]] = info[1] return dictionary def user_input(): #Get the user sentence in English that needs to be translated sentence = input('Type your English sentence:') return sentence def translate(sentence, dictionary): #take the user inputted sentence and split it into indvidual words and then translate each word words = sentence.split() for word in words: if word in dictionary: print (dictionary[word], sep=' ', end=' ') else: print('?',end=' ') # Unknown word def add_sentence(sentence): #take the user sentence and split and count each word words = sentence.split() for word in words: word = word.strip() if word in counts: counts[word] += 1 # if word already in count it adds another count to it else: counts[word] = 1 # if word not in count creates new count for the word def print_word_frequency(): #once user quits prints the words and frequency of the words print ('{:<10} {:<10}'.format ('Word','Frequency')) print('-------------------') for key, value in counts.items(): print('{:<10} {:<10}'.format(key, value)) def main(): #main function to call upon the other functions so they work together dictionary = load_dictionary('Hmong.txt') sentence = user_input() translate(sentence, dictionary) add_sentence(sentence) done = False while not done: print() another_translation = input('Another translation (Y/N):') if another_translation == 'Y' or another_translation == 'y': main() elif another_translation == 'N' or another_translation == 'n': done = True print_word_frequency() main()I do not suppose you could tell me why my code asks for another translation after saying not if I do it after more then one attempt. and when there is only one entry it quits as it should.
|