Question: Need Python help don't know how I keep getting ??????? as the answer to translating English to Hmong. Attached is my output This is my

Need Python help don't know how I keep getting ??????? as the answer to translating English to Hmong. Attached is my output

This is my code

#import string package import string

#Implement the method to load load_dictionary(file) #to load the Hmong dictionary text file #contains line number, Hmong word and #respective translated word def load_dictionary(filename): #Open the filename file=open(filename) #create the empty dict word_dict=dict() #use for-loop to iterate each line for line in file: #split line by comma fields=line.strip().split(',') #ensuring the length of result list if len(fields)==3: #extracting value at index 1 as hmong word, #index2 as english word hmong_list=fields[1].lower() english_list=fields[2].lower() #add the word to word_dict, with english word being #key an hmong word being value word_dict[english_list]=hmong_list #close the file file.close() #return the new word_dict object return word_dict

#Implement the method to translate a sentence def translate(sentence): #loading dictionary from a file called Homng_dictionary.txt list_words=load_dictionary('Homng_dictionary.txt') #Initialze the variable to store translated text translated='' #use for loop through each word in sentence for word in sentence.split(): #if word is in dict, append the respective #hmong word to be translated if word in list_words: translated+=list_words[word]+' ' #If word not found in the textfie #then append a ? symbol else: translated+='? ' #returning translated text, #after removing trailing white space return translated.strip()

#method to print word frequency def print_word_frequency(text): #declare a dictionary freq_words=dict() #looping and counting frequency of each word in text for word in text.split(): #if word already in counts, adding 1 to count, #else adding with 1 as count if word in freq_words: freq_words[word]+=1 #set the freq_words to 1 else: freq_words[word]=1 #display header word and frequency print('{:10s} {:10s}'.format('Word', 'Frequency')) print('---------------------') #use for loop to print the word and frequency for word in freq_words: #print the word and frequency print('{:10s} {:10d}'.format(word,freq_words[word]))

#Implement the main method def main(): #declare flag value as true flag=True #variable to store all text entered by user converted_text='' #looping as long as loop is True while flag: #prompt and read the sentence sentence=input("Type your English sentence: ") #converting text to lower sentence = sentence.lower() #looping and removing all puntuation symbols from text for i in string.punctuation: #replace the puntucaltion as empty ' ' sentence = sentence.replace(i,' ') #translating it, printing translation translated=translate(sentence) #print the translated text print('Hmong: ',translated) #append to text converted_text+=sentence+' ' #set the flag value to True if input is y or Y, #else False flag=input("Another translation (Y/N): ").lower()=='y' #at the end, print the word frequency print_word_frequency(converted_text)

#call the main() main() Need Python help don't know how I keep getting ??????? as theanswer to translating English to Hmong. Attached is my output This is

Hmong_dictionary.txt raws lias nws.bis. was that nws.he yog.was rau,for on on yog.are nrog.with lawy.they yuay be ntawmat ib.one muaj have no.this los ntawm from los ntawm by. kub.hot lo lus.word tab sis.but dab tsi.what ib co.some yog.is nws.it koj.you los yog.gr muai.had tus the ntawm of mus rauto thiab and ib tuga 1354 baracters in 00 linge 15973baracters in 26A linge salarted *Python 3.8.1 Shell Python 3.8.1 (v3.8.1: 16293b6006, Dec 18 2019, 14:08:53) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license()" for more information. >>> - RESTART: /Users/jadeturgeon/Documents/Intro to Computer Science/untitled folde r/week 5/program 5.py Type your English sentence: I can help you help him Hmong: ? ? ? ? ? ? Another translation (Y/N)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!