I would like some Python code that takes in a sentence which is in Greek and converts it into English characters. Can anyone please help.
-
Please, be more specific, what do you mean by "convert fro Greek into English _characters_"? – Stanislav Poslavsky Jan 01 '20 at 13:08
-
@StanislavPoslavsky i want to make a code in which i type a greek word or phrase and then it is converted into english characters e.g. 'γεια σου' ----> 'geia sou' – George Jan 01 '20 at 16:56
3 Answers
I am not sure at what level you want the conversion to happen, but if you want to use a character translation for which you have control over, you could use str.maketrans()
and str.translate()
methods.
The first method generate a translation table according to the specifications of its argument, where the characters of the first argument are replaced with characters from the second argument (and the third argument specifies which characters to delete).
Such a translation table is used with str.translate()
, e.g.:
greek_alphabet = 'ΑαΒβΓγΔδΕεΖζΗηΘθΙιΚκΛλΜμΝνΞξΟοΠπΡρΣσςΤτΥυΦφΧχΨψΩω'
latin_alphabet = 'AaBbGgDdEeZzHhJjIiKkLlMmNnXxOoPpRrSssTtUuFfQqYyWw'
greek2latin = str.maketrans(greek_alphabet, latin_alphabet)
print('αυτο ειναι ενα παραδειγμα'.translate(greek2latin))
# auto einai ena paradeigma
Note that while this gives you greater control over the substitutions, it does not provide you with implicit translation of similar characters. For example ά
and α
are two separate characters, and would require a separate entry in the translation table.

- 25,683
- 4
- 73
- 99
-
-
-
i also have another question and this one might be harder. So i am trying to make a app for my car radio in which i have a CD with many directories and files and they are in Greek characters so they don't change in English characters. So i am trying to make an app that goes through my directories on files and Checks if the letters are in latin or english. Please help if u can. thanks – George Jan 01 '20 at 18:08
-
To navigate thru folders and files, see this: https://stackoverflow.com/questions/10377998/how-can-i-iterate-over-files-in-a-given-directory. And to check if it contains non-latin (greek) characters, see this: https://stackoverflow.com/questions/3094498/how-can-i-check-if-a-python-unicode-string-contains-non-western-letters – Kalma Jan 01 '20 at 18:30
-
1
-
1A better mapping, closer to what is used in practice as greeklish (with the warning that there is no 1-to-1 replacement for the char 'ψ' which are phonetically replaced by 'ps'. Here I replace with 'c' which the corresponding key in the Greek keyboard): ```greek_alphabet = "ΑΆαάΒβΓγΔδΕΈεέΖζΗΉηήΘθΙΊιίϊΐΚκΛλΜμΝνΞξΟΌοόΠπΡρΣσςΤτΥΎυύΦφΧχΨψΩΏωώ"``` ```latin_alphabet = "AAaaBbGgDdEEeeZzHHhh88IIiiiiKkLlMmNnXxOOooPpRrSssTtYYyyFfXxCcWWww"``` – fekioh Feb 22 '23 at 10:22
-
1As mentioned toward the end, the `maketrans` approach is perfectible in its argument. I am not a Greek speaker myself, so I left this to the reader. As for the handling of `"Ψ"` and `"ψ"`, you could consider adding a few extra `str.replace()` calls, e.g. `str.maketrans(greek_alphabet, latin_alphabet).replace("Ψ", "Ps").replace("ψ", "ps")` – norok2 Feb 22 '23 at 10:39
-
I actually missed Ξ,ξ as well. The additional calls would then be `.replace("Ψ", "Ps").replace("ψ", "ps").replace("Ξ", "Ks").replace("ξ", "ks")`. And of course, the chars in question should be removed form the initial mapping: ```greek_alphabet = "ΑΆαάΒβΓγΔδΕΈεέΖζΗΉηήΘθΙΊιίϊΐΚκΛλΜμΝνΟΌοόΠπΡρΣσςΤτΥΎυύΦφΧχΩΏωώ" latin_alphabet = "AAaaBbGgDdEEeeZzHHhh88IIiiiiKkLlMmNnOOooPpRrSssTtYYyyFfXxWWww"``` – fekioh Feb 22 '23 at 12:15
unidecode module will do the trick. Try this:
from unidecode import unidecode
s = "αυτό είναι ένα παράδειγμα"
s = unidecode(s)
print(s)
Output:
auto einai ena paradeigma

- 111
- 2
- 15
-
And he should [install it via pip](https://pypi.org/project/Unidecode/) first. – Ekrem Dinçel Jan 01 '20 at 14:34
-
i also have another question and this one might be harder. So i am trying to make a app for my car radio in which i have a CD with many directories and files and they are in Greek characters so they don't change in English characters. So i am trying to make an app that goes through my directories on files and Checks if the letters are in latin or english. Please help if u can. thanks – George Jan 02 '20 at 08:56
Google translate has a module called googeltrans. For example
>>> from googletrans import Translator
>>> translator = Translator()
>>> output = translator.translate('αυτό είναι ένα παράδειγμα')
>>> output.text
'This is an example'

- 114,268
- 16
- 167
- 218
-
1
-
perhaps worth mentioning that this does not really answer the original question – norok2 Aug 10 '22 at 16:13