Google Translate

english-frenchHave you ever used Google Translate to translate some text or a full webpage?

For this challenge we are going to try to write a Python script to translate from English to French. However we are only going to translate numbers as follows:

Number English French
7 Seven Sept
24 Twenty-four Vingt-quatre
365 Three hundred and sixty-five Trois cent soixante-cinq
25,642 Twenty-five thousand six hundred and forty-two Vingt-cing mille six cent quarante-deux


From the examples given above we can see that the same rules are used in both English and French to write numbers in full. This means that we can write a simple algorithm here to translate word for word each of the words used from English to French. Note that even though this approach will be quite accurate to translate numbers written in full, it would be very inaccurate to translate full sentences. This is because the English grammar and the French grammar are different and though both languages have a lot of similarities they do not follow exactly the same rules. (Grammar and conjugation)

grammar-conjugation

For our translation algorithm we will focus on a word for word translation. To do so we need an English/French dictionary. We will use a text file containing all the words we need: One, Two, Three… Ten, Eleven, Twelve, … Thirty, Forty, Fifty, … Hundred, Thousand.


TextFiledictionary.txt

Finally we can write our algorithm which will:

  1. Ask the user to enter a number in full English,
  2. Open the dictionary.txt file in read mode,
  3. Loop through each line of the text file
    • Split and extract the data (English and French word)
    • Replace the English word with the French word in the number
  4. Close the text file
  5. Display the translated number on screen

Un, Deux, Trois…


Let’s test this code. You can use the numbers given in the table above to test this code:

Your Task:


Complete this code for the program to:

  1. Ask the user if they want to translate from English to French (Option 1) or from French to English (Option 2),
  2. Allow for the French to English translation using a similar approach as the one used to translate from English to French, using the same dictionary.txt file, without having to change it.
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 3.9 / 5. Vote count: 8

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: , ,