14
15
16:- module(grammar_words, [
17 word/3, 18 word/4, 19 word_initial/3, 20 word_initial/4, 21 word_noninitial/3, 22 word_noninitial/4, 23 word_capitalize/4, 24 words/3, 25 words/4, 26 words_initial/3, 27 words_initial/4, 28 words_noninitial/3, 29 words_noninitial/4, 30 get_position/3, 31 warning/6, 32 try/4, 33 reset_progress_record/1, 34 get_unparsed_tokens_number/1 35 ]). 36
37:- use_module('../logger/error_logger', [
38 add_warning_message_once/4,
39 add_error_message_once/4
40 ]). 41:- use_module('../lexicon/lexicon_interface'). 42:- use_module('../lexicon/functionwords'). 43:- use_module('../lexicon/chars'). 44:- use_module('../lexicon/is_in_lexicon').
60word(Word) -->
61 [Word],
62 record_position.
63
64word(Word) -->
65 [^, Word],
66 record_position.
74word(Word, Condition) -->
75 [Word],
76 { call(Condition) },
77 record_position.
78
79word(Word, Condition) -->
80 [^, Word],
81 { call(Condition) },
82 record_position.
89word_initial(Word) -->
90 [^, Word],
91 record_position.
98word_initial(Word, Condition) -->
99 [^, Word],
100 { call(Condition) },
101 record_position.
108word_noninitial(Word) -->
109 [Word],
110 record_position.
118word_noninitial(Word, Condition) -->
119 [Word],
120 { call(Condition) },
121 record_position.
128word_capitalize(Word, _WordInitial) -->
129 [Word],
130 record_position.
131
132word_capitalize(Word, _WordInitial) -->
133 [^, Word],
134 record_position.
135
136word_capitalize(_Word, WordInitial) -->
137 [^, WordInitial],
138 record_position.
145words(WordList) -->
146 words_noninitial(WordList).
147
148words(WordList) -->
149 [^],
150 words_noninitial(WordList).
158words(WordList, Condition) -->
159 words_noninitial(WordList, Condition).
160
161words(WordList, Condition) -->
162 [^],
163 words_noninitial(WordList, Condition).
170words_initial(WordList) -->
171 [^],
172 words_noninitial(WordList).
180words_initial(WordList, Condition) -->
181 [^],
182 words_noninitial(WordList, Condition).
189words_noninitial([]) -->
190 record_position.
191
192words_noninitial([Word|Rest]) -->
193 [Word],
194 words_noninitial(Rest).
202words_noninitial([], Condition) -->
203 { call(Condition) },
204 record_position.
205
206words_noninitial([Word|Rest], Condition) -->
207 [Word],
208 words_noninitial(Rest, Condition).
215warning(Type, SentenceID, Subject, Description) -->
216 get_position(Pos),
217 {
218 PrevPos is Pos - 1,
219 add_warning_message_once(Type, SentenceID-PrevPos, Subject, Description)
220 }.
229try(Goal, _, Tokens, Tokens) :-
230 call(Goal),
231 !.
232
233try(_, error(Type, SentenceID, Subject, Description)) -->
234 get_position(Pos),
235 {
236 PrevPos is Pos - 1,
237 add_error_message_once(Type, SentenceID-PrevPos, Subject, Description),
238 fail
239 }.
240
241try(_, warning(Type, SentenceID, Subject, Description)) -->
242 get_position(Pos),
243 {
244 PrevPos is Pos - 1,
245 add_warning_message_once(Type, SentenceID-PrevPos, Subject, Description)
246 }.
253:- dynamic tokencount/1.
261reset_progress_record(TokenList) :-
262 retractall(position_backwards(_)),
263 record_position(TokenList, TokenList),
264 retractall(tokencount(_)),
265 length(TokenList, Length),
266 assert(tokencount(Length)),
267 !.
274get_unparsed_tokens_number(Number) :-
275 position_backwards(First),
276 findall(P, position_backwards(P), Positions),
277 get_minimum(Positions, First, Number).
284:- dynamic position_backwards/1.
292get_position(Position, [^|Tokens], [^|Tokens]) :-
293 !,
294 length(Tokens, Length),
295 tokencount(TokenCount),
296 Position is TokenCount - Length.
297
298get_position(Position, Tokens, Tokens) :-
299 !,
300 length(Tokens, Length),
301 tokencount(TokenCount),
302 Position is TokenCount - Length.
309record_position(List, List) :-
310 length(List, Length),
311 record_position(Length).
319record_position(Pos) :-
320 position_backwards(Pos),
321 !.
322
323record_position(Pos) :-
324 assert(position_backwards(Pos)).
331get_minimum([], M, M).
332
333get_minimum([N|Rest], Temp, M) :-
334 N < Temp,
335 !,
336 get_minimum(Rest, N, M).
337
338get_minimum([_|Rest], Temp, M) :-
339 get_minimum(Rest, Temp, M)
Word-level Grammar Rules
This module contains word-level grammar rules. It manages the fact that certain words can be capitalized at the beginning of a sentence. Furthermore, it keeps track of the parsing process and, in the case of an error, it can determine up to which token parsing succeeded.