Have 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:
|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)
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.
Finally we can write our algorithm which will:
- Ask the user to enter a number in full English,
- Open the dictionary.txt file in read mode,
- 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
Un, Deux, Trois…
Let’s test this code. You can use the numbers given in the table above to test this code:
Complete this code for the program to:
- Ask the user if they want to translate from English to French (Option 1) or from French to English (Option 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.