Question

This program is in python and thanks fro whoever help me. In this program, you will...

This program is in python and thanks fro whoever help me.

In this program, you will build an English to Hmong translator program. Hmong is a language widely spoken by most Southeast Asian living in the twin cities.

The program lets the user type in a sentence in English and then translate it to a Hmong sentence. The program does not care about grammar or punctuation marks. That means your program should remove punctuation marks from the English words before attempt to translate them. Make sure it is case insensitive. You are given a file containing tuples of entry number, English word, and Hmong word.

If the dictionary does not contain the English word, simply put a question mark (?) as the translation.

Your program should consists of the following functions:

load_dictionary(filename)

Read the file containing the tuples, insert the entries into the dictionary and return the dictionary object.

translate(sentence)

Take a sentence in English and return a sentence in English.

print_word_frequency()

Print the frequency of English words that were translated.

main

The main logic loop.

The main loop:

The program prompts the user for an English sentence. It translate each word in Hmong and print the Hmong words in a sentence. If an English word does not exist in the dictionary, it uses ? as the Hmong translation of that word. Each time the program finishes translating, it asks the user if they want to continue. If not, it displays the English words and the number of time the words have been entered into the program. See sample run below.

Sample run:

Type your English sentence: I can help you help him.

Hmong: kuv tau pab koj pab nws

Another translation (Y/N): Y

Type your English sentence: You can help me.

Hmong: koj tau pab kuv.

Another translation (Y/N): N

Word       Frequency
-----------------------
i 1
can       2
you       2
help       2
him        1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi. I have answered this same question before.

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required (except if you want to change input file name)

#code

import string

#method to load hmong dictionary from file
def load_dictionary(filename):
    #opening file
    file=open(filename)
    #creating empty dict
    data=dict()
    #looping through each line
    for line in file:
        #splitting 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=fields[1].lower()
            eng=fields[2].lower()
            #adding to dict, with eng word being key an hmong word being value
            data[eng]=hmong
    #closing file
    file.close()
    #returning data
    return data

#helper method to format a text
def format(text):
    #converting text to lower
    text = text.lower()
    #looping and removing all puntuation symbols from text
    for i in string.punctuation:
        text = text.replace(i, ' ')
    #returning formatted text
    return text

#method to translate a sentence
def translate(sentence, data):
    #variable to store translated text
    translated=''
    #looping through each word in sentence
    for word in sentence.split():
        #if word is in dict, appending corresponding hmong word to translated
        if word in data:
            translated+=data[word]+' '
        else:
            #else, appending a ? symbol
            translated+='? '
    #returning translated text, after removing trailing white space
    return translated.strip()


#method to print word frequency
def print_word_frequency(text):
    counts=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 counts:
            counts[word]+=1
        else:
            counts[word]=1
    #displaying heading
    print('{:10s} {:10s}'.format('Word', 'Frequency'))
    print('---------------------')
    #looping and printing each word and count
    for word in counts:
        print('{:10s} {:10d}'.format(word,counts[word]))


#main method
def main():
    #loading dictionary from a file called dictionary.txt, make sure that the file name
    #is correct, or else, use correct file name as in your system
    data=load_dictionary('dictionary.txt')
    loop=True #loop controller
    text='' #variable to store all text entered by user
    #looping as long as loop is True
    while loop:
        #reading sentence, formatting it
        sentence=input("Type your English sentence: ")
        sentence=format(sentence)
        #translating it, printing translation
        translated=translate(sentence,data)
        print('Hmong:',translated)
        #appending to text
        text+=sentence+' '
        #setting loop to True if input is y or Y, else False
        loop=input("Another translation (Y/N): ").lower()=='y'
    #at the end, printing word frequency
    print_word_frequency(text)

#calling main()
main()

#input file name format

#output

Add a comment
Know the answer?
Add Answer to:
This program is in python and thanks fro whoever help me. In this program, you will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please complete the following: Write a Python script to do the following: 1. Ask the use...

    Please complete the following: Write a Python script to do the following: 1. Ask the use to enter several sentences, one at a time in a loop). To end the sentence entry, the user enters a blank (empty) sentence. 2. Extract each word from each sentence and put it in a list. This will require at least one loop to go through each sentence in the list. It is up to you how you want to get the words in...

  • In this lab you will write a spell check program. The program has two input files:...

    In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the document to be spellchecked. The program will read in the words for the dictionary, then will read the document and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is or type in a replacement word and add...

  • Write a program IN PYTHON that checks the spelling of all words in a file. It...

    Write a program IN PYTHON that checks the spelling of all words in a file. It should read each word of a file and check whether it is contained in a word list. A word list available below, called words.txt. The program should print out all words that it cannot find in the word list. Requirements Your program should implement the follow functions: main() The main function should prompt the user for a path to the dictionary file and a...

  • Dictionary.java DictionaryInterface.java Spell.java SpellCheck.java In this lab you will write a spell check program. The program...

    Dictionary.java DictionaryInterface.java Spell.java SpellCheck.java In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the input file to be spell checked. The program will read in the words for the dictionary, then will read the input file and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is, add...

  • In Java please Only use methods in the purpose. Thank you The purpose of this assignment is to help you learn Java iden...

    In Java please Only use methods in the purpose. Thank you The purpose of this assignment is to help you learn Java identifiers, assignments, input/output nested if and if/else statements, switch statements and non-nested loops. Purpose Question 2-String variables/Selection & loops. (8.5 points) Write a complete Java program which prompts the user for a sentence on one line where each word is separated by one space, reads the line into one String variable using nextline), converts the string into Ubbi...

  • Write a structured (procedural) Python program that solves the following spec: Soundex System Coding: Soundex is...

    Write a structured (procedural) Python program that solves the following spec: Soundex System Coding: Soundex is a system that encodes a word into a letter followed by three numbers that roughly describe how the word sounds. Therefore, similar sounding words have the same four-character code. Use the following set of (slightly modified #4) rules to create a translator from English words to Soundex Code: Retain the first letter of the word. For letters 2 …n, delete any/all occurrences of the...

  • All the white space among words in a text file was lost. Write a C++ program...

    All the white space among words in a text file was lost. Write a C++ program which using dynamic programming to get all of the possible original text files (i.e. with white spaces between words) and rank them in order of likelihood with the best possible runtime. You have a text file of dictionary words and the popularity class of the word (words are listed from popularity 1-100 (being most popular words), 101-200, etc) - Input is a text file...

  • Python program This assignment requires you to write a single large program. I have broken it...

    Python program This assignment requires you to write a single large program. I have broken it into two parts below as a suggestion for how to approach writing the code. Please turn in one program file. Sentiment Analysis is a Big Data problem which seeks to determine the general attitude of a writer given some text they have written. For instance, we would like to have a program that could look at the text "The film was a breath of...

  • Design and code a SWING GUI to translate text entered in English into Pig Latin. You...

    Design and code a SWING GUI to translate text entered in English into Pig Latin. You can assume that the sentence contains no punctuation. The rules for Pig Latin are as follows: For words that begin with consonants, move the leading consonant to the end of the word and add “ay”. Thus, “ball” becomes “allbay”; “button” becomes “uttonbay”; and so forth. For words that begin with vowels, add “way’ to the end of the word. Thus, “all” becomes “allway”; “one”...

  • Help c++ Description: This program is part 1 of a larger program. Eventually, it will be...

    Help c++ Description: This program is part 1 of a larger program. Eventually, it will be a complete Hangman game. For this part, the program will Prompt the user for a game number, Read a specific word from a file, Loop through and display each stage of the hangman character I recommend using a counter while loop and letting the counter be the number of wrong guesses. This will help you prepare for next week Print the final messages of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT