대화 프로그램 - 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

목록
번호 제목 글쓴이 날짜 조회 수
38 꼭 필요한 리눅스 명령어, file 졸리운_곰 2019.10.15 411
37 [Ubuntu 18.04] 원격 데스크톱 file 졸리운_곰 2019.02.23 331
36 Tasksel – Easily and Quickly Install Group Softwares in Debian and Ubuntu file 졸리운_곰 2018.10.14 296
35 How to Use the wget Linux Command to Download Web Pages and Files : 리눅스 wget으로 사이트 다운로드(저장) 졸리운_곰 2018.09.12 472
34 우분투 16.04 원격 데스크탑 설정 ubuntu 16.04 xrdp file 졸리운_곰 2018.08.12 495
33 Connect To Ubuntu 16.04 / 17.10 / 18.04 Desktop Via Remote Desktop Connection (RDP) With Xrdp file 졸리운_곰 2018.06.22 254
32 ubuntu 18.04 원격데스크톱 xrdp 그놈접속 : Ubuntu 18.04: Connect to GNOME desktop environment via XRDP file 졸리운_곰 2018.05.13 1442
31 MSI 외장그래픽 노트북에 Ubuntu 설치 : How to Install Ubuntu 16.04 on MSI GE62 6QC 졸리운_곰 2018.05.06 499
30 Ubuntu 16.04에서 Docker 설치 졸리운_곰 2018.05.04 351
29 Docker: Docker image which runs XRDP on Ubuntu 16.04 desktop file 졸리운_곰 2018.05.03 387
28 [리눅스 GUI 하드디스크 상태 모니터링 툴] GSmartControl - A GUI Tool to Check HDD/SSD Health on Linux file 졸리운_곰 2018.01.16 346
27 ubuntu xfce4 rdp에서 MS Code나 Atom 에디터가 안뜰때 졸리운_곰 2017.10.14 251
26 Install Atom Text Editor in Ubuntu 16.04 (both 32&64bit) file 졸리운_곰 2017.10.14 305
25 우분투 서버 윈도우 원격접속(rdp) 설정 졸리운_곰 2017.10.14 431
24 우분투 서버 한글 설정 및 한글폰트 설치 졸리운_곰 2017.10.14 458
23 우분투(Ubuntu)에서 putty로 ssh 원격 접속 실패 해결 방법 file 졸리운_곰 2017.08.27 316
22 우분투 16.04 원격 데스크탑 설정 file 졸리운_곰 2017.07.08 559
21 Ubuntu에서 PATH 설정하기 졸리운_곰 2017.03.22 294
20 [Ubuntu] GUI를 통해 쉽고 빠르게 폴더 공유하기 file 졸리운_곰 2017.02.19 328
19 ubuntu 에서 compiz 화면루틴이 cpu rate 많이 (20%이상) 잡을 때 낮추는 법 졸리운_곰 2017.02.15 243
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED