Debian Codegraph @

src/mod/txt/doc/afnix-wm-txt.xml

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <!-- ====================================================================== -->
4 <!-- = afnix-wm-txt.xml = -->
5 <!-- = standard text processing module - writer manual = -->
6 <!-- ====================================================================== -->
7 <!-- = This program is free software; you can redistribute it and/or = -->
8 <!-- = modify it provided that this copyright notice is kept intact. = -->
9 <!-- = This program is distributed in the hope that it will be useful but = -->
10 <!-- = without any warranty; without even the implied warranty of = -->
11 <!-- = merchantability or fitness for a particular purpose. In no event = -->
12 <!-- = shall the copyright holder be liable for any direct, indirect, = -->
13 <!-- = incidental or special damages arising in any way out of the use = -->
14 <!-- = of this software. = -->
15 <!-- ====================================================================== -->
16 <!-- = copyright (c) 1999-2023 - amaury darsch = -->
17 <!-- ====================================================================== -->
18
19 <chapter module="txt" number="1">
20 <title>Standard Text Processing Module</title>
21
22 <p>
23 The <em>Standard Text Processing</em> module is an original
24 implementation of an object collection dedicated to text processing.
25 Although text scaning is the current operation perfomed in the field
26 of text processing, the module provides also specialized object
27 to store and index text data. Text sorting and transliteration is also
28 part of this module.
29 </p>
30
31 <!-- scanning capabilities -->
32 <section>
33 <title>Scanning concepts</title>
34
35 <p>
36 Text scanning is the ability to extract lexical elements or
37 <em>lexemes</em> from a stream. A scanner or lexical analyzer is
38 the principal object used to perform this task. A scanner is
39 created by adding special object that acts as a pattern
40 matcher. When a pattern is matched, a special object called a
41 <em>lexeme</em> is returned.
42 </p>
43
44 <!-- pattern object -->
45 <subsect>
46 <title>Pattern object</title>
47
48 <p>
49 A <code>Pattern</code> object is a special object that acts as
50 model for the string to match. There are several ways to build a
51 pattern. The simplest way to build it is with a regular
52 expression. Another type of pattern is a balanced pattern. In
53 its first form, a pattern object can be created with a regular
54 expression object.
55 </p>
56
57 <example>
58 # create a pattern object
59 const pat (afnix:txt:Pattern "$d+")
60 </example>
61
62 <p>
63 In this example, the pattern object is built to detect integer
64 objects.
65 </p>
66
67 <example>
68 pat:check "123" # true
69 pat:match "123" # 123
70 </example>
71
72 <p>
73 The <code>check</code> method return true if the input string
74 matches the pattern. The <code>match</code> method returns the
75 string that matches the pattern. Since the pattern object can
76 also operates with stream object, the <code>match</code> method
77 is appropriate to match a particular string. The pattern object
78 is, as usual, available with the appropriate predicate.
79 </p>
80
81 <example>
82 afnix:txt:pattern-p pat # true
83 </example>
84
85 <p>
86 Another form of pattern object is the balanced pattern. A
87 balanced pattern is determined by a starting string and an
88 ending string. There are two types of balanced pattern. One is a
89 single balanced pattern and the other one is the recursive
90 balanced pattern. The single balanced pattern is appropriate for
91 those lexical element that are defined by a character. For
92 example, the classical C-string is a single balanced pattern
93 with the double quote character.
94 </p>
95
96 <example>
97 # create a balanced pattern
98 const pat (afnix:txt:Pattern "ELEMENT" "&lt;" "&gt;")
99 pat:check "&lt;xml&gt;" # true
100 pat:match "&lt;xml&gt;" # xml
101 </example>
102
103 <p>
104 In the case of the C-string, the pattern might be more
105 appropriately defined with an additional <em>escape
106 character</em>. Such character is used by the pattern matcher to
107 grab characters that might be part of the pattern definition.
108 </p>
109
110 <example>
111 # create a balanced pattern
112 const pat (afnix:txt:Pattern "STRING" "'" '\\')
113 pat:check "'hello'" # true
114 pat:match "'hello'" # "hello"
115 </example>
116
117 <p>
118 In this form, a balanced pattern with an escape character is
119 created. The same string is used for both the starting and
120 ending string. Another constructor that takes two strings can be
121 used if the starting and ending strings are different.
122
123 The last pattern form is the balanced recursive form. In this
124 form, a starting and ending string are used to delimit the
125 pattern. However, in this mode, a recursive use of the starting
126 and ending strings is allowed. In order to have an exact match,
127 the number of starting string must equal the number of ending
128 string. For example, the C-comment pattern can be viewed as
129 recursive balanced pattern.
130 </p>
131
132 <example>
133 # create a c-comment pattern
134 const pat (afnix:txt:Pattern "STRING" "/*" "*/" )
135 </example>
136 </subsect>
137
138 <!-- lexeme object -->
139 <subsect>
140 <title>Lexeme object</title>
141
142 <p>
143 The <code>Lexeme</code> object is the object built by a scanner
144 that contains the matched string. A lexeme is therefore a tagged
145 string. Additionally, a lexeme can carry additional information
146 like a source name and index.
147 </p>
148
149 <example>
150 # create an empty lexeme
151 const lexm (afnix:txt:Lexeme)
152 afnix:txt:lexeme-p lexm # true
153 </example>
154
155 <p>
156 The default lexeme is created with any value. A value can be set
157 with the <code>set-value</code> method and retrieved with the
158 <code>get-value</code> methods.
159 </p>
160
161 <example>
162 lexm:set-value "hello"
163 lexm:get-value # hello
164 </example>
165
166 <p>
167 Similar are the <code>set-tag</code> and <code>get-tag</code>
168 methods which operate with an integer. The source name and index
169 are defined as well with the same methods.
170 </p>
171
172 <example>
173 # check for the source
174 lexm:set-source "world"
175 lexm:get-source # world
176
177 # check for the source index
178 lexm:set-index 2000
179 lexm:get-index # 2000
180 </example>
181 </subsect>
182 </section>
183
184 <!-- text scanning -->
185 <section>
186 <title>Text scanning</title>
187
188 <p>
189 Text scanning is the ability to extract lexical elements or
190 lexemes from an input stream. Generally, the lexemes are the
191 results of a matching operation which is defined by a pattern
192 object. As a result, the definition of a scanner object is the
193 object itself plus one or several pattern object.
194 </p>
195
196 <!-- scanner construction -->
197 <subsect>
198 <title>Scanner construction</title>
199
200 <p>
201 By default, a scanner is created without pattern objects. The
202 <code>length</code> method returns the number of pattern
203 objects. As usual, a predicate is associated with the scanner
204 object.
205 </p>
206
207 <example>
208 # the default scanner
209 const scan (afnix:txt:Scanner)
210 afnix:txt:scanner-p scan # true
211
212 # the length method
213 scan:length # 0
214 </example>
215
216 <p>
217 The scanner construction proceeds by adding pattern
218 objects. Each pattern can be created independently, and later
219 added to the scanner. For example, a scanner that reads real,
220 integer and string can be defined as follow:
221 </p>
222
223 <example>
224 # create the scanner pattern
225 const REAL (
226 afnix:txt:Pattern "REAL" [$d+.$d*])
227 const STRING (
228 afnix:txt:Pattern "STRING" "&quot;" '\\')
229 const INTEGER (
230 afnix:txt:Pattern "INTEGER" [$d+|"0x"$x+])
231 # add the pattern to the scanner
232 scanner:add INTEGER REAL STRING
233 </example>
234
235 <p>
236 The order of pattern integration defines the priority at which a
237 token is recognized. The symbol name for each pattern is
238 optional since the functional programming permits the creation
239 of patterns directly. This writing style makes the scanner
240 definition easier to read.
241 </p>
242 </subsect>
243
244 <!-- unsing the scanner -->
245 <subsect>
246 <title>Using the scanner</title>
247
248 <p>
249 Once constructed, the scanner can be used <em>as is</em>. A
250 stream is generally the best way to operate. If the scanner
251 reaches the end-of-stream or cannot recognize a lexeme, the nil
252 object is returned. With a loop, it is easy to get all lexemes.
253 </p>
254
255 <example>
256 while (trans valid (is:valid-p)) {
257 # try to get the lexeme
258 trans lexm (scanner:scan is)
259 # check for nil lexeme and print the value
260 if (not (nil-p lexm)) (println (lexm:get-value))
261 # update the valid flag
262 valid:= (and (is:valid-p) (not (nil-p lexm)))
263 }
264 </example>
265
266 <p>
267 In this loop, it is necessary first to check for the end of the
268 stream. This is done with the help of the special loop construct
269 that initialize the <code>valid</code> symbol. As soon as the
270 the lexeme is built, it can be used. The lexeme holds the value
271 as well as it tag.
272 </p>
273 </subsect>
274 </section>
275
276 <!-- text sorting -->
277 <section>
278 <title>Text sorting</title>
279
280 <p>
281 Sorting is one the primary function implemented inside the
282 <em>text processing</em> module. There are three sorting
283 functions available in the module.
284 </p>
285
286 <!-- ascending and descending order sorting -->
287 <subsect>
288 <title>Ascending and descending order sorting</title>
289
290 <p>
291 The <code>sort-ascent</code> function operates with a vector
292 object and sorts the elements in ascending order. Any kind of
293 objects can be sorted as long as they support a comparison
294 method. The elements are sorted in placed by using a <em>quick
295 sort</em> algorithm.
296 </p>
297
298 <example>
299 # create an unsorted vector
300 const v-i (Vector 7 5 3 4 1 8 0 9 2 6)
301 # sort the vector in place
302 afnix:txt:sort-ascent v-i
303 # print the vector
304 for (e) (v) (println e)
305 </example>
306
307 <p>
308 The <code>sort-descent</code> function is similar to the
309 <code>sort-ascent</code> function except that the object are
310 sorted in descending order.
311 </p>
312 </subsect>
313
314 <!-- lexical sorting -->
315 <subsect>
316 <title>Lexical sorting</title>
317
318 <p>
319 The <code>sort-lexical</code> function operates with a vector
320 object and sorts the elements in ascending order using a
321 lexicographic ordering relation. Objects in the vector must be
322 literal objects or an exception is raised.
323 </p>
324 </subsect>
325 </section>
326
327 <!-- transliteration -->
328 <section>
329 <title>Transliteration</title>
330
331 <p>
332 Transliteration is the process of changing characters my mapping
333 one to another one. The transliteration process operates with a
334 character source and produces a target character with the help of
335 a mapping table. The transliteration process is not necessarily
336 reversible as often indicated in the literature.
337 </p>
338
339 <!-- literate object -->
340 <subsect>
341 <title>Literate object</title>
342
343 <p>
344 The <code>Literate</code> object is a transliteration object
345 that is bound by default with the identity function mapping. As
346 usual, a predicate is associate with the object.
347 </p>
348
349 <example>
350 # create a transliterate object
351 const tl (afnix:txt:Literate)
352
353 # check the object
354 afnix:txt:literate-p tl # true
355 </example>
356
357 <p>
358 The transliteration process can also operate with an escape
359 character in order to map double character sequence into a
360 single one, as usually found inside programming language.
361 </p>
362
363 <example>
364 # create a transliterate object by escape
365 const tl (afnix:txt:Literate '\\')
366 </example>
367 </subsect>
368
369 <!-- transliteration configuration -->
370 <subsect>
371 <title>Transliteration configuration</title>
372
373 <p>
374 The <code>set-map</code> configures the transliteration mapping
375 table while the <code>set-escape-map</code> configure the escape
376 mapping table. The mapping is done by setting the source
377 character and the target character. For instance, if one want to
378 map the tabulation character to a white space, the mapping table
379 is set as follow:
380 </p>
381
382 <example>
383 tl:set-map '\t' ' '
384 </example>
385
386 <p>
387 The escape mapping table operates the same way. It should be
388 noted that the mapping algorithm translate first the input
389 character, eventually yielding to an escape character and then
390 the escape mapping takes place. Note also that the
391 <code>set-escape</code> method can be used to set the escape
392 character.
393 </p>
394
395 <example>
396 tl:set-map '\t' ' '
397 </example>
398 </subsect>
399
400 <!-- transliteration process -->
401 <subsect>
402 <title>Transliteration process</title>
403
404 <p>
405 The transliteration process is done either with a string or an
406 input stream. In the first case, the <code>translate</code>
407 method operates with a string and returns a translated
408 string. On the other hand, the <code>read</code> method returns
409 a character when operating with a stream.
410 </p>
411
412 <example>
413 # set the mapping characters
414 tl:set-map '\h' 'w'
415 tl:set-map '\e' 'o'
416 tl:set-map '\l' 'r'
417 tl:set-map '\o' 'd'
418
419 # translate a string
420 tl:translate "helo" # word
421 </example>
422 </subsect>
423 </section>
424 </chapter>