The program is meant to read a text file containing tuples that have a key (integers 1-1000), and an associated english and hmong word. It then creates a dictionary.
User then enters an english sentence and the dictionary returns the translation in hmong. If the entry contains words not in the text file, it returns a question mark.
It then asks if they want to enter another sentence, if they say no then the program will return a list that prints the frequency of each english word that the user entered during the program.
I have the general script done but the logic is just not working and I'm unsure why. When I have try to enter an english sentence the program returns Hmong: ?
even though the words are in the text file, but when I enter the same sentence into the Another translation (Y/N):
field, it then accepts the sentence but won't print the translation and instead just prints the frequency of the words.
import string
def load_dictionary(filename):
file=open(filename)
word_dict=dict()
for line in file:
fields=line.strip().split(',')
if len(fields)==3:
hmong_list=fields[1].lower()
english_list=fields[2].lower()
word_dict[english_list]=hmong_list
file.close()
return word_dict
def translate(sentence):
list_words=load_dictionary('C:\\Users\\Bridget\\Downloads\\HmongWords.txt')
translated=''
for word in sentence.split():
if word in list_words:
translated+=list_words[word]+' '
else:
translated+='? '
return translated.strip()
def print_word_frequency(text):
freq_words=dict()
for word in text.split():
if word in freq_words:
freq_words[word]+=1
else:
freq_words[word]=1
print('{:10s} {:10s}'.format('Word', 'Frequency'))
for word in freq_words:
print('{:10s} {:10d}'.format(word,freq_words[word]))
def main():
flag=True
converted_text=''
while flag:
sentence=input("Type your English sentence: ")
sentence = sentence.lower()
for i in string.punctuation:
sentence = sentence.replace(i,' ')
translated=translate(sentence)
print('Hmong: ',translated)
converted_text+=sentence+' '
flag=input("Another translation (Y/N): ").lower()=='y'
print_word_frequency(converted_text)
main()
Sample run
Type your English sentence: I can help you help him.
Hmong: ?
Another translation (Y/N): Y
Type your English sentence: I can help you help him
Hmong: ?
Another translation (Y/N): I can help you help him
Word Frequency
Word Frequency
Word Frequency
Word Frequency
Word Frequency
Word Frequency
Word Frequency
Word Frequency
Word Frequency
Word Frequency
Word Frequency
Word Frequency
i 2
can 2
help 4
you 2
him 2
I also am not seeing why it prints word and frequency over and over instead of just once. Any guidance would be appreciated!
Your code contains 2 problems.
First. You are not reading from the file correctly. for line in file
is not the correct way, you need to do for line in file.readlines()
Second. You close the file after the first iteration of the loop
for line in file:
fields=line.strip().split(',')
if len(fields)==3:
hmong_list=fields[1].lower()
english_list=fields[2].lower()
word_dict[english_list]=hmong_list
file.close() # here
the file.close()
should be indented one less
for line in file:
fields=line.strip().split(',')
if len(fields)==3:
hmong_list=fields[1].lower()
english_list=fields[2].lower()
word_dict[english_list]=hmong_list
file.close() # should be here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.