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]
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="">5>
{
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=""> 5>
}//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="">90>
{
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
Post a Comment