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.
Output:
Type your English sentence:I can help
kuv tau pab
Another translation (Y/N):I can help you help him
Another translation (Y/N):y
Type your English sentence:I can help you help him
kuv tau pab koj pab nws
Another translation (Y/N):n
Word Frequency
-------------------
I 2
can 2
help 3
you 1
him 1
Another translation (Y/N):
and when there is only one entry it quits as it should.
Output:
Type your English sentence:I can help you help him
kuv tau pab koj pab nws
Another translation (Y/N):n
Word Frequency
-------------------
I 1
can 1
help 2
you 1
him 1