| Did you know ... | Search Documentation: |
| Predicate phrase_from_file/3 |
[nondet]phrase_from_file(:Grammar,
+File)
Options are passed to open/4
and phrase_from_stream/3.
The latter notable allows for as(chars) to call a DCG
compiled for dealing with character lists rather than codes.
Below is an example that counts the number of times a string appears in a file. The library dcg/basics provides string//1 matching an arbitrary string and remainder//1 which matches the remainder of the input without parsing.
:- use_module(library(dcg/basics)).
file_contains(File, Pattern) :-
phrase_from_file(match(Pattern), File).
match(Pattern) -->
string(_),
string(Pattern),
remainder(_).
match_count(File, Pattern, Count) :-
aggregate_all(count, file_contains(File, Pattern), Count).
This can be called as (note that the pattern must be a string (code list)):
?- match_count('pure_input.pl', `file`, Count).