products/sources/formale Sprachen/C/Eliza image not shown  

Quellcode-Bibliothek

© Kompilation durch diese Firma

[Weder Korrektheit noch Funktionsfähigkeit der Software werden zugesichert.]

Datei: Eliza.c   Sprache: C

//------------------------------------------------------------------------------
// aus
// http://www.sourcecodesworld.com/articles/How-to-build-Eliza-Chatterbot.asp
//
//------------------------------------------------------------------------------


class progstr
{
public:
char userip[MAX_USER_INPUT];
char keyword[30];
int keyfound;
int keyno;
int nullip;
    // constructor
    progstr()
    {
        keyno=-1;
        nullip=0;
        keyfound=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);
    }

    // defined later
    void display_resp(int num);
    void quit_display_resp(int num);

};


//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
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=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


//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
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");
}


//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void resp :: display_resp(int num)
{
cout<<"ELIZA > ";
for(int i=0;i<strlen(replys[num]);i++)
{
     // for deliberate typing errors 
     // (for simulating the human behavoir ;-)

     if(RanNum(6)==0)
     { 
     char c=RanNum(100);
     if(c=='\n' || c=='\b' || c==13)
          cout<<"w";
     else
          cout<<c;
     delay(RanNum(DELAY));
     // correcting the deliberate typing error
     cout<<"\b";
}


// * is used to write anything after the // keyword
// as it is, but with some transformatio // ns like 
// converting MY to YOUR.< // br> 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 need for 
     // transposing any word.
     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

// giving the prompt back to user
cout<<"\n"<<user<<" > ";
}

{
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;
}
key_no++;
}

}

¤ Dauer der Verarbeitung: 0.0 Sekunden  (vorverarbeitet)  ¤





Download des
Quellennavigators
Download des
sprechenden Kalenders

in der Quellcodebibliothek suchen




Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.


Bemerkung:

Die farbliche Syntaxdarstellung ist noch experimentell.


Bot Zugriff