[ACCEPTED]-Read a file line by line in Prolog-dcg

Accepted answer
Score: 25

You can use read to read the stream. Remember 7 to invoke at_end_of_stream to ensure no syntax errors.

Example:

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'.

Thus 6 by invoking main you will recieve the output:

?- main.
[line 0,line 1,line 2,line 3,line 4,line 5,line 6,line 7,line 8,line 9]
true 

Just 5 configure main. The output here is an example 4 by using write, of course. Configure to match 3 your request.

I assume that this principle 2 can be applied to answer your question. Good 1 luck.

Score: 18

In SWI-Prolog, the cleanest solution is 4 to write a DCG that describes what a "line" is, then 3 call a predicate for each line. Use library(pio) to 2 apply the DCG to a file.

EDIT: As requested, consider:

:- 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).

Sample 1 usage: ?- phrase_from_file(lines(Ls), 'your_file.txt').

Score: 2

There are kind of more possible in number 8 and more reasonable in performance solutions, to 7 get uninterpreted i.e plain text lines from 6 a file:

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)).

Here are 5 some timings, reading a file of 655 lines:

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 4 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

I guess a SWI-Prolog solution that 3 reads into a string instead into an atom 2 could be faster. But in the above we compare 1 atom against atom reading.

Score: 0

Given the responses here I created this, which 2 more like 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).

code 1 :

:- 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).
Score: 0

There is a nice example in de SWI-Prolog 1 documentation:

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
    ).

source: https://www.swi-prolog.org/pldoc/man?predicate=read_string/5

More Related questions