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.
- 21,682
- 3
- 59
- 88
-
No problem. If this solves your problem, you may consider upvoting / accepting the answer. – norok2 Jan 01 '20 at 17:07
-
-
-
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
-
unidecode module will do the trick. Try this:
from unidecode import unidecode
s = "αυτό είναι ένα παράδειγμα"
s = unidecode(s)
print(s)
Output:
auto einai ena paradeigma
- 113
- 2
- 14
-
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'
- 107,498
- 14
- 145
- 201