summaryrefslogtreecommitdiff
path: root/lang/prolog/1.8.7/src/thesaurus
blob: 4694981dd57d83e2f797ea57bd3b83034d26777b (plain)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
(* ------------------- VERSION 2     19.01.87 ------------------- *)
PACKET thesaurus handling                (* Autor: J.Liedtke      *)
 
       DEFINES     THESAURUS , 
                   := , 
                   empty thesaurus , 
                   insert,               (* fuegt ein Element ein               *)
                   delete,               (* loescht ein Element falls vorhanden *)
                   rename,               (* aendert ein Element falls vorhanden *)
                   CONTAINS ,            (* stellt fest, ob enthalten           *)
                   link ,                (* index in thesaurus                  *) 
                   name ,                (* name of entry                       *)
                   decode invalid chars ,(* Steuerzeichen dekodieren            *)
                   get ,                 (* get next entry ("" is eof)          *)
                   highest entry :       (* highest valid index of thes         *)


TYPE THESAURUS = TEXT ;
 
LET nil = 0 ,
    niltext = "" ,
    max name length = 80 ,
    begin entry char = ""0"" ,
    end entry char   = ""255"" ,
    nil entry        = ""0""255"" ,
    nil name         = "" ,
    quote            = """" ;

TEXT VAR entry ,
         dummy ;
INT VAR  cache index := 0 ,
         cache pos ;


TEXT PROC decode (INT CONST number) :

  dummy := "  " ;
  replace (dummy, 1, number) ;
  dummy .

ENDPROC decode ;

INT PROC decode (TEXT CONST string, INT CONST position) :

  subtext (string, position, position + 1) ISUB 1 .

ENDPROC decode ;

PROC access (THESAURUS CONST thesaurus, TEXT CONST name) :

  construct entry ;
  IF NOT cache identifies entry
    THEN search through thesaurus list
  FI ;
  IF entry found
    THEN cache index := decode (list, cache pos - 2)
    ELSE cache index := 0
  FI .

construct entry :
  entry := begin entry char ;
  entry CAT name ;
  decode invalid chars (entry, 2) ;
  entry CAT end entry char .

search through thesaurus list :
  cache pos := pos (list, entry) .

cache identifies entry :
  cache pos <> 0 AND
  pos (list, entry, cache pos, cache pos + LENGTH entry) = cache pos .

entry found :  cache pos > 0 .

list :  CONCR (thesaurus) .

ENDPROC access ;

PROC access (THESAURUS CONST thesaurus, INT CONST index) :

  IF cache identifies index
    THEN cache index := index ;
         construct entry
    ELSE cache pos := pos (list, decode (index) + begin entry char) ;
         IF entry found
           THEN cache pos INCR 2 ;
                cache index := index ;
                construct entry
           ELSE cache index := 0 ;
                entry := niltext
         FI
  FI .

construct entry :
  entry := subtext (list, cache pos, pos (list, end entry char, cache pos)) .

cache identifies index :
  subtext (list, cache pos-2, cache pos) = decode (index) + begin entry char .

entry found :  cache pos > 0 .

list :    CONCR (thesaurus) .

ENDPROC access ;



THESAURUS PROC empty thesaurus :

  THESAURUS : (""1""0"")

ENDPROC empty thesaurus ; 
 
 
OP := (THESAURUS VAR dest, THESAURUS CONST source ) : 
 
  CONCR (dest) := CONCR (source) .

ENDOP := ; 
 
TEXT VAR insert name ;

PROC insert (THESAURUS VAR thesaurus, TEXT CONST name, INT VAR index) : 
 
  insert name := name ;
  decode invalid chars (insert name, 1) ;
  insert name if possible .

insert name if possible :
  IF insert name = "" OR LENGTH insert name > max name length
    THEN index := nil ; errorstop ("Name unzulaessig")
  ELIF overflow
      THEN index := nil
      ELSE insert element
  FI .
 
overflow :
  LENGTH CONCR (thesaurus) + LENGTH insert name + 4 > max text length .

insert element :
  search free entry ;
  IF   entry found 
    THEN insert into directory 
    ELSE add entry to directory if possible
  FI .

search free entry :
  access (thesaurus, nil name) .

insert into directory :
  change (list, cache pos + 1, cache pos, insert name) ;
  index := cache index .

add entry to directory if possible :
  INT CONST next free index := decode (list, LENGTH list - 1) ;
  add entry to directory .

add entry to directory :
  list CAT begin entry char ;
  cache pos := LENGTH list ;
  cache index := next free index ;
  list CAT insert name ;
  list CAT end entry char + decode (next free index + 1) ;
  index := cache index .

entry found :   cache index > 0 .

list :          CONCR (thesaurus) .

ENDPROC insert ; 
 
PROC decode invalid chars (TEXT VAR name, INT CONST start pos) :

  INT VAR invalid char pos := pos (name, ""0"", ""31"", start pos) ;
  WHILE invalid char pos > 0 REP
    change (name, invalid char pos, invalid char pos, decoded char) ;
    invalid char pos := pos (name, ""0"", ""31"", invalid char pos)
  PER ;
  change all (name, ""255"", quote + "255" + quote) .

decoded char :  quote + text(code(name SUB invalid char pos)) + quote.

ENDPROC decode invalid chars ;

PROC insert (THESAURUS VAR thesaurus, TEXT CONST name) :

  INT VAR index ;
  insert (thesaurus, name, index) ;
  IF index = nil AND NOT is error
    THEN errorstop ("THESAURUS-Ueberlauf")
  FI .

ENDPROC insert ;

PROC delete (THESAURUS VAR thesaurus, TEXT CONST name, INT VAR index) : 
 
  access (thesaurus, name) ;
  index := cache index ;
  delete (thesaurus, index) .

ENDPROC delete ;

PROC delete (THESAURUS VAR thesaurus, INT CONST index) :

  access (thesaurus, index) ;
  IF entry found
    THEN delete entry
  FI .

delete entry :
  IF is last entry of thesaurus
    THEN cut off as much as possible
    ELSE set to nil entry
  FI .

set to nil entry :
  change (list, cache pos, cache pos + LENGTH entry - 1, nil entry) . 

cut off as much as possible :
  WHILE predecessor is also nil entry REP
    set cache to this entry
  PER ;
  list := subtext (list, 1, cache pos - 1) ;
  erase cache .

predecessor is also nil entry :
  subtext (list, cache pos - 4, cache pos - 3) = nil entry .

set cache to this entry :
  cache pos DECR 4 .

erase cache :
  cache pos := 0 ;
  cache index := 0 .

is last entry of thesaurus :
  pos (list, end entry char, cache pos) = LENGTH list - 2 .

list :     CONCR (thesaurus) .

entry found :   cache index > nil .

ENDPROC delete ;

 
BOOL OP CONTAINS (THESAURUS CONST thesaurus, TEXT CONST name ) : 
 
  IF   name = niltext OR LENGTH name > max name length
    THEN FALSE
    ELSE access (thesaurus, name) ; entry found
  FI . 
 
entry found :   cache index > nil . 
 
ENDOP CONTAINS ; 
 
PROC rename (THESAURUS VAR thesaurus, TEXT CONST old, new) :
 
  rename (thesaurus, link (thesaurus, old), new)

ENDPROC rename ;

PROC rename (THESAURUS VAR thesaurus, INT CONST index, TEXT CONST new) :

  insert name := new ;
  decode invalid chars (insert name, 1) ;
  IF overflow
    THEN errorstop ("THESAURUS-Ueberlauf")
  ELIF insert name = "" OR LENGTH insert name > max name length 
    THEN errorstop ("Name unzulaessig")
    ELSE change to new name
  FI .
 
overflow :
  LENGTH CONCR (thesaurus) + LENGTH insert name + 4 > max text length .

change to new name :
  access (thesaurus, index) ;
  IF cache index <> 0 AND entry <> ""
    THEN change (list, cache pos + 1, cache pos + LENGTH entry - 2, insert name)
  FI .

list :    CONCR (thesaurus) .

ENDPROC rename ;
 
INT PROC link (THESAURUS CONST thesaurus, TEXT CONST name) : 
 
  access (thesaurus, name) ;
  cache index .

ENDPROC link ; 
 
TEXT PROC name (THESAURUS CONST thesaurus, INT CONST index) : 
 
  access (thesaurus, index) ;
  subtext (entry, 2, LENGTH entry - 1) .

ENDPROC name ;

PROC get (THESAURUS CONST thesaurus, TEXT VAR name, INT VAR index) :

  identify index ;
  REP
    to next entry
  UNTIL end of list COR valid entry found PER .

identify index :
  IF index = 0
    THEN cache index := 0 ;
         cache pos   := 1
    ELSE access (thesaurus, index)
  FI .

to next entry :
  cache pos := pos (list, begin entry char, cache pos + 1) ;
  IF cache pos > 0
    THEN correct cache pos ;
         get entry
    ELSE get nil entry
  FI .

correct cache pos :
  IF (list SUB cache pos + 2) = begin entry char
    THEN cache pos INCR 2
  ELIF (list SUB cache pos + 1) = begin entry char
    THEN cache pos INCR 1
  FI .

get entry :
  cache index INCR 1 ;
  index := cache index ;
  name := subtext (list, cache pos + 1, end entry pos - 1) .

get nil entry :
  cache index := 0 ;
  cache pos := 0 ;
  index := 0 ;
  name := "" .

end entry pos :     pos (list, end entry char, cache pos) .

end of list :       index = 0 .

valid entry found : name <> "" .

list :              CONCR (thesaurus) .

ENDPROC get ;

INT PROC highest entry (THESAURUS CONST thesaurus) :              (*840813*)

  decode (list, LENGTH list - 1) - 1 .

list :   CONCR (thesaurus) .

ENDPROC highest entry ;

ENDPACKET thesaurus handling ;