| Reference | Downloads | Github

Dictionary won't translate punctuation

I’m trying to create an experiment in the Hebrew language, which requires participants input words through the keyboard. In order to do that, I created a dictionary that translates English to Hebrew. However, while it works fine for letters, it cannot translate punctuation, regardless of the language.

Here is the dictionary I created:

eng_to_heb  = {'a' : u'ש', 'b' : u'נ', 'c' : u'ב', 'd' : u'ג',
    'e' : u'ק', 'f' : u'כ' , 'g' : u'ע' , 'h' : u'י', 'i' : u'ן', 
    'j' : u'ח', 'k' : u'ל', 'l' : u'ך', 'm' : u'צ' , 'n' : u'מ', 
    'o' : u'ם', 'p' : u'פ', 'q' : u'/', 'r' : u'ר', 's' : u'ד',
    't' : u'א', 'u' : u'ו',  'v': u'ה', 
    'w' : u"'", 'x' : u'ס', 'y' : u'ט',
    'z' : u'ז',  'slash' :  u".", "comma" : u"ת", u'ת':u'ת',
     "semicolon" : u'ף', 'period' : u'ץ' , 'aspostrophe' : u',',
    'space' : ' '}

And the code to input the words:

    if charsEntered != []:
        if event.getKeys(keyList = ["backspace"]):
    #get new chars entered
    newChars = event.getKeys(keyList = list(string.ascii_letters))

    if newChars:
        for each in newChars:

    #include new chars in list
    #charsEntered.extend([for i in newChars: eng_to_heb[i] ] )
    #create string out of list
    thirdWordString = "".join(charsEntered)[::-1]

Does anyone faced a similar problem? or have a solution?

Thanks in advance,

You should actually state exactly what the problem is rather than just “it cannot translate punctuation.”

But at a guess, there are a few issues with the code:

  • Firstly, only call event.getKeys() once, rather than twice to check for the backspace and then again for other keys: this can mean that keypresses will get lost.

  • This line of code ensures that no keypresses will be detected except for letters of the alphabet:

newChars = event.getKeys(keyList = list(string.ascii_letters))

Just newChars = event.getKeys() should suffice, or explicitly list the punctuation elements as well as letters of the alphabet.

  • Then when you are able to detect keys like 'space', code like this:
if newChars:
        for each in newChars:

would break it up into individual characters like 's', 'p', 'a' etc, which you won’t want.

Thank you very much for the help!