2011-05-15 42 views
0

如何修復程序以準備與SWI Prolog一起使用?錯誤信息,無法正常工作 - 序言

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

我修改read_stringwrite_stringreadwrite

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 

是可以刪除空白arguement _G907的價值?>,併成爲唯一的一句話?

+0

只是爲了澄清,如果您轉換爲SWI prolog,請在問題中明確說明併爲其添加標籤。 :) – Bart 2011-05-15 21:13:05

+0

@Bart:已經完成:) – Tom 2011-05-15 21:17:51

回答

0

您在此特定示例中的錯誤是由您的輸入引起的。您的輸入與您knowledge_base中的第一個條目相匹配。由於您的任何輸入都不匹配,所以返回的列表長度爲0,導致調用隨機的問題,這似乎需要至少爲1的值。

嘗試「您的姓名是什麼」作爲輸入例如,看看是否有效。

+0

1? - chatterbot2。 >'你的名字是什麼'。 你可以打電話給我CHATTERBOT2。 所以它的工作原理,嗯,它可能產生隨機答案沒有這樣的事情:錯誤:隨機/ 1:域錯誤:'not_less_than_one'預計,發現'0'例外:(7)select_response([],_G485)? creep – Tom 2011-05-15 21:12:45

+0

如果你得到這個錯誤,這意味着在某個點隨機獲得了一個值0作爲輸入。因此,檢查爲什麼會發生,並儘量避免它。無論如何,在這種情況下,它更多的是這個例子的侷限性。您只能輸入在您的knowledge_base中明確定義的條目。如果你不這樣做,你會得到錯誤。 – Bart 2011-05-15 21:15:25

+0

當改變爲另一個值,例如。 666 - >錯誤消失,但仍然有一件壞事 - 看看: >'狗是黑的'。 我不確定我是否瞭解您正在談論的內容._G421 來自knowledge_base的問題正常運作。 – Tom 2011-05-15 21:22:15

相關問題