错误消息,无法正常工作 - prolog

时间:2011-05-15 20:52:53

标签: prolog

如何修复程序以准备与SWI Prolog一起使用?

链接到来源:http://ai-programming.com/prolog_bot_tutorial.htm chatterbot2:

我将read_stringwrite_string修改为readwrite

    knowledge_base([
    ['WHAT IS YOUR NAME', 
    ['MY NAME IS CHATTERBOT2.',
     'YOU CAN CALL ME CHATTERBOT2.',
     'WHY DO YOU WANT TO KNOW MY NAME?']
    ],

    ['HI', 
    ['HI THERE!',
     'HOW ARE YOU?',
     'HI!']
    ],

    ['HOW ARE YOU',
    ['I''M DOING FINE!',
    'I''M DOING WELL AND YOU?',
    'WHY DO YOU WANT TO KNOW HOW AM I DOING?']
    ],

    ['WHO ARE YOU',
    ['I''M AN A.I PROGRAM.',
     'I THINK THAT YOU KNOW WHO I''M.',
     'WHY ARE YOU ASKING?']
    ],

    ['ARE YOU INTELLIGENT',
    ['YES,OFCORSE.',
     'WHAT DO YOU THINK?',
     'ACTUALY,I''M VERY INTELLIGENT!']
    ],

    ['ARE YOU REAL',
    ['DOES THAT QUESTION REALLY MATERS TO YOU?',
     'WHAT DO YOU MEAN BY THAT?',
     'I''M AS REAL AS I CAN BE.']
     ] ]).

select(0, [H|_], H).
select(N, [_|T], L) :- N > 0, N1 is N - 1, select(N1, T, L).

list_length([], 0).
list_length([_|T], Length):- list_length(T, Length2), Length is Length2 + 1.

quit_session(X):- X = bye, 
    nl, write('IT WAS NICE TALKING TO YOU USER, SEE YOU NEXT TIME!').

no_response_found(ListOfResponse):-
    list_length(ListOfResponse, NumOfResponse),
    NumOfResponse == 0,
    write('I''M NOT SURE IF I  UNDERSTAND WHAT YOU  ARE TALKING ABOUT.'), !.

no_response_found(_).

get_keyword(KeyList, [KeyList,_]).
get_response(RespList, [_, RespList]).

select_response(RespList, Response):-
    list_length(RespList, NumOfResponse),
    IndexOfResponse is integer(random(NumOfResponse)),
    select(IndexOfResponse, RespList, Response), !.

select_response(_, _).

find_match(Input, [FirstRecord|RestDatabase], ListOfResponse):-
    get_keyword(Keyword, FirstRecord),
    Keyword == Input, get_response(ListOfResponse, FirstRecord), !;
    find_match(Input, RestDatabase, ListOfResponse).

find_match(_, [_], _).

chatterbot2:- 
    repeat,
    nl, write('>'),
    read(Input),
    knowledge_base(ListOfRecord),
    find_match(Input, ListOfRecord, ListOfResponse),
    no_response_found(ListOfResponse),
    select_response(ListOfResponse, Response),
    write(Response), nl,
    quit_session(Input).

当我尝试使用它时,我得到:

鐀1 ?- chatterbot2.

>hi.
I'M NOT SURE IF I  UNDERSTAND WHAT YOU  ARE TALKING ABOUT.
ERROR: random/1: Domain error: `not_less_than_one' expected, found `0'
   Exception: (7) select_response([], _G492) ? creep
2 ?- chatterbot2.

>'What do you do ?'.
I'M NOT SURE IF I  UNDERSTAND WHAT YOU  ARE TALKING ABOUT.
ERROR: random/1: Domain error: `not_less_than_one' expected, found `0'
   Exception: (7) select_response([], _G485) ? creep
3 ?- chatterbot2.

>'Dog is black'.
I'M NOT SURE IF I  UNDERSTAND WHAT YOU  ARE TALKING ABOUT.
ERROR: random/1: Domain error: `not_less_than_one' expected, found `0'
   Exception: (7) select_response([], _G485) ? creep
4 ?- 

编辑: 随机值= / = 0:

1 ?- chatterbot2.

>'NOT IN BASE'.
I'M NOT SURE WHAT ARE YOU TALKING ABOUT._G907

是否可以删除空白争议的值 _G907 ?>并成为唯一的句子?

1 个答案:

答案 0 :(得分:0)

此特定示例中的错误是由您的输入引起的。您的输入与knowledge_base中的第一个条目匹配。由于没有任何输入匹配,返回的列表长度为0,导致调用random的问题,似乎需要至少为1的值。

尝试'你的名字是什么'作为输入,看看是否有效。