我目前正在开发一个递归 Prolog 程序,将路由链接在一起以创建一个 基本的 伯明翰地区的 GPS。目前我可以得到这样的输出:

输入

routeplan(selly_oak, aston, P).

输出

P = [selly_oak, edgbaston, ... , aston]

我想做的是让我的程序提供某种接口,所以如果我要输入以下内容:

Route from selly_oak to aston

它将为我提供:

Go from selly_oak to edgbaston
Go from edgbaston to ...
Finally, Go from ... to aston.

Prolog 是一种功能强大的语言,所以我认为这是很容易实现的,但是我拿出的许多书籍似乎都跳过了这一部分。据我所知,我必须使用 write() 和 read() 之类的东西,尽管我不知道细节。

任何一个 Prolog 新手都可以提供一些基本示例或更多信息的链接吗?

编辑: 其中很多答案看起来非常复杂,解决方案应该只有大约 5-10 行代码。读取值不是问题,因为我可以执行以下操作:

find:- 
    write('Where are you? '), 
    read(X), 
    nl, write('Where do you want to go? '),
    read(Y), 
    loopForRoute(X,Y).

如果可以使用 write() 写出输出,那么我更喜欢使用新行 (nl),这样它就会像上面的输出一样显示。

如果这是我的输入,那么我将如何安排顶部路线计划()来处理这些输入?另外,如果我要添加这些车站的线路作为额外参数,那么这将如何实现?所有链接都在文件的开头定义,如下所示:

rlinks(selly_oak, edgbaston, uob_line).
rlinks(edgbaston, bham_new_street, main_line).

因此,有了这些信息,能够像这样阅读该行就好了。

Go from selly_oak to edgbaston using the uob_line
Go from edgbaston to ... using the ...
Finally, go from ... to aston using the astuni_line
有帮助吗?

解决方案

有关这样的事情,我通常创建壳谓词。所以你的情况...

guided:-
    print('Enter your start point'),nl,
    read(Start),
    print('Enter your destination'),nl,
    read(Dest),
    routeplan(Start, Dest, Route),
    print_route(Route).

和print_route / 1可能是一些递归这样的:

print_route([]).

print_route([[A,B,Method]|Tail]):-
    print_route(Tail),
    print('Go from '), print(A),
    print(' to '), print(B),
    print(' by '), print(Method), nl.

我假定routeplan / 3谓词的第三变量是一个列表的列表。此外,它的加入到尾部建成。如果它不是,它应该很容易适应。向中的注释。

其他提示

有一本详细讨论这些事情的书 Prolog 程序员的自然语言处理作者:迈克尔·A.卡温顿。

一般来说,你需要做的是

  1. 对输入进行标记
  2. 解析标记(例如与 DCG)以获得输入 routeplan/3
  3. 称呼 routeplan/3
  4. 根据输出生成一些英语 routeplan/3

像这样的东西(在 SWI-Prolog 中工作):

% Usage example:
%
% ?- query_to_response('Route from selly_oak to aston', Response).
%
% Response = 'go from selly_oak to edgbaston then go from edgbaston
%         to aston then stop .'
%
query_to_response(Query, Response) :-
    concat_atom(QueryTokens, ' ', Query), % simple tokenizer
    query(path(From, To), QueryTokens, []),
    routeplan(From, To, Plan),
    response(Plan, EnglishTokens, []),
    concat_atom(EnglishTokens, ' ', Response).

% Query parser
query(path(From, To)) --> ['Route'], from(From), to(To).
from(From) --> [from], [From], { placename(From) }.
to(To) --> [to], [To], { placename(To) }.

% Response generator
response([_]) --> [stop], [.].
response([From, To | Tail]) -->
    goto(path(From, To)), [then], response([To | Tail]).
goto(path(From, To)) --> [go], from(From), to(To).

% Placenames
placename(selly_oak).
placename(aston).
placename(edgbaston).

% Mock routeplan/3
routeplan(selly_oak, aston, [selly_oak, edgbaston, aston]).

嗯,如果我理解正确的话,你只是想很好地格式化列表打印出来,不是吗?

在SWI-Prolog中工作的:

output_string([A,B],StrIn,StrOut) :-
 concat_atom([StrIn, 'Finally, Go from ', A, ' to ', B, '.'],StrOut),
 write(StrOut).

output_string([A,B|Rest],StrIn,StrOut) :-
 concat_atom([StrIn,'Go from ', A, ' to ', B, '.\n'],StrAB),
 output_string([B|Rest],StrAB,StrOut).

然后用调用

output_string(P,'',_).

这可能不是很有效,但它的工作。 :)

这里有几个谓词来从文件中读取/流线成Prolog的字符串:

%%% get_line(S, CL): CL is the string read up to the end of the line from S.
%%% If reading past end of file, returns 'end_of_file' in CL first, raises
%%% an exception second time.
%%% :- pred get_string(+stream, -list(int)).
get_line(S, CL) :-
    peek_code(S, C),
    (   C = -1
    ->  get_code(S, _),
        CL = end_of_file
    ;   get_line(S, C, CL)).

get_line(_, -1, CL) :- !, CL = [].  % leave end of file mark on stream
get_line(S, 0'\n, CL) :- !,
    get_code(S, _),
    CL = [].
get_line(S, C, [C|CL]) :-
    get_code(S, _),
    peek_code(S, NC),
    get_line(S, NC, CL).

%% read_lines(L): reads lines from current input to L.  L is a list of list
%% of character codes, newline characters are not included.
%% :- pred read_lines(-list(list(char))).
read_lines(L) :-
    current_input(In),
    get_line(In, L0),
    read_lines(In, L0, L).

%% read_lines(F, L): reads lines from F to L.  L is a list of list of character
%% codes, newline characters are not included.
%% :- pred read_lines(+atom, -list(list(char))).
read_lines(F, L) :-
    fail_on_error(open(F, read, S)),
    call_cleanup((get_line(S, L0),
              read_lines(S, L0, L)),
             close(S)).

read_lines(_, end_of_file, L) :- !, L = [].
read_lines(S, H, [H|T]) :-
    get_line(S, NH),
    read_lines(S, NH, T).

然后,看看 DCG S表示信息如何解析的字符串。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top