Skip to main content

T9 Dictionary


T9's objective is to make it easier to type text messages. It allows words to be entered by a single keypress for each letter, as opposed to the multi-tap approach used in conventional mobile phone text entry, in which several letters are associated with each key, and selecting one letter often requires multiple keypresses.[Wikipedia]
It combines the groups of letters on each phone key with a fast-access dictionary of words. It looks up in the dictionary all words corresponding to the sequence of keypresses and orders them by frequency of use. As T9 gains familiarity with the words and phrases the user commonly uses, it speeds up the process by offering the most frequently used words first and then lets the user access other choices with one or more presses of a predefined Next key.[Wikipedia]





I have developed this code for T9 Dictionary, using VS 2010, User may enter any numeric code, and system will return most five words associated with this code.

#include
#include
#include
#include
#include

using namespace std;

string WordsArr[5];

typedef struct Node
{
     int count;
     int number;
     char *Words[5];

     struct Node *left;
     struct Node *right;
}TreeNode;
typedef struct TNode
{
     char Number;
     bool treminate;
     struct TNode *one;
     struct TNode *two;
     struct TNode *three;
     struct TNode *four;
     struct TNode *five;
     struct TNode *six;
     struct TNode *seven;
     struct TNode *eight;
     struct TNode *nine;
}TriesNode;

void CreateTree(TreeNode **root)
{
     *root=NULL;
}
bool CheckDupli(TreeNode *temp,string str)
{
     for(int i=0;icount;i++)
     {
           if(strcmp(temp->Words[i],str.c_str())==0)
                return false;
     }

     return true;
}
void Tree_insert(int num,string str,TreeNode **root)
{
     TreeNode *temp=*root;
     if(*root==NULL)
     {
           TreeNode *temp1=(TreeNode*)malloc(sizeof(TreeNode));
           temp1->count=1;
           temp1->number=num;
          
           //temp1->word1=new char[str.length()];
           //strcpy(temp1->word1,str.c_str());

           temp1->Words[0]=new char[str.length()];
           strcpy(temp1->Words[0],str.c_str());

           temp1->left=NULL;
           temp1->right=NULL;
           *root=temp1;

     }
     else if(temp->number==num)
     {
           if(temp->count<5 o:p="">
           {
                     if(CheckDupli(temp,str))
                     {
                           temp->Words[temp->count]=new char[str.length()];
                           strcpy(temp->Words[temp->count],str.c_str());
                           temp->count++;
                     }

           }//if temp1->count<5 span="">
     }//if temp->number==num
     else if(temp->number>num)
     {
           if(temp->left==NULL)
           {
                TreeNode *NewNode=(TreeNode*)malloc(sizeof(TreeNode));
                NewNode->count=1;
                NewNode->number=num;
                NewNode->Words[0]=new char[str.length()];
                strcpy(NewNode->Words[0],str.c_str());
                NewNode->left=NULL;
                NewNode->right=NULL;
                temp->left=NewNode;
           }
           else
           {
           Tree_insert(num,str,&temp->left);
           }
     }
     else if(temp->number
     {
           if(temp->right==NULL)
           {
                TreeNode *NewNode=(TreeNode*)malloc(sizeof(TreeNode));
                NewNode->count=1;
                NewNode->number=num;
                NewNode->Words[0]=new char[str.length()];
                strcpy(NewNode->Words[0],str.c_str());
                NewNode->left=NULL;
                NewNode->right=NULL;
                temp->right=NewNode;
           }
           else
           {
           Tree_insert(num,str,&temp->right);
           }
     }
}

int GenerateCode(string str)
{
     int Code=0;
     int len=str.length();
     for(int i=0;i
     {
           if(str[i]>=65 && str[i]<90 o:p="">
           {
                str[i]=str[i]+32;
           }

           if(str[i]>='a' && str[i]<='c')
           {
                Code*=10;
                Code+=2;
           }
           else if(str[i]>='d' && str[i]<='f')
           {
                Code*=10;
                Code+=3;
           }
           else if(str[i]>='g' && str[i]<='i')
           {
                Code*=10;
                Code+=4;
           }
           else if(str[i]>='j' && str[i]<='l')
           {
                Code*=10;
                Code+=5;
           }
           else if(str[i]>='m' && str[i]<='o')
           {
                Code*=10;
                Code+=6;
           }
           else if(str[i]>='p' && str[i]<='s')
           {
                Code*=10;
                Code+=7;
           }
           else if(str[i]>='t' && str[i]<='v')
           {
                Code*=10;
                Code+=8;
           }
           else if(str[i]>='w' && str[i]<='z')
           {
                Code*=10;
                Code+=9;
           }
     }//for

     return Code;
}
int Search_IN_BST(int num,TreeNode *root)
{
     TreeNode *temp=root;
     if(root==NULL)
     {
           return 0;
     }
     else if(temp->number==num)
     {
           for(int i=0;icount;i++)
           {
                WordsArr[i]=temp->Words[i];
           }
          
           return temp->count;
     }
     else if(temp->number>num)
     {
           Search_IN_BST(num,temp->left);
     }
     else if(temp->number
     {
           Search_IN_BST(num,temp->right);
     }
}
void CreateTries(TriesNode **root)
{
     *root=NULL;    
}
TriesNode* CreateNewNode(int num)
{
           TriesNode *t=(TriesNode*)malloc(sizeof(TriesNode));
           t->Number=num;
           t->treminate=false;

           t->one=NULL;
           t->two=NULL;
           t->three=NULL;
           t->four=NULL;
           t->five=NULL;
           t->six=NULL;
           t->seven=NULL;
           t->eight=NULL;
           t->nine=NULL;
           return t;
}
void Insert_IN_Tries(string str,TriesNode **root)
{
     if(*root==NULL)
     {
           TriesNode *t1=CreateNewNode('+');
           t1->treminate=false;
           *root=t1;
     }

     TriesNode *temp=*root;
     int len=str.length();
     for(int i=0;i
     {
           if(str[i]=='1')
           {
                if(temp->one==NULL)
                {
                     TriesNode *t=CreateNewNode('1');
                     temp->one=t;
                     temp=temp->one;
                }
                else
                {
                     temp=temp->one;
                }
           }
           else if(str[i]=='2')
           {
                if(temp->two==NULL)
                {
                     TriesNode *t=CreateNewNode('2');
                     temp->two=t;
                     temp=temp->two;
                }
                else
                {
                     temp=temp->two;
                }
           }
           else if(str[i]=='3')
           {
                if(temp->three==NULL)
                {
                     TriesNode *t=CreateNewNode('3');
                     temp->three=t;
                     temp=temp->three;
                }
                else
                {
                     temp=temp->three;
                }
           }
           else if(str[i]=='4')
           {
                if(temp->four==NULL)
                {
                     TriesNode *t=CreateNewNode('4');
                     temp->four=t;
                     temp=temp->four;
                }
                else
                {
                     temp=temp->four;
                }
           }
           else if(str[i]=='5')
           {
                if(temp->five==NULL)
                {
                     TriesNode *t=CreateNewNode('5');
                     temp->five=t;
                     temp=temp->five;
                }
                else
                {
                     temp=temp->five;
                }
           }
           else if(str[i]=='6')
           {
                if(temp->six==NULL)
                {
                     TriesNode *t=CreateNewNode('6');
                     temp->six=t;
                     temp=temp->six;
                }
                else
                {
                     temp=temp->six;
                }
           }
           if(str[i]=='7')
           {
                if(temp->seven==NULL)
                {
                     TriesNode *t=CreateNewNode('7');
                     temp->seven=t;
                     temp=temp->seven;
                }
                else
                {
                     temp=temp->seven;
                }
           }
           else if(str[i]=='8')
           {
                if(temp->eight==NULL)
                {
                     TriesNode *t=CreateNewNode('8');
                     temp->eight=t;
                     temp=temp->eight;
                }
                else
                {
                     temp=temp->eight;
                }
           }
           else if(str[i]=='9')
           {
                if(temp->nine==NULL)
                {
                     TriesNode *t=CreateNewNode('9');
                     temp->nine=t;
                     temp=temp->nine;
                }
                else
                {
                     temp=temp->nine;
                }
           }

     }//for

     temp->treminate=true;

}
bool Search_In_Tries(string str,TriesNode *root)
{
     TriesNode *temp=root;
     bool notfound=true;

     int len=str.length();
     for(int i=0;i
     {
           if(str[i]=='1')
           {
                if(temp->one!=NULL)
                {
                     temp=temp->one;
                }
                else
                {
                     notfound=false;
                     break;
                }
           }
           else if(str[i]=='2')
           {
                if(temp->two!=NULL)
                {
                     temp=temp->two;
                }
                else
                {
                     notfound=false;
                     break;
                }
           }
           else if(str[i]=='3')
           {
                if(temp->three!=NULL)
                {
                     temp=temp->three;
                }
                else
                {
                     notfound=false;
                     break;
                }
           }
           else if(str[i]=='4')
           {
                if(temp->four!=NULL)
                {
                     temp=temp->four;
                }
                else
                {
                     notfound=false;
                     break;
                }
           }
           else if(str[i]=='5')
           {
                if(temp->five!=NULL)
                {
                     temp=temp->five;
                }
                else
                {
                     notfound=false;
                     break;
                }
           }
           else if(str[i]=='6')
           {
                if(temp->six!=NULL)
                {
                     temp=temp->six;
                }
                else
                {
                     notfound=false;
                     break;
                }
           }
           else if(str[i]=='7')
           {
                if(temp->seven!=NULL)
                {
                     temp=temp->seven;
                }
                else
                {
                     notfound=false;
                     break;
                }
           }
           else if(str[i]=='8')
           {
                if(temp->eight!=NULL)
                {
                     temp=temp->eight;
                }
                else
                {
                     notfound=false;
                     break;
                }
           }
           else if(str[i]=='9')
           {
                if(temp->nine!=NULL)
                {
                     temp=temp->nine;
                }
                else
                {
                     notfound=false;
                     break;
                }
           }



           }//for

           if(temp->treminate==true && notfound==true)
           {
                return true;
           }
           else
           {
                return false;

           }
}

void showProgress(int Persentage)
{
     system("cls");
     cout<<"Generating Codes:"<"%";
}
    
void main()
{

     TreeNode *Tree;
     CreateTree(&Tree);

     TriesNode *Tries;
     CreateTries(&Tries);

     ///////////Read file/////////////////
     ifstream ifile;
     ifile.open("f:\\WordsDic.txt", ios::in);


     cout<<"Generating Code...";
     string input="";
     int noOfWord=0;
    
     int Per=25;
     int inter=14527;
     if(ifile)
     {
           while(!ifile.eof())
           {
                ifile>>input;
                int Code=GenerateCode(input);
                Tree_insert(Code,input,&Tree);
                char *as=new char;
                string S_Code=string(itoa(Code,as,10));
                Insert_IN_Tries(S_Code,&Tries);
                noOfWord++;

                if(noOfWord==inter)
                {
                     //Sleep(1000);
     //              showProgress(Per);
           //         inter*=2;
                //   Per+=Per;
                }
           }
     }
     else
     {
           cout<<"File Not Open";
           exit(0);
     }
     /////////////////////////////////////////////

     cout<<"Total Word in Dic:"<

     char Choice='y';
     do
     {
     string Num="";
     cout<<"\nEnter Number to Search:"; cin>>Num;
     bool Found=Search_In_Tries(Num,Tries);

     if(Found==true)
     {
           int Code=atoi(Num.c_str());;
           int count=Search_IN_BST(Code,Tree);

           cout<<":Related Words:\n";
           int x=1;
           for(int i=0,x=1;i
           {
                cout<":"<" "
;
           }
     }
     else
     {
           cout<<":No Related Word Found:\n";
     }
     cout<<"\nContinue Search? y/n:";
     cin>>Choice;
     }while(Choice=='y' || Choice=='Y');
    
     cout<<"\n";
}


I developed this code during my Degree; this code is based on iterative method, which is lengthy but easy to understand for beginners. You may optimized this code and developed recursive one and frequency based selection of words(Code return most frequent words). I know this code is not perfect but not that bad, it works fine for me.

Comments

Popular posts from this blog

Font identifier and Unicode converter for Hindi

Font identifier and Unicode converter for Hindi Fonts are used to represent text in document. Fonts are mainly two kind non-Unicode and Unicode fonts. Complex scripts like Hindi and other Asian languages well represented in Unicode fonts. There are some other ways to write these languages for e.g we can use ASCII/ISCII codes to represent different characters of Hindi, but there are large numbers of characters in Hindi script as compared to English. Therefore, we always need multiple ASCII/ISCII encoded characters combination to represent a single character of Hindi Script. One major problem in these ASCII encoding based fonts is that we cannot easily transfer text from one system to another. The system must have these text fonts. There is hundreds of ASCII/ISCII encoding based fonts which are used to write Hindi text. New software systems are based on Unicode fonts.                   ...

Binary Search Tree in ASP .Net

Binary Search Tree in ASP .Net To create Binary Search Tree(BST) in Asp.net application   first you need to create a Node class. Something like following : class Node {     public String data;     public int freq = 0;     public Node left, right;     public Node()     { }     public Node( String data)     {         this .data = data;         left = null ;         right = null ;     } } Next You need to create a class including different functions.Like class BinaryTreeImp {     Node root;     String outputfreq = "" ;     static int count = 0;     public BinaryTreeImp()     {      ...

Hindi to Punjabi Machine Translation System

The Hindi To Punjabi Machine Translation System has been developed using Direct/Rule based Approach by Dr.Vishal Goyal and Dr. G.S Lehal. Various large size Lexicon resources  have been used to map Source and Target language words.  In general, if the two languages are structurally similar, in particular as regards lexical correspondences, morphology and word order, the case for abstract syntactic analysis seems less convincing. Since the present research work deals with a pair of closely related language, so the direct translation system is the obvious choice. The overall system architecture shown below, is adopted for Hindi to Punjabi Machine Translation System. The system is divided into three stages: Preprocessing, Translation Engine, and Post Processing stage. Following is the description of various steps of this architecture.  PreProcessing   The pre-processing stage is a collection of operations that are applied on input  data to make it pr...