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.
Output:
Type your English sentence:I can help
kuv tau pab
Another translation (Y/N):y
Type your English sentence:no can help
tsis muaj tau pab
Another translation (Y/N):n
Word Frequency
no 1
can 1
help 1
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.
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:
Output:
This 1
is 2
a 2
random 2
sentence, 1
if 1
sentence 1
what 1
you 1
want 1
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))
Output:
This 1
is 2
a 2
random 2
sentence, 1
if 1
sentence 1
what 1
you 1
want 1
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.
No, you have to accumulate
How do I go about doing that? Making a global counter?
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:
Output:
This 1
is 2
a 2
random 2
sentence, 1
if 1
sentence 1
what 1
you 1
want 1
-------------------
This 2
is 3
a 2
random 2
sentence, 1
if 1
sentence 2
what 1
you 1
want 1
another 1
like 1
the 1
first 1
-------------------
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.
Please, you should be able to figure out how to do that.
It's quite simple, all the work is done!
loop...
while not quit
input sentence
call add_sentence
...
call print_counts
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