-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_file.pl
111 lines (100 loc) · 3.58 KB
/
read_file.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
:- module(read_file, [read_file/2]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% File reader for .pddl files.
%% It reads the input file character by character and parse it
%% into a list. Brackets, comma, period and question marks
%% are treated as separate words. White spaces separe words.
%%
%% Similar to read_sent in Pereira and Shieber, Prolog and
%% Natural Language Analysis, CSLI, 1987.
%%
%% Examples:
%% :- read_file('input.txt', L).
%% input.txt > The sky was blue, after the rain.
%% L = [the, sky, was, blue, ',', after, the, rain, '.']
%%
%% :- read_file('domain.pddl', L).
%% domain.pddl >
%% (define (domain BLOCKS)
%% (:requirements :strips :typing :action-costs)
%% (:types block)
%% (:predicates (on ?x - block ?y - block)
%% ...
%% L = ['(', define, '(', domain, blocks, ')', '(', :, requirements|...].
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% read_file(+File, -List).
read_file(File, Words) :-
see(File), % sets File as the new stream input
get_code(C), % gets the code of the next character
read_rest(C, Words),
seen. % closes the stream and sets the standard input as the new stream input
%% read_rest(+Code, -List).
% unifies [] when input ends
read_rest(-1, []) :- !.
read_rest(C, [Word|Words]) :-
C = 34,
!,
get_code(C1),
read_json_value(C1, Chars, Next),
name(Word, Chars),
read_rest(Next, Words). % keeps track of the last character read (Next)
% ignores spaces, tabs and newlines
read_rest(C, Words) :-
(C = 32 ; C = 10 ; C = 9 ; C = 13 ; C = 92),
!,
get_code(C1),
read_rest(C1, Words).
% brackets, comma, period or question marks are treated as separed words
read_rest(C, [Char|Words]) :-
(C = 40 ; C = 41 ; C = 44 ; C = 45 ; C = 46 ; C = 63 ; C = 58 ; C = 91 ; C = 93),
name(Char, [C]),
!,
get_code(C1),
read_rest(C1, Words).
% reads comments to the end of line
read_rest(59, Words) :-
get_code(Next),
!,
read_comment(Next, Last), % keeps track of the last character read
read_rest(Last, Words).
% otherwise gets all of the next word
read_rest(C, [Word|Words]) :-
read_word(C, Chars, Next),
name(Word, Chars),
read_rest(Next, Words). % keeps track of the last character read (Next)
%% read_json_value(+Code, -Characters, -NextCode).
% stops if " found
read_json_value(34, [], Next) :-
get_code(Next),
!.
% otherwise, gets characters and converts them to lower case
read_json_value(C, [LC|Chars], Last) :-
lower_case(C, LC),
get_code(Next),
read_json_value(Next, Chars, Last).
%% read_comment(+Code, -LastCode).
% keep reading as long you don't find end-of-line or end-of-file
read_comment(10, 10) :- !.
read_comment(-1, -1) :- !.
read_comment(_, Last) :-
get_code(Next),
read_comment(Next, Last).
%% read_word(+Code, -Characters, -NextCode).
% spaces, commas, newlines, periods, end-of-file or question marks separate words
read_word(C, [], C) :-
(C = 32 ; C = 44 ; C = 10 ; C = 9 ; C = 13 ;
C = 46 ; C = 63 ; C = 40 ; C = 41 ; C = 58 ;
C = -1 ; C = 93),
!.
% otherwise, gets characters and converts them to lower case
read_word(C, [LC|Chars], Last) :-
lower_case(C, LC),
get_code(Next),
read_word(Next, Chars, Last).
%% lower_case(+Code, -LowerCaseCode).
% converts to lower case if necessary
lower_case(C, C) :-
(C < 65 ; C > 90),
!.
lower_case(C, LC) :-
LC is C + 32.