Created
October 10, 2019 05:47
-
-
Save carrotflakes/368974d34ea1a07d227028b695b95b60 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 知識 | |
food("中華料理"). | |
food("フランス料理"). | |
food("イタリア料理"). | |
place("新宿"). | |
place("渋谷"). | |
place("池袋"). | |
restaurant("川香苑 本店", "中華料理", "新宿", 3000). | |
restaurant("上海小吃", "中華料理", "新宿", 5000). | |
find_restaurant(Food, Place, Budget, Restaurants) :- | |
findall(Name, ','(restaurant(Name, Food, Place, Price), Price =< Budget), Restaurants). | |
% エンコーダ/デコーダ | |
decode_rule(greeting(morning), "おはようございます"). | |
decode_rule(greeting(morning), "おはよう"). | |
decode_rule(greeting(afternoon), "こんにちは"). | |
decode_rule(greeting(evening), "こんばんは"). | |
decode_rule(thanks, "ありがとうございます"). | |
decode_rule(thanks, "ありがとう"). | |
decode_rule(accept(thanks), "どういたしまして"). | |
decode_rule(terminate, "終了"). | |
decode_rule(accept(terminate), "終了します"). | |
decode_rule(food(X), X) :- food(X). | |
decode_rule(food(X), Codes) :- food(X), append(X, "が食べたい", Codes). | |
decode_rule(food(X), Codes) :- food(X), append(X, "店を探して", Codes). | |
decode_rule(accept(food(X)), Codes) :- food(X), append(X, "店ですね", Codes). | |
decode_rule(ask(place), "場所はどの辺りですか?"). | |
decode_rule(place(X), X) :- place(X). | |
decode_rule(place(X), Codes) :- place(X), append(X, "です", Codes). | |
decode_rule(accept(place(X)), Codes) :- place(X), append(X, "ですね", Codes). | |
decode_rule(ask(budget), "予算の上限はいくらですか?"). | |
decode_rule(budget(X), Codes) :- nonvar(Codes), append(NumberCodes, "円", Codes), number_codes(X, NumberCodes). | |
decode_rule(budget(X), Codes) :- nonvar(X), number_codes(X, NumberCodes), append(NumberCodes, "円", Codes). | |
decode_rule(budget(X), Codes) :- nonvar(Codes), append(NumberCodes, "円です", Codes), number_codes(X, NumberCodes). | |
decode_rule(budget(X), Codes) :- nonvar(X), number_codes(X, NumberCodes), append(NumberCodes, "円です", Codes). | |
decode_rule(accept(budget(X)), Codes) :- number_codes(X, NumberCodes), append(NumberCodes, "ですね", Codes). | |
decode_rule(any(Codes), Codes) :- nonvar(Codes). | |
decode_rule(and(X, Y), Codes) :- | |
nonvar(X), nonvar(Y), | |
decode_rule(X, CodesX), decode_rule(Y, CodesY), | |
append(CodesX, "、", Codes1), append(Codes1, CodesY, Codes). | |
encode(Text, Term) :- | |
atom_codes(Text, Codes), | |
decode_rule(Term, Codes). | |
decode(Term, Text) :- | |
decode_rule(Term, Codes), | |
atom_codes(Text, Codes). | |
% 応答ルール | |
conv_rule(agent, greeting(X), Ctx, greeting(X), Ctx). | |
conv_rule( | |
agent, | |
food(Food), Ctx, | |
and(accept(food(Food)), ask(place)), find_restaurant(Food, null, null)). | |
conv_rule( | |
agent, | |
place(Place), find_restaurant(Food, _, null), | |
and(accept(place(Place)), ask(budget)), find_restaurant(Food, Place, null)). | |
conv_rule( | |
agent, | |
budget(Budget), find_restaurant(Food, Place, _), | |
and(accept(budget(Budget)), any(Codes)), null) :- | |
find_restaurant(Food, Place, Budget, Restaurants), | |
%Restaurants = ["ほげ", "ふが", "ぴよ"], | |
join(Restaurants, ConcatedRestaurants), | |
append(ConcatedRestaurants, " が見つかりました", Codes). | |
conv_rule(agent, thanks, Ctx, accept(thanks), Ctx). | |
conv_rule(agent, terminate, Ctx, accept(terminate), end). | |
% 1ターンの対話を行なう | |
conv(Persona, Text1, Ctx1, Text2, Ctx2) :- | |
encode(Text1, Term1), | |
conv_rule(Persona, Term1, Ctx1, Term2, Ctx2), | |
decode(Term2, Text2). | |
conv(Persona, Text1, Ctx, 'すみません、言い方を変えてみてください', Ctx). | |
% 対話する | |
main_loop(end). | |
main_loop(Ctx1) :- | |
read(Text1), | |
conv(agent, Text1, Ctx1, Text2, Ctx2), | |
write('agent: '), write(Text2), | |
nl, | |
main_loop(Ctx2). | |
main :- main_loop(null). | |
% ユーティリティ | |
join([], ""). | |
join([X], X). | |
join([X|Xs], Codes) :- | |
join(Xs, Codes1), | |
append(X, ",", Codes2), | |
append(Codes2, Codes1, Codes). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment