Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Including Files */
- #include <iostream>
- #include <conio.h>
- #include <string>
- #include <time.h>
- #include <math.h>
- #include <stdlib.h>
- #include <dos.h>
- #include <stdio.h>
- #include <fstream>
- /* Defining Costants*/
- // Max. length in characters of any automated reply ( Keyword only)
- # define MAX_RESP_LEN 100
- // Max. number of responses for any keyword
- # define MAX_RESP_NO 20
- // Max. number of keywords
- # define MAX_KEY 16
- // Max. number of Transpositions
- # define TRANSPOSE 12
- // Max. Size Of User Input
- # define MAX_USER_INPUT 100
- // Max. Length of Keyword
- # define MAX_KWD_LEN 20
- // Delay involved in typing (in ms)
- # define DELAY 20
- using namespace std;
- /* Defining Global Variables */
- // for getting the user name
- char user[30];
- // for word transpositions
- char wordin[TRANSPOSE][MAX_RESP_LEN];
- char wordout[TRANSPOSE][MAX_RESP_LEN];
- int RanNum(int max);
- void initialize_global();
- class progstr
- {
- public:
- char userip[MAX_USER_INPUT];
- char keyword[30];
- int keyfound;
- int keyno;
- int nullip;
- // constructor
- progstr()
- {
- keyno=-1;
- nullip=0;
- }
- }ip;
- class resp
- {
- int tot_resp;
- int last_resp;
- char replys[MAX_RESP_NO][MAX_RESP_LEN];
- char word[MAX_KWD_LEN];
- public:
- // constructor
- resp()
- {
- tot_resp=0;
- last_resp=-1;
- }
- int getcount()
- {
- return last_resp;
- }
- void addword(char str[MAX_KWD_LEN])
- {
- strcpy(word,str);
- }
- char * getword()
- {
- return word;
- }
- void addresp(char str[MAX_RESP_LEN])
- {
- strcpy(replys[++last_resp],str);
- }
- void display_resp(int num);
- void quit_display_resp(int num);
- };
- void display_logo()
- {
- cout <<" */*****. Version 0.1a "<< endl;
- cout <<" ,*( /*. ,. "<< endl;
- cout <<" ** *, "<< endl;
- cout <<" ** ,, "<< endl;
- cout <<" ,*,****//. **,*, ,, *** ,, .. / /**** **,*, "<< endl;
- cout <<" ,** *** /*.*.*/*/ ** ** /*(, ,.*/, ** "<< endl;
- cout <<" .//*# //.*#..**, /( .*. // *# // "<< endl;
- cout <<" ./*,, /* *. ,* ** /, *( ,* /* "<< endl;
- cout <<"**. */*# /, *// *, */.**,.,/*. *( ./, .*// "<< endl;
- cout <<" ./(((##* *((#(. # (. .* ***/ * */ *((#/. # "<< endl;
- }
- void display_line()
- {
- int width=80;
- int i=0;
- int x=1; //wherex(); HACKGEOS
- int y=1; //wherey()+1; HACKGEOS
- x=40;
- for(int k=0;k<40;k++)
- {
- //delay(DELAY);
- //gotoxy(x+k,y);
- cout<<(char)240;
- //gotoxy(x-k,y);
- //delay(DELAY);
- cout<<(char)240;
- }
- cout<<"\n";
- }
- void resp :: display_resp(int num)
- {
- cout<<"<Sakura> ";
- for(int i=0;i<strlen(replys[num]);i++)
- {
- // for deliberate typing errors
- if(RanNum(6)==0)
- { char c=RanNum(100);
- if(c=='\n' || c=='\b' || c==13)
- cout<<"w";
- else
- cout<<c;
- //delay(RanNum(DELAY));
- cout<<"\b";
- }
- if(replys[num][i]=='*')
- {
- char * s1=ip.userip+strlen(ip.keyword);
- short int flag=0;
- for(int m=0;m<TRANSPOSE;m++)
- {
- char * s2=wordin[m];
- char *ptr=NULL;
- ptr=strstr(s1,s2);
- if(ptr!=NULL)
- {
- // transposition word found in the
- // user input
- flag=1;
- // printing text before wordin[m]
- int times=ptr-s1;
- for(int i=0;i<times;i++)
- {
- //delay(DELAY);
- cout<<ip.userip[strlen(ip.keyword)+i];
- }
- // printing the wordout
- cout<<wordout[m];
- // printing the left overs
- char c;
- c=*(ptr+strlen(wordin[m]));
- int t=0;
- while(c!='\0')
- {
- cout<<*(ptr+strlen(wordin[m])+t);
- t++;
- c=*(ptr+strlen(wordin[m])+t);
- }
- }
- } // end of for
- // if flag is still zero , this means no transpose.
- if(0==flag)
- {
- char c;
- c=*(s1+strlen(ip.keyword));
- int t=0;
- while(c!='\0')
- {
- cout<<*(s1+t);
- t++;
- c=*(s1+t);
- }
- } // end of if
- break;
- }
- else
- {
- cout<<replys[num][i];
- //delay(RanNum(DELAY));
- }
- } // end of for
- cout<<"\n"<<user<<" > ";
- }
- void resp :: quit_display_resp(int num)
- {
- cout<<"<Sakura> ";
- for(int i=0;i<strlen(replys[num]);i++)
- {
- // for deliberate typing errors
- if(RanNum(6)==0)
- { char c=RanNum(100);
- if(c=='\n' || c=='\b' || c==13)
- cout<<"w";
- else
- cout<<c;
- //delay(RanNum(DELAY));
- cout<<"\b";
- }
- cout<<replys[num][i];
- //delay(RanNum(DELAY));
- } // end of for
- }
- resp keys[MAX_KEY];
- int RanNum(int max)
- {
- if (0==max) max=1;
- srand ( time(NULL) );
- //randomize();
- return rand() % max;
- }
- void find_keyword()
- {
- int len=0;
- int lenkey=0;
- int key_no=0;
- char teststr[50];
- while((ip.keyfound==0) &&(key_no!=MAX_KEY))
- {
- // getting the length of the keyword
- lenkey=strlen(keys[key_no].getword());
- char *ptr=NULL;
- ptr=strstr(ip.userip,keys[key_no].getword());
- if (ptr!=NULL)
- {
- // keyword found !
- ip.keyfound=1;
- ip.keyno=key_no;
- strcpy(ip.keyword,keys[key_no].getword());
- break;
- }
- /*//////////////////////////////////////////
- for(int i=0;i<lenkey;i++)
- {
- teststr[i]=ip.userip[i];
- }
- teststr[i]='\0';
- if (strcmpi(teststr,keys[key_no].getword())==0)
- {
- ip.keyfound=1;
- ip.keyno=key_no;
- strcpy(ip.keyword,keys[key_no].getword());
- }
- //////////////////////////////////////////*/
- key_no++;
- }
- }
- void read_from_file()
- {
- ifstream fin;
- int index=-1;
- fin.open("eliza.dat");
- char line[MAX_RESP_LEN];
- while(fin)
- {
- fin.getline(line,MAX_RESP_LEN);
- char *ptr=NULL;
- ptr=(char *)strstr("@KWD@",line);
- if(strlen(line)<1)
- {
- break;
- }
- else if(ptr!=NULL)
- {
- // the next line is a keyword
- fin.getline(line,MAX_RESP_LEN);
- keys[++index].addword(line);
- }
- else
- {
- // it is a response
- keys[index].addresp(line);
- }
- } // end of while
- } // end of function
- //delay(int x)
- //{
- // for (int i=0; i<x * 1000; i++);
- //
- //}
- void main()
- {
- //clrscr();
- display_line();
- display_logo();
- display_line();
- // for initializing the global variables
- initialize_global();
- // for no response by the user.
- resp null_resp;
- null_resp.addresp("Wait, what?");
- null_resp.addresp("Come again?");
- null_resp.addresp("I can't hold a conversation with you if you don't say anything. :<");
- // upon logging in
- resp signon;
- signon.addresp("Hello! My name is Sakura. Pleased to meet you!");
- signon.addresp("So tell me, how are you today?");
- signon.addresp("Namaste! I've always wanted a chance to say that.");
- // when no key found
- resp no_key;
- no_key.addresp("I'm not quite sure if I follow.");
- no_key.addresp("I think you lost me. :3c");
- no_key.addresp("I see.");
- no_key.addresp("What does that even mean?");
- no_key.addresp("Would you be able to clarify that for me, please? ;A;");
- no_key.addresp("Intriguing...");
- resp bye;
- bye.addresp("Goodbye! Enjoy the rest of your day. :3");
- bye.addresp("I hope I'll see you again soon!");
- bye.addresp("Thanks for spending time with me today!");
- // reading data from dictionary
- read_from_file();
- // STARTING CONVERSATION WITH THE USER
- // welcoming the user
- cout<<"<Sakura> Hi! What's your name?\n";
- cin>>user;
- signon.display_resp(RanNum(signon.getcount()));
- fflush(stdin);
- gets(ip.userip);
- strcpy(ip.userip,strupr(ip.userip));
- while(strcmpi(ip.userip,"bye")!=0)
- {
- find_keyword();
- if(strlen(ip.userip)<1)
- {
- null_resp.display_resp(RanNum(null_resp.getcount()));
- }
- else if(ip.keyfound==1)
- {
- keys[ip.keyno].display_resp(RanNum(keys[ip.keyno].getcount()));
- }
- else
- {
- no_key.display_resp(RanNum(no_key.getcount()));
- }
- // again returning to normal values of the data items
- strcpy(ip.userip," ");
- ip.keyno=-1;
- ip.keyfound=0;
- fflush(stdin);
- gets(ip.userip);
- strcpy(ip.userip,strupr(ip.userip));
- } // end of while
- bye.quit_display_resp(RanNum(null_resp.getcount()));
- cout<<endl;
- display_line();
- cout<<"I tried to make the code work with lowercase letters. I hope it doesn't break.";
- display_line();
- getch();
- }
- void initialize_global()
- {
- strcpy(wordin[0],"are");
- strcpy(wordout[0],"am");
- strcpy(wordin[1],"am");
- strcpy(wordout[1],"are");
- strcpy(wordin[2],"were");
- strcpy(wordout[2],"was");
- strcpy(wordin[3],"was");
- strcpy(wordout[3],"were");
- strcpy(wordin[4],"you");
- strcpy(wordout[4],"me");
- strcpy(wordin[5]," I ");
- strcpy(wordout[5],"you");
- strcpy(wordin[6],"your");
- strcpy(wordout[6],"my");
- strcpy(wordin[7],"my");
- strcpy(wordout[7],"your");
- strcpy(wordin[8],"I've");
- strcpy(wordout[8],"you've");
- strcpy(wordin[9],"you've");
- strcpy(wordout[9],"I've");
- strcpy(wordin[10],"I'm");
- strcpy(wordout[10],"you're");
- strcpy(wordin[11],"you're");
- strcpy(wordout[11],"I'm");
- strcpy(wordin[12],"me");
- strcpy(wordout[12],"you");
- strcpy(wordin[13],"you");
- strcpy(wordout[13],"me");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement