Saturday, 26 March 2022

How to Create a Tamil Phonetic keyboard inputs in python?

My target: create a Phonetic Keyboard in the Tamil language, using dictionary key mapping. My struggle: How to replace my keys with values and set that value to my textbox. For Example: If I press "K" in textbox1, then my textbox1.text will change into the Tamil letter "க்", if I press "Ku" then textbox1.text will be replaced by the Tamil letter "கு",, if I press "kuu" then textbox1.text will be replaced by the Tamil letter "கூ" And then If I press "m" then the Tamil letter "ம்" will be added to the previous letter "கூ" and now textbox1.text becomes "கூம்"

import sys

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

tamil_dict = {"a":{'a':'அ','aa':'ஆ'},
              "k":{'k':'க்','ka':'க','kaa':'கா','ki':'கி','kii':'கீ','ku':'கு','kuu':'கூ'},
              "m":{'m':'ம்','ma':'ம','maa':'மா','mi':'மி','mii':'மீ','mu':'மு','muu':'மூ'},
              "i":{"i":"இ"},
              "e":"ஈ "}


class Keyboard_Dict(QWidget):

    def __init__(self):
        super().__init__()
        self.setWindowTitle("Tamil InPut ")

        self.tbox1 = QLineEdit()
        self.tbox1.setFont(QFont('Arial Unicode MS', 10, QFont.Bold))
        self.tbox1.textChanged.connect(self.func_textbox_textchanged)

        self.tbox2 = QLineEdit()
        self.tbox2.setFont(QFont('Arial Unicode MS', 10, QFont.Bold))

        self.vbox = QVBoxLayout()
        self.vbox.addWidget(self.tbox1)
        self.vbox.addWidget(self.tbox2)
        self.setLayout(self.vbox)

        self.process_txt_temp_letter = ""
        self.process_txt_temp_position = 0
        self.process_letter_found = False
        self.process_letter_temp = False
        self.processed_text =""
    def func_textbox_textchanged(self,txt):
        self.txt_len = len(self.tbox1.text())
        if self.txt_len >= 1:
            self.process_txt_position = self.txt_len-1
            self.process_text_letter = (txt[self.process_txt_position])


            if self.process_letter_found == False:
                if (txt[self.txt_len-1]) in tamil_dict:
                    self.process_letter_found = True
                    self.process_txt_temp_position = (self.txt_len-1)
                    self.process_txt_temp_letter = (txt[self.txt_len-1])
                    self.process_letter_temp = True

            if self.process_letter_temp == True :
                if (txt[self.process_txt_temp_position:]) in tamil_dict[self.process_txt_temp_letter]:
                    self.processed_text =  tamil_dict[self.process_txt_temp_letter][txt[self.process_txt_temp_position:]]
                    print(self.processed_text)
                    # print("jjjjjjjjj",tamil_dict[self.process_txt_temp_letter][txt[self.process_txt_temp_position:]])

                elif (txt[self.process_txt_temp_position:]) not in tamil_dict[self.process_txt_temp_letter]:
                    self.process_txt_temp_position = self.txt_len - 1
                    self.process_txt_temp_letter = (txt[self.process_txt_temp_position])

                    if self.process_txt_temp_letter not in tamil_dict:
                        self.process_letter_temp = False
                        self.process_letter_found = False
                    else:
                        self.processed_text = tamil_dict[self.process_txt_temp_letter][txt[self.process_txt_temp_position:]]
                        print(self.processed_text)
                        # print("ffffff", tamil_dict[self.process_txt_temp_letter][txt[self.process_txt_temp_position:]])
        self.tbox2.setText(self.processed_text)





def main():
    app = QApplication(sys.argv)

    mainscreen = Keyboard_Dict()
    app.setStyle("Fusion")
    mainscreen.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

update : 1 I Have a keys Maps as follows :

      *Main character/processed first character : "k"* 
        k     : "Apple"
        ka    : "Bannana"
        kaa   : "Citrus:
        ki    : "Orange"
        kii   : "Pine" 
        *Main character/processed first character : "M"* 
         m     : "is" 
         ma    : "more"
         maa   : "taste"
 *Main character/processed first character : "i"* 
         i     : "world"
         ii    : "earth"

tamil_dict ={"k":{"k":"apple","ka":"bannana","kaa":"citrus","ki":"orange","kii":"pine"},
             "m":{"m":"is","ma":"more","maa":"taste"},
             "i":{"i":"world","ii":"earth"}
            }


     

In my textbox.text, my first character is "A", not found in dictionary, so need not process that character and display as it is.

In my textbox.text, my second input word is "k", which is found in my dictionery and also "k" is the main Word. Now My processed word is "A"+"k", my textbox is replaced as follows, "A" + "Apple" = AApple.

In my textbox.text, my third charcter is "a", Now my processed word is "ka", its found in dict . so replaced with that equivalent value "Banana". Now My processed word is "A"+"ka", and now my textbox.text as follows : "A"+"banana" = Abanana.

In my textbox.text, my fourth charcter is "a", my processed word becomes "Kaa" its equvilient value is "citrus", now my processed word is "A"+"kaa" , its equvlient value is "A"+"citrus" = "Acitrus"

In my textbox.text my fifth input character is "m", now my processed word is "kaam",not found in dictionery. Now we split character "m" and check it in dict, whether its found or not, if Found, then we replace its equvilent value. Now My processed word is "A"+'kaa'+"m" and its vaule is "A"+"citurs"+"is", my textbox.text as follows "Acitrusis"

sixth input word is "a", now my processed character is "A"+"kaa"+"ma" equvilaent value is "A"+"citrus"+"more". Textbox.text become "Acitrusmore"

Now, if I press charcter "i" not found in "m" set. so we split "i" sepreately and check it in dict, if found, replace that value or leave as it is.

If 7th input character is " space bar" then process will end.

if my 8th input charcter is some english alphabet, once agin start process and replace equvilaent value and so on

update : 2 For more cleareance : enter image description here



from How to Create a Tamil Phonetic keyboard inputs in python?

No comments:

Post a Comment