Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Translator
#10
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
Reply


Messages In This Thread
Translator - by Zatoichi - Feb-11-2018, 09:13 PM
RE: Translator - by Larz60+ - Feb-12-2018, 12:27 AM
RE: Translator - by Zatoichi - Feb-12-2018, 12:41 AM
RE: Translator - by Larz60+ - Feb-12-2018, 12:44 AM
RE: Translator - by Zatoichi - Feb-12-2018, 12:52 AM
RE: Translator - by Larz60+ - Feb-13-2018, 01:27 AM
RE: Translator - by Zatoichi - Feb-13-2018, 02:16 AM
RE: Translator - by Larz60+ - Feb-13-2018, 02:22 AM
RE: Translator - by Larz60+ - Feb-13-2018, 02:27 AM
RE: Translator - by Zatoichi - Feb-13-2018, 03:04 AM
RE: Translator - by Larz60+ - Feb-13-2018, 05:18 AM
RE: Translator - by Zatoichi - Feb-14-2018, 03:34 AM
RE: Translator - by Larz60+ - Feb-14-2018, 04:56 AM
RE: Translator - by Zatoichi - Feb-15-2018, 04:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Alphabetic Telephone Number Translator teafshadow 4 5,141 Oct-20-2019, 02:56 PM
Last Post: perfringo
  Alphabetic Telephone Number Translator MEH012 4 22,206 Apr-25-2018, 08:47 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020