我想读取一个纯文本文件,并对每一行应用一个谓词(这些谓词包含“write”来进行输出)。我该如何做?
您可以使用 read
来读取流。记得调用 at_end_of_stream
来确保没有语法错误。
示例:
readFile.pl
main :-
open('myFile.txt', read, Str),
read_file(Str,Lines),
close(Str),
write(Lines), nl.
read_file(Stream,[]) :-
at_end_of_stream(Stream).
read_file(Stream,[X|L]) :-
\+ at_end_of_stream(Stream),
read(Stream,X),
read_file(Stream,L).
myFile.txt
'line 0'.
'line 1'.
'line 2'.
'line 3'.
'line 4'.
'line 5'.
'line 6'.
'line 7'.
'line 8'.
'line 9'.
因此,通过调用 main
,您将收到输出:
?- main.
[line 0,line 1,line 2,line 3,line 4,line 5,line 6,line 7,line 8,line 9]
true
只需配置 main
。这里的输出是使用 write
的示例,当然可以根据您的请求进行配置。
我认为这个原则可以应用于回答您的问题。祝你好运。
:- use_module(library(pio)).
lines([]) --> call(eos), !.
lines([Line|Lines]) --> line(Line), lines(Lines).
eos([], []).
line([]) --> ( "\n" ; call(eos) ), !.
line([L|Ls]) --> [L], line(Ls).
使用示例:?- phrase_from_file(lines(Ls), 'your_file.txt')
findall/3
来获取某个模式的所有实例,但我看到你只是使用一个解析整个文件的 dcg。出于好奇,为什么我们必须使用 call(eos)
而不是一个 dcg 规则呢? - Shoncall//1
(然后是eos/2
)用于可移植地引用隐式DCG参数的全部内容。您不能使用DCG规则替代它,因为DCG规则受到翻译规则的限制,只能引用这些参数的特定部分。 "可移植"意味着这与任何特定的Prolog系统如何将DCG规则翻译为Prolog规则无关,因此它可以在支持目前由ISO起草的DCG的所有系统中工作。 - mat有许多种解决方案可以从文件中获取未经处理的纯文本行,这些解决方案数量更多,性能更加合理:
SWI-Prolog:
最初的回答:
read_line(S, X) :-
read_line_to_codes(S, L),
read_line2(L, X).
read_line2(end_of_file, _) :- !, fail.
read_line2(L, X) :-
atom_codes(X, L).
Jekejeke Prolog:
:- use_module(library(stream/console)).
最初的回答:
在读取一份655行的文件时,以下是一些时间记录:test :-
open('<path>', read, Stream),
test(Stream),
close(Stream).
test(Stream) :-
read_line(Stream, _), !,
test(Stream).
test(_).
SWI-Prolog:
̀?- time((between(1,100,_), test, fail; true)).
% 328,300 inferences, 0.125 CPU in 0.143 seconds (88% CPU, 2626400 Lips)
true.
Jekejeke Prolog:
?- time((between(1,100,_), test, fail; true)).
% Up 121 ms, GC 2 ms, Thread Cpu 94 ms (Current 05/07/19 17:19:05)
Yes
file_line(File, Line) :-
setup_call_cleanup(open(File, read, In),
stream_line(In, Line),
close(In)).
stream_line(In, Line) :-
repeat,
( read_line_to_string(In, Line0),
Line0 \== end_of_file
-> Line0 = Line
; !,
fail
).
来源:https://www.swi-prolog.org/pldoc/man?predicate=read_string/5
?- read_file('test.txt', tokenize,5,L). %first 5 lines
?- read_file('test.txt', tokenize,L). %the whole file
?- read_file('test.txt', split,5,L). %just split
?- open('test.txt',read,S), read_lines(S,split,5,L), close(S).
代码:
:- module(files,[read_line/3, read_file/3, read_file/4, read_lines/3, read_lines/4, split/2, split/3, split/4]).
:- use_module(library(pcre)).
string2atoms(Strings, Atoms) :- maplist(atom_string, Atoms, Strings).
split(Str, Lst) :- split_string(Str, " ", "", Lst).
split(Str, Separator, Lst) :- split_string(Str, Separator, "", Lst).
split(Str, Separator, Pad, Lst) :- split_string(Str, Separator, Pad, Lst).
is_empty(Str) :- re_match(Str, '^\s*$').
non_empty(Str) :- ( is_empty(Str) -> false ; true).
tokenize(String,Tokens) :- split(String,Lst), string2atoms(Lst,Tokens).
%read a line and execute a Goal on it
read_line(Stream,Goal,Args) :-
\+ at_end_of_stream(Stream), read_line_to_string(Stream,Str),
%% \+ isempty(Str), call(Goal,Str,Args).
( is_empty(Str) -> true ; call(Goal,Str,Args)).
% given Stream execute Goal on every line. with the option to process only N lines
read_lines(Stream, _, _,_) :- at_end_of_stream(Stream), !. %is EOF
read_lines(_, _, 0,_) :- !. % only N lines
read_lines(Stream, Goal, N, [Res|T]) :-
N0 is N - 1, read_line(Stream, Goal, Res), writeln(Res),
read_lines(Stream, Goal, N0, T).
%read the whole file
read_lines(Stream, Goal, LoL) :- read_lines(Stream, Goal, 1000000, LoL).
%given file name execute Goal on every line
read_file(File, Goal, N, Res) :-
open(File, read, Stream), read_lines(Stream, Goal, N, Res), close(Stream).
read_file(File, Goal, Res) :- read_file(File, Goal, 1000000, Res).