プレーンテキストファイルを読み取り、各行に述語を適用したいと思います(述語にwrite
は出力を行うものが含まれています)。どうすればいいですか?
5 に答える
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
もちろん、ここでの出力はを使用した例です。リクエストに一致するように構成します。
この原則はあなたの質問に答えるために適用できると思います。幸運を。
SWI-Prologでは、最もクリーンな解決策は、「行」が何であるかを説明するDCGを記述してから、各行の述語を呼び出すことです。library(pio)を使用して、DCGをファイルに適用します。
編集:要求に応じて、検討してください:
:- 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').
ファイルから未解釈のプレーンテキスト行を取得するために、パフォーマンスソリューションでは、より多くの可能性があり、より合理的なものがあります。
SWI-プロローグ:
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プロローグ:
:- use_module(library(stream/console)).
655行のファイルを読み取るタイミングは次のとおりです。
test :-
open('<path>', read, Stream),
test(Stream),
close(Stream).
test(Stream) :-
read_line(Stream, _), !,
test(Stream).
test(_).
SWI-プロローグ:
̀?- time((between(1,100,_), test, fail; true)).
% 328,300 inferences, 0.125 CPU in 0.143 seconds (88% CPU, 2626400 Lips)
true.
Jekejekeプロローグ:
?- 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
アトムではなく文字列に読み込むSWI-Prologソリューションの方が速いと思います。しかし、上記では、アトムとアトムの読み取り値を比較しています。
ここでの応答を考慮して、これを作成しました。これは、Pythonの「with」に似ています。
?- 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).
de SWI-Prologのドキュメントには良い例があります:
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