Advertisement
Purianite

Eliza mod

Jan 20th, 2013
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.75 KB | None | 0 0
  1. /* Including Files */
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <string>
  5. #include <time.h>
  6. #include <math.h>
  7. #include <stdlib.h>
  8. #include <dos.h>
  9. #include <stdio.h>
  10. #include <fstream>
  11.  
  12. /* Defining Costants*/
  13. // Max. length in characters of any automated reply ( Keyword only)
  14. # define MAX_RESP_LEN 100
  15. // Max. number of responses for any keyword
  16. # define MAX_RESP_NO 20
  17. // Max. number of keywords
  18. # define MAX_KEY 16
  19. // Max. number of Transpositions
  20. # define TRANSPOSE 12
  21. // Max. Size Of User Input
  22. # define MAX_USER_INPUT 100
  23. // Max. Length of Keyword
  24. # define MAX_KWD_LEN 20
  25. // Delay involved in typing (in ms)
  26. # define DELAY 20
  27.  
  28. using namespace std;
  29.  
  30. /* Defining Global Variables */
  31.  
  32. // for getting the user name
  33. char user[30];
  34. // for word transpositions
  35. char wordin[TRANSPOSE][MAX_RESP_LEN];
  36. char wordout[TRANSPOSE][MAX_RESP_LEN];
  37. int RanNum(int max);
  38. void initialize_global();
  39.  
  40. class progstr
  41. {
  42. public:
  43. char userip[MAX_USER_INPUT];
  44. char keyword[30];
  45. int keyfound;
  46. int keyno;
  47. int nullip;
  48.     // constructor
  49.     progstr()
  50.     {
  51.     keyno=-1;
  52.     nullip=0;
  53.     }
  54. }ip;
  55.  
  56. class resp
  57. {
  58. int tot_resp;
  59. int last_resp;
  60. char replys[MAX_RESP_NO][MAX_RESP_LEN];
  61. char word[MAX_KWD_LEN];
  62. public:
  63.     // constructor
  64.     resp()
  65.     {
  66.     tot_resp=0;
  67.     last_resp=-1;
  68.     }
  69.     int getcount()
  70.     {
  71.     return last_resp;
  72.     }
  73.     void addword(char str[MAX_KWD_LEN])
  74.     {
  75.     strcpy(word,str);
  76.     }
  77.     char * getword()
  78.     {
  79.     return word;
  80.     }
  81.     void addresp(char  str[MAX_RESP_LEN])
  82.     {
  83.     strcpy(replys[++last_resp],str);
  84.     }
  85.     void display_resp(int num);
  86.     void quit_display_resp(int num);
  87.  
  88. };
  89. void display_logo()
  90. {
  91.     cout <<"   */*****.                          Version 0.1a              "<< endl;
  92.     cout <<" ,*(       /*.          ,.                                     "<< endl;
  93.     cout <<" **                     *,                                     "<< endl;
  94.     cout <<"  **                    ,,                                     "<< endl;
  95.     cout <<"   ,*,****//.     **,*, ,,    *** ,,     .. /  /****   **,*,   "<< endl;
  96.     cout <<"           ,**  ***   /*.*.*/*/   **     ** /*(,   ,.*/,   **  "<< endl;
  97.     cout <<"            .//*#     //.*#..**,  /(    .*. //      *#     //  "<< endl;
  98.     cout <<"           ./*,,      /* *.    ,* **    /,  *(     ,*      /*  "<< endl;
  99.     cout <<"**.      */*#  /,    *// *,     */.**,.,/*. *(     ./,   .*//  "<< endl;
  100.     cout <<"  ./(((##*      *((#(. # (.     .*  ***/  * */       *((#/. #  "<< endl;  
  101. }
  102. void display_line()
  103. {
  104. int width=80;
  105. int i=0;
  106. int x=1; //wherex(); HACKGEOS
  107. int y=1; //wherey()+1; HACKGEOS
  108. x=40;
  109. for(int k=0;k<40;k++)
  110. {
  111. //delay(DELAY);
  112. //gotoxy(x+k,y);
  113. cout<<(char)240;
  114. //gotoxy(x-k,y);
  115. //delay(DELAY);
  116. cout<<(char)240;
  117. }
  118. cout<<"\n";
  119. }
  120.  
  121. void resp :: display_resp(int num)
  122.     {
  123.     cout<<"<Sakura> ";
  124.     for(int i=0;i<strlen(replys[num]);i++)
  125.     {
  126.         // for deliberate typing errors
  127.         if(RanNum(6)==0)
  128.         {       char c=RanNum(100);
  129.             if(c=='\n' || c=='\b' || c==13)
  130.                 cout<<"w";
  131.             else
  132.                 cout<<c;
  133.             //delay(RanNum(DELAY));
  134.             cout<<"\b";
  135.         }
  136.  
  137.         if(replys[num][i]=='*')
  138.         {
  139.         char * s1=ip.userip+strlen(ip.keyword);
  140.         short int flag=0;
  141.         for(int m=0;m<TRANSPOSE;m++)
  142.             {
  143.             char * s2=wordin[m];
  144.             char *ptr=NULL;
  145.             ptr=strstr(s1,s2);
  146.  
  147.             if(ptr!=NULL)
  148.                 {
  149.                 // transposition word found in the
  150.                 // user input
  151.                 flag=1;
  152.  
  153.                 // printing text before wordin[m]
  154.                 int times=ptr-s1;
  155.                 for(int i=0;i<times;i++)
  156.                     {
  157.                     //delay(DELAY);
  158.                     cout<<ip.userip[strlen(ip.keyword)+i];
  159.                     }
  160.                 // printing the wordout
  161.                 cout<<wordout[m];
  162.                 // printing the left overs
  163.                 char c;
  164.                 c=*(ptr+strlen(wordin[m]));
  165.                 int t=0;
  166.                 while(c!='\0')
  167.                     {
  168.                     cout<<*(ptr+strlen(wordin[m])+t);
  169.                     t++;
  170.                     c=*(ptr+strlen(wordin[m])+t);
  171.                     }
  172.                 }
  173.             } // end of for
  174.             // if flag is still zero , this means no transpose.
  175.             if(0==flag)
  176.             {
  177.             char c;
  178.             c=*(s1+strlen(ip.keyword));
  179.             int t=0;
  180.             while(c!='\0')
  181.                 {
  182.                 cout<<*(s1+t);
  183.                 t++;
  184.                 c=*(s1+t);
  185.                 }
  186.             } // end of if
  187.             break;
  188.         }
  189.     else
  190.             {
  191.                 cout<<replys[num][i];
  192.                 //delay(RanNum(DELAY));
  193.             }
  194.     } // end of for
  195.     cout<<"\n"<<user<<" > ";
  196.     }
  197. void resp :: quit_display_resp(int num)
  198. {
  199.     cout<<"<Sakura> ";
  200.     for(int i=0;i<strlen(replys[num]);i++)
  201.     {
  202.         // for deliberate typing errors
  203.         if(RanNum(6)==0)
  204.         {       char c=RanNum(100);
  205.             if(c=='\n' || c=='\b' || c==13)
  206.                 cout<<"w";
  207.             else
  208.                 cout<<c;
  209.             //delay(RanNum(DELAY));
  210.             cout<<"\b";
  211.         }
  212.         cout<<replys[num][i];
  213.         //delay(RanNum(DELAY));
  214.     } // end of for
  215. }
  216. resp keys[MAX_KEY];
  217. int RanNum(int max)
  218. {
  219.     if (0==max) max=1;
  220. srand ( time(NULL) );
  221. //randomize();
  222. return rand() % max;
  223. }
  224. void find_keyword()
  225. {
  226. int len=0;
  227. int lenkey=0;
  228. int key_no=0;
  229. char teststr[50];
  230. while((ip.keyfound==0) &&(key_no!=MAX_KEY))
  231. {
  232. // getting the length of the keyword
  233. lenkey=strlen(keys[key_no].getword());
  234.  
  235. char *ptr=NULL;
  236. ptr=strstr(ip.userip,keys[key_no].getword());
  237. if (ptr!=NULL)
  238.     {
  239.     // keyword found !
  240.     ip.keyfound=1;
  241.     ip.keyno=key_no;
  242.     strcpy(ip.keyword,keys[key_no].getword());
  243.     break;
  244.     }
  245. /*//////////////////////////////////////////
  246. for(int i=0;i<lenkey;i++)
  247.     {
  248.     teststr[i]=ip.userip[i];
  249.     }
  250. teststr[i]='\0';
  251. if (strcmpi(teststr,keys[key_no].getword())==0)
  252.     {
  253.          ip.keyfound=1;
  254.          ip.keyno=key_no;
  255.          strcpy(ip.keyword,keys[key_no].getword());
  256.     }
  257. //////////////////////////////////////////*/
  258. key_no++;
  259. }
  260. }
  261.  
  262. void read_from_file()
  263. {
  264. ifstream fin;
  265. int index=-1;
  266. fin.open("eliza.dat");
  267. char line[MAX_RESP_LEN];
  268. while(fin)
  269. {
  270.     fin.getline(line,MAX_RESP_LEN);
  271.     char *ptr=NULL;
  272.     ptr=(char *)strstr("@KWD@",line);
  273.     if(strlen(line)<1)
  274.     {
  275.         break;
  276.     }
  277.     else if(ptr!=NULL)
  278.     {
  279.         // the next line is a keyword
  280.         fin.getline(line,MAX_RESP_LEN);
  281.         keys[++index].addword(line);
  282.     }
  283.     else
  284.     {
  285.         // it is a response
  286.         keys[index].addresp(line);
  287.     }
  288.  
  289. } // end of while
  290. } // end of function
  291.  
  292. //delay(int x)
  293. //{
  294. //  for (int i=0; i<x * 1000; i++);
  295. //
  296. //}
  297.  
  298. void main()
  299. {
  300. //clrscr();
  301.  
  302. display_line();
  303. display_logo();
  304. display_line();
  305. // for initializing the global variables
  306. initialize_global();
  307.  
  308. // for no response by the user.
  309. resp null_resp;
  310. null_resp.addresp("Wait, what?");
  311. null_resp.addresp("Come again?");
  312. null_resp.addresp("I can't hold a conversation with you if you don't say anything. :<");
  313.  
  314. // upon  logging in
  315. resp signon;
  316. signon.addresp("Hello! My name is Sakura. Pleased to meet you!");
  317. signon.addresp("So tell me, how are you today?");
  318. signon.addresp("Namaste! I've always wanted a chance to say that.");
  319.  
  320. // when no key found
  321. resp no_key;
  322. no_key.addresp("I'm not quite sure if I follow.");
  323. no_key.addresp("I think you lost me. :3c");
  324. no_key.addresp("I see.");
  325. no_key.addresp("What does that even mean?");
  326. no_key.addresp("Would you be able to clarify that for me, please? ;A;");
  327. no_key.addresp("Intriguing...");
  328.  
  329. resp bye;
  330. bye.addresp("Goodbye! Enjoy the rest of your day. :3");
  331. bye.addresp("I hope I'll see you again soon!");
  332. bye.addresp("Thanks for spending time with me today!");
  333. // reading data from dictionary
  334. read_from_file();
  335.  
  336. // STARTING CONVERSATION WITH THE USER
  337. // welcoming the user
  338.  
  339. cout<<"<Sakura> Hi! What's your name?\n";
  340. cin>>user;
  341. signon.display_resp(RanNum(signon.getcount()));
  342. fflush(stdin);
  343. gets(ip.userip);
  344. strcpy(ip.userip,strupr(ip.userip));
  345. while(strcmpi(ip.userip,"bye")!=0)
  346. {
  347.     find_keyword();
  348. if(strlen(ip.userip)<1)
  349. {
  350.     null_resp.display_resp(RanNum(null_resp.getcount()));
  351. }
  352. else if(ip.keyfound==1)
  353.     {
  354.     keys[ip.keyno].display_resp(RanNum(keys[ip.keyno].getcount()));
  355.     }
  356. else
  357.     {
  358.     no_key.display_resp(RanNum(no_key.getcount()));
  359.     }
  360. // again returning to normal values of the data items
  361. strcpy(ip.userip," ");
  362. ip.keyno=-1;
  363. ip.keyfound=0;
  364. fflush(stdin);
  365. gets(ip.userip);
  366. strcpy(ip.userip,strupr(ip.userip));
  367. } // end of while
  368.     bye.quit_display_resp(RanNum(null_resp.getcount()));
  369.     cout<<endl;
  370.     display_line();
  371. cout<<"I tried to make the code work with lowercase letters. I hope it doesn't break.";
  372.     display_line();
  373. getch();
  374. }
  375.  
  376.  
  377. void initialize_global()
  378. {
  379. strcpy(wordin[0],"are");
  380. strcpy(wordout[0],"am");
  381.  
  382. strcpy(wordin[1],"am");
  383. strcpy(wordout[1],"are");
  384.  
  385. strcpy(wordin[2],"were");
  386. strcpy(wordout[2],"was");
  387.  
  388. strcpy(wordin[3],"was");
  389. strcpy(wordout[3],"were");
  390.  
  391. strcpy(wordin[4],"you");
  392. strcpy(wordout[4],"me");
  393.  
  394. strcpy(wordin[5]," I ");
  395. strcpy(wordout[5],"you");
  396.  
  397. strcpy(wordin[6],"your");
  398. strcpy(wordout[6],"my");
  399.  
  400. strcpy(wordin[7],"my");
  401. strcpy(wordout[7],"your");
  402.  
  403. strcpy(wordin[8],"I've");
  404. strcpy(wordout[8],"you've");
  405.  
  406. strcpy(wordin[9],"you've");
  407. strcpy(wordout[9],"I've");
  408.  
  409. strcpy(wordin[10],"I'm");
  410. strcpy(wordout[10],"you're");
  411.  
  412. strcpy(wordin[11],"you're");
  413. strcpy(wordout[11],"I'm");
  414.  
  415. strcpy(wordin[12],"me");
  416. strcpy(wordout[12],"you");
  417.  
  418. strcpy(wordin[13],"you");
  419. strcpy(wordout[13],"me");
  420.  
  421. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement