-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenize.pl
304 lines (231 loc) · 8.52 KB
/
tokenize.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
:- module(tokenize,
[ tokenize/2,
tokenize/3,
tokenize_file/2,
tokenize_file/3,
untokenize/2
]).
/** <module> tokenize
This module offers a simple tokenizer with some basic options.
It may be improved towards a cleaner and more extensible tool if there
is enough interest (from myself or others).
@author Shon Feder
@license <http://unlicense.org/>
Rational:
tokenize_atom/2, in library(porter_stem), is inflexible, in that it doesn't
allow for the preservation of white space or control characters, and
it only tokenizes into a list of atoms. This module allows for options to
include or exclude things like spaces and punctuation, and for packing tokens.
It also provides a simple predicate for reading lists of tokens back into
text.
Ideally, provided I have the drive and/or there is any interest in this package,
this would become an extensible, easily configurable tokenizing utility.
*/
%% untokenize(+Tokens:list(term), -Untokens:list(codes)) is semidet.
%
% True when Untokens is unified with a code list representation of each
% token in Tokens.
% TODO structure(Options:[lines, brackets])
% TODO mode(generate) ; mode(parse)
% TODO add output format option
untokenize(Tokens, Untokens) :-
untokenize(Tokens, Untokens, []).
untokenize(Tokens, Untokens, _Options) :-
maplist(token_to(codes), Tokens, TokenCodes),
phrase(non_tokens(TokenCodes), Untokens).
non_tokens([T]) --> T.
non_tokens([T|Ts]) --> T, non_tokens(Ts).
%% tokenize_file(+File:atom, -Tokens:list(term)) is semidet.
%
% @see tokenize_file/3 is called with an empty list of options: thus, with defaults.
%
% Note: does not use phrase_from_file/3, thus not lazy or transparent
% This choice was made so that tokenize_file will work with remotely
% accessed files.
% TODO: add more source options
tokenize_file(File, Tokens) :-
tokenize_file(File, Tokens, []).
%% tokenize_file(+File:atom, -Tokens:list(term), +Options:list(term)) is semidet.
%
% True when Tokens is unified with a list of tokens represening
% the text of File.
%
% @see tokenize/3 which has the same available options and behavior.
tokenize_file(File, Tokens, Options) :-
read_file_to_codes(File, Codes, [encoding(utf8)]),
tokenize(Codes, Tokens, Options).
%% tokenize(+Text:list(code), -Tokens:list(term)) is semidet.
%
% @see tokenize/3 is called with an empty list of options: thus, with defaults.
% TODO: add support for unicode
tokenize(Text, Tokens) :-
tokenize(Text, Tokens, []).
%% tokenize(+Text:list(code), -Tokens:list(term), +Options:list(term)) is semidet.
%
% True when Tokens is unified with a list of tokens representing the text from
% Text, according to the options specified in Options.
%
% NOTE: this predicate currently fails if invalid option arguments are given
% and, worse, it succeeds silently if there are invalid option parameters.
%
% A token is one of:
%
% * a word (contiguous alpha-numeric chars): `word(W)`
% * a punctuation mark (determined by `char_type(C, punct)`): `punct(P)`
% * a control character (determined by `char_typ(C, cntrl)`): `cntrl(C)`
% * a space ( == " "): `spc(S)`.
%
% Valid options are:
%
% * cased(+bool) : Determines whether tokens perserve cases of the source text.
% * spaces(+bool) : Determines whether spaces are represted as tokens or discarded.
% * cntrl(+bool) : Determines whether control characters are represented as tokens or discarded.
% * punct(+bool) : Determines whether punctuation characters are represented as tokens or discarded.
% * to(+on_of([strings,atoms,chars,codes])) : Determines the representation format used for the tokens.
% * pack(+bool) : Determines whether tokens are packed or repeated.
tokenize(Text, Tokens, Options) :-
string_codes(Text, Codes),
phrase(process_options, [Options-Codes], [Options-Tokens]).
% PROCESSING OPTIONS
%
% NOTE: This way of processing options is probably stupid.
% I will correct/improve/rewrite it if there is ever a good
% reason to. But for now, it works.
%
% TODO: Throw exception if invalid options are passed in.
% At the moment it just fails.
%% Dispatches dcgs by option-list functors, with default values.
process_options -->
opt(cased, false),
non_opt(tokenize_text),
opt(spaces, true),
opt(cntrl, true),
opt(punct, true),
opt(to, atoms),
opt(pack, false).
%% opt(+OptionFunctor:atom, DefaultValue:nonvar)
%
% If dcg functor is identical to the option name with 'opt_' prefixed,
% then the dcg functor can be omitted.
opt(Opt, Default) -->
{ atom_concat('opt_', Opt, Opt_DCG) },
opt(Opt, Default, Opt_DCG).
%% opt(+OptionFunctor:atom, +DefaultValue:nonvar, +DCGFunctor:atom).
opt(Opt, Default, DCG) -->
state(Opts-Text0, Text0),
{
pad(Opt, Selection, Opt_Selection),
option(Opt_Selection, Opts, Default),
DCG_Selection =.. [DCG, Selection]
},
DCG_Selection,
state(Text1, Opts-Text1).
%% This ugly bit should be dispensed with...
opt(Opt, Default, _) -->
state(Opts-_),
{
var(Default), \+ option(Opt, Opts),
writeln("Unknown options passed to opt//3: "),
write(Opt)
}.
%% non_opt(+DCG).
%
% Non optional dcg to dispatch. Passes the object of concern
% without the options list, then recovers option list.
non_opt(DCG) -->
state(Opts-Text0, Text0),
DCG,
state(Text1, Opts-Text1).
state(S0), [S0] --> [S0].
state(S0, S1), [S1] --> [S0].
%% Dispatching options:
opt_cased(true) --> [].
opt_cased(false) --> state(Text, LowerCodes),
{
text_to_string(Text, Str),
string_lower(Str, LowerStr),
string_codes(LowerStr, LowerCodes)
}.
tokenize_text --> state(Text, Tokenized),
{ phrase(tokens(Tokenized), Text) }.
opt_spaces(true) --> [].
opt_spaces(false) --> state(T0, T1),
{ exclude( =(spc(_)), T0, T1) }.
opt_cntrl(true) --> [].
opt_cntrl(false) --> state(T0, T1),
{ exclude( =(cntrl(_)), T0, T1) }.
opt_punct(true) --> [].
opt_punct(false) --> state(T0, T1),
{ exclude( =(punct(_)), T0, T1) }.
opt_to(codes) --> [].
opt_to(Type) --> state(CodeTokens, Tokens),
{ maplist(token_to(Type), CodeTokens, Tokens) }.
opt_pack(false) --> [].
opt_pack(true) --> state(T0, T1),
{ phrase(pack_tokens(T1), T0) }.
%% POST PROCESSING
%% Convert tokens to alternative representations.
token_to(Type, Token, Converted) :-
( Type == strings -> Conversion = inverse(string_codes)
; Type == atoms -> Conversion = inverse(atom_codes)
; Type == chars -> Conversion = inverse(string_chars)
; Type == codes -> Conversion = string_codes
),
call_into_term(Conversion, Token, Converted).
%% Packing repeating tokens
%
pack_tokens([T]) --> pack_token(T).
pack_tokens([T|Ts]) --> pack_token(T), pack_tokens(Ts).
pack_token(P) --> pack(Token, N), {Token =.. [F,T], P =.. [F,T,N]}.
pack(X, Count) --> [X], pack(X, 1, Count).
pack(_, Total, Total) --> call(eos).
pack(X, Total, Total), [Y] --> [Y], { Y \= X }.
pack(X, Count, Total) --> [X], { succ(Count, NewCount) },
pack(X, NewCount, Total).
%% PARSING
tokens([T]) --> token(T), call(eos), !.
tokens([T|Ts]) --> token(T), tokens(Ts).
%% tokens(_) --> {length(L, 200)}, L, {format(L)}, halt, !. % For debugging.
token(word(W)) --> word(W), call(eos), !.
token(word(W))," " --> word(W), " ".
token(word(W)), C --> word(W), (punct(C) ; cntrl(C) ; nasciis(C)).
token(spc(S)) --> spc(S).
token(punct(P)) --> punct(P).
token(cntrl(C)) --> cntrl(C).
token(other(O)) --> nasciis(O).
spc(" ") --> " ".
sep --> ' '.
sep --> call(eos), !.
word(W) --> csyms(W).
csyms([L]) --> csym(L).
csyms([L|Ls]) --> csym(L), csyms(Ls).
csym(L) --> [L], {code_type(L, csym)}.
% non ascii's
nasciis([C]) --> nascii(C), (call(eos), !).
nasciis([C]),[D] --> nascii(C), [D], {D < 127}.
nasciis([C|Cs]) --> nascii(C), nasciis(Cs).
nascii(C) --> [C], {C > 127}.
%% blanks --> [].
%% blanks --> ' '.
' ' --> space.
' ' --> space, ' '.
... --> [].
... --> [_], ... .
space --> [S], {code_type(S, white)}.
punct([P]) --> [P], {code_type(P, punct)}.
cntrl([C]) --> [C], {code_type(C, cntrl)}.
eos([], []).
%% move to general module
codes_to_lower([], []).
codes_to_lower([U|Uppers], [L|Lowers]) :-
code_type(U, to_upper(L)),
codes_to_lower(Uppers, Lowers).
call_into_term(P, Term, Result) :-
Term =.. [F, Arg],
call(P, Arg, ResultArg),
Result =.. [F, ResultArg].
inverse(P, A, B) :-
call(P, B, A).
pad(T_Args, X, T_X_Args) :-
T_Args =.. [T|Args],
T_X_Args =.. [T, X| Args].