대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '

대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '\0' )

{

  *p++ = '\0';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '\0';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '\0' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '\0';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

 

대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '\0' )

{

  *p++ = '\0';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '\0';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '\0' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '\0';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

' )

{

  *p++ = '

대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '\0' )

{

  *p++ = '\0';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '\0';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '\0' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '\0';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

 

대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '\0' )

{

  *p++ = '\0';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '\0';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '\0' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '\0';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '

대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '\0' )

{

  *p++ = '\0';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '\0';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '\0' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '\0';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

 

대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '\0' )

{

  *p++ = '\0';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '\0';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '\0' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '\0';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '

대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '\0' )

{

  *p++ = '\0';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '\0';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '\0' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '\0';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

 

대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '\0' )

{

  *p++ = '\0';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '\0';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '\0' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '\0';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '

대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '\0' )

{

  *p++ = '\0';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '\0';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '\0' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '\0';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

 

대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '\0' )

{

  *p++ = '\0';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '\0';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '\0' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '\0';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

 

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '

대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '\0' )

{

  *p++ = '\0';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '\0';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '\0' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '\0';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

 

대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '\0' )

{

  *p++ = '\0';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '\0';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '\0' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '\0';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

' )

{

  *p++ = '

대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '\0' )

{

  *p++ = '\0';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '\0';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '\0' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '\0';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

 

대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '\0' )

{

  *p++ = '\0';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '\0';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '\0' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '\0';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '

대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '\0' )

{

  *p++ = '\0';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '\0';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '\0' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '\0';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

 

대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '\0' )

{

  *p++ = '\0';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '\0';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '\0' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '\0';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '

대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '\0' )

{

  *p++ = '\0';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '\0';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '\0' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '\0';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

 

대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '\0' )

{

  *p++ = '\0';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '\0';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '\0' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '\0';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '

대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '\0' )

{

  *p++ = '\0';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '\0';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '\0' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '\0';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

 

대화 프로그램 - ELIZA

 

튜링 테스트란 1950년 앨런 튜링(Alan Turing)이 고안해낸 것으로

지능적인 시스템인지, 즉 인공지능을 가지고 있는지 시험할 수 있는 기준이라 할 수 있습니다.

컴퓨터에게 질문을 하여 대화상대가 사람인지 기계인지 구분할 수 없으면 시험을 통과한 것으로 판정하고,

기계라고 판단할 수 있으면 실패한 것으로 판정하는 방법입니다. 

 

1966년 MIT의 교수인 조셉 웨즌바움(Joseph Weizenbaum)이

환자와 대화하면서 심리치료사의 역할을 수행할 수 있는 프로그램인 'ELIZA'를 개발하였는데,

실제로 메사츄세스 종합병원에서 사람들을 대상으로 시험한 결과

다수가 자신이 대화한 상대가 컴퓨터라는 것을 알지 못했다고 합니다.

 

하지만 이는 실제 인공지능이라기 보다 단순히 키워드을 비교하여

미리 저장되어 있는 대화를 출력하는 속임수라 할 수 있습니다

 

 

 

여기서는 이러한 ELIZA를 C코드로 구현한 프로그램을 살펴보도록 하겠습니다

'C 인공지능 프로그래밍, Herbert Schildt, 신경숙 역, 도서출판 세웅'에 나온 프로그램입니다.

 

먼저 가장 기본적인 방법은 사용자가 입력한 문장에서 키워드들을 뽑아

미리 저장되어 있는 답변과 비교하여 일치하는 문장을 출력하는 것입니다. 

trans[] 배열이 키워드와 그에 맞는 답변을 저장하고 있습니다.

 

두 번째 방법은 사용자가 입력한 문장을 저장하고 있다가

적절한 순간에 이전의 주제를 꺼내 화제를 돌리도록 합니다. 

topics[][] 큐에 사용자의 입력문장을 저장하고 있다가 

사용자의 입력문장이 너무 짧은 경우 이전의 문장을 다시 출력하여 줍니다.

 

 

 

여기서 사용된 키워드 매칭 방법은

일반적인 자연어처리(Natural Language Processing)와 전혀 관계가 없습니다.

 

자연어처리 기법은 먼저 문장을 파싱(parsing)하여

각 형태소별로 구분하는 구문분석(syntax analysis)를 하고

그 다음 각 형태소와 단어의 뜻을 분석하여 문장의 의미를 파악하는

의미분석(semantic analysis)를 해야합니다.

 

그리고 이전의 문장들을 저장해놓고 현재의 문장과의 연결과

현재 화제 등을 파악하는 담화분석(discourse analysis) 부분이 필요합니다

 

그 밖에도 여러가지 복잡한 기법들이 필요한데

아직까지 구문분석은 거의 완벽하게 되지만

나머지 기법들은 성능이 떨어져서 더 많은 연구가 필요하다고 하더군요.

 

 

 

다음은 ELIZA와의 대화 일부입니다.

 

How are you this beautiful day?

: I don't know, I feel bad

 

How do you know that?

: I feel like I could kill

 

I was wrong to kill

: I never seem to win

 

Don't be negative - be positive!

: why, are you sure?

 

Remember, therapy is good for you.

: why, are you sure?

 

Stop repeating yourself.

: Hey, who cares jerk

 

Don't ever call me a jerk!

: I'll call you a jerk any time I want to

 

Let's not talk about me.

: I am unhappy

 

Why are you unhappy?

 

 

 

---------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

 

 

#define MAX   100  // 최대 토픽의 개수

#define SHORT  10

#define VERYSHORT 3

 

 

// 함수 선언

void respond(char *s);

void get_token(void);

int is_in(char c, char *s);

int lookup(char *token);

void assert_topic(char *t);

int find_topic(char *t);

int in_topics(char *s);

 

 

// 일반적인 컴퓨터의 대답 자료

char *response[] = {

"How are you?",

"Did you have a happy childhood?",

"Did you hate your father?",

"i'm not sure I understand",

""

};

 

// 키워드에 맞는 컴퓨터의 대답 자료

char *trans[] = {

"you", "Let's not talk about me.",

"think", "Why do you think that?",

"hate", "So you hate something - tell me more.",

"what", "Why do you ask?",

"want", "Why do you want that?",

"need", "We all need many things - is this special?",

"why", "Remember, therapy is good for you.", 

"know", "How do you know that?",

"bye", "Your bill will be mailed to you.",

"murder", "I don't like killing",

"kill", "It is wrong to kill",

"jerk", "Don't ever call me a jerk!",

"can't", "Don't be negative - be positive.",

"failure", "Strive for success.",

"never", "Don't be negative - be positive.",

"unhappy", "Why are you unhappy?",

""

};

 

char topics[MAX][80];

char token[80];

char *p_pos;

int  res = 0;

int  head = 0;

int  tail = 0;

 

 

 

 

 

//**************************************************************************

// Name : main()

// Desc : 메인 함수

//**************************************************************************

void

main(void)

{

char s[80];

 

 

 

printf("%s\n", response[res++]);

 

// 사용자의 입력을 받고 컴퓨터의 반응을 반복함

do

{

  printf(" : ");

  

  p_pos = s;

  gets(s);

 

  respond(s);

 

} while( strcmp(s, "bye") );

}

 

 

 

 

 

//**************************************************************************

// Name : respond()

// Desc : 사용자의 입력에 따라 적절한 대답을 출력

//**************************************************************************

void

respond(char *s)

{

char t[80];

int  loc;

 

 

 

// 입력문장이 너무 짧을 경우 이전에 사용자가 했던 말로 화제를 돌림

if( strlen(s) < VERYSHORT && strcmp(s, "bye") )

{

  if( find_topic(t) )

  {

   printf("You just said : ");

   printf("%s \n", t);

   printf("Tell me more.\n");

  }

  else

  {

   if( !*response[res] ) res = 0;

 

   printf("%s\n", response[res++]);

  }

 

  return;

}

 

 

 

// 사용자가 같은 말을 반복했을 경우 화를 내는 대답을 출력

if( in_topics(s) )

{

  printf("Stop repeating yourself!\n");

 

  return;

}

 

 

 

// 사용자의 입력문장이 짧지 않다면 토픽 큐에 저장해둠

if( strlen(s) > SHORT ) assert_topic(s);

 

 

 

// 사용자의 입력문장의 키워드와 trans[] 배열과 비교하여 일치하는 대답을 출력

do

{

  get_token();

 

  loc = lookup(token);

 

  if( loc != -1 )

  {

   printf("%s\n", trans[loc+1]);

 

   return;

  }

} while( *token );

 

printf("Tell me more...\n");

}

 

 

 

 

 

//**************************************************************************

// Name : lookup()

// Desc : 키워드가 trans[] 배열에 있는지 검색

//**************************************************************************

int

lookup(char *token)

{

int t;

 

 

 

t = 0;

 

while( *trans[t] )

{

  if( !strcmp(trans[t], token) ) return t;

 

  t += 2;

}

 

 

 

return -1;

}

 

 

 

 

 

//**************************************************************************

// Name : assert_topic()

// Desc : 사용자의 입력을 토픽 큐에 저장

//**************************************************************************

void

assert_topic(char *t)

{

if( head == MAX ) head = 0;

 

strcpy(topics[head], t);

head++;

}

 

 

 

 

 

//**************************************************************************

// Name : find_topic()

// Desc : 토픽 큐에서 하나를 꺼냄

//**************************************************************************

int

find_topic(char *t)

{

if( tail != head )

{

  strcpy(t, topics[tail]);

  tail++;

 

  if( tail == MAX ) tail = 0;

 

  return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : in_topics()

// Desc : 사용자의 입력이 토픽 큐에 있는지 검색

//**************************************************************************

int

in_topics(char *s)

{

int t;

 

 

 

for( t = 0; t < MAX; t++ )

{

  if( !strcmp(s, topics[t]) ) return 1;

}

 

 

 

return 0;

}

 

 

 

 

 

//**************************************************************************

// Name : get_token()

// Desc : 사용자의 입력문장에서 키워드(토큰)을 뽑아냄

//**************************************************************************

void

get_token(void)

{

char *p;

 

 

 

p = token;

 

// 앞에 있는 빈칸을 건너뜀

while( *p_pos == ' ' ) p_pos++;

 

// NULL이 나오면 리턴

if( *p_pos == '\0' )

{

  *p++ = '\0';

 

  return;

}

 

// ",.!?"이 나오면 리턴

if( is_in(*p_pos, ",.!?") )

{

  *p = *p_pos;

  p++; p_pos++;

  *p = '\0';

 

  return;

}

 

 

 

while( *p_pos != ' ' && *p_pos != '\0' && !is_in(*p_pos, ".,?!") )

{

  *p = tolower(*p_pos++);

  p++;

}

 

*p = '\0';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

';

}

 

 

 

 

 

//**************************************************************************

// Name : is_in()

// Desc : 

//**************************************************************************

int

is_in(char c, char *s)

{

while( *s )

{

  if( c == *s ) return 1;

 

  s++;

}

 

 

 

return 0;

}

 

 

[출처] http://aidev.co.kr/chatbot/467

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.

댓글 0

목록
번호 제목 글쓴이 날짜 조회 수
28 [Linux programming][turbo c] The libXbgi Library file 졸리운_곰 2023.02.05 248
27 [unix][linux][compiler] Yacc와 Lex 시작하기 file 졸리운_곰 2021.09.08 261
26 [Visual Studio 2019] Visual Studio로 Linux 원격 개발하기(Ubuntu 설치부터 SSH 서버접속까지) file 졸리운_곰 2021.08.29 235
25 [unix][emacs] 이맥스와 함께하는 개발환경 . emacs 튜토리얼 file 졸리운_곰 2021.08.18 319
24 [Linux][C/C++] cmake 사용법과 활용 file 졸리운_곰 2020.11.08 552
23 [CMake] Linux C/C++ cmake 시작하기 졸리운_곰 2020.11.08 398
22 [C++] CMake Build System file 졸리운_곰 2020.11.08 328
21 Windows 10에 한국어 입출력이 가능한 리눅스 데스크톱 설치하기 file 졸리운_곰 2020.06.29 289
20 wxwidgets 과 codeblock 설치(리눅스) 졸리운_곰 2019.12.25 228
19 ubuntu 18.04.2 LTS 개발환경 세팅 졸리운_곰 2019.12.25 223
18 Ubuntu 16.04 개발환경 세팅하기! 졸리운_곰 2017.03.21 356
17 tcl/tk 자료 secret 졸리운_곰 2017.02.23 0
16 CentOS xrdp 설정 (원격데스크탑) 졸리운_곰 2017.02.01 453
15 Five lightweight Linux desktop worlds for extreme open-sourcers file 졸리운_곰 2016.05.12 1022
14 Logging every shell command 졸리운_곰 2016.05.11 581
13 How to keep a detailed audit trail of what’s being done on your Linux systems file 졸리운_곰 2016.05.11 467
12 Docker의 소개와 간단한 사용법 file 졸리운_곰 2015.11.24 804
11 Make Self-Extracting Archives with makeself.sh 졸리운_곰 2015.11.07 363
10 리눅스 간단 배포 Linux simple deploy 제작 : make self file 졸리운_곰 2015.11.07 352
9 Joinc: Linux 커널에서의 디바이스 드라이버 작성 졸리운_곰 2015.06.16 833
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED