File: glossary.scm

package info (click to toggle)
dico 2.9-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 16,908 kB
  • sloc: ansic: 85,886; sh: 50,207; lex: 2,624; yacc: 1,437; tcl: 1,434; python: 1,281; makefile: 1,266; perl: 881; lisp: 489; awk: 157; pascal: 125; cpp: 44; fortran: 25; asm: 19; sed: 16; xml: 8
file content (318 lines) | stat: -rw-r--r-- 7,616 bytes parent folder | download
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
;;;; This file is part of GNU Dico
;;;; Copyright (C) 2008-2019 Sergey Poznyakoff
;;;;
;;;; GNU Dico is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 3, or (at your option)
;;;; any later version.
;;;;
;;;; GNU Dico is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with GNU Dico.  If not, see <http://www.gnu.org/licenses/>.

(define-module (glossary)
  #:use-module (guile-user)
  #:use-module (srfi srfi-13)
  #:use-module (ice-9 getopt-long)
  #:use-module (ice-9 rdelim)
  #:use-module (ice-9 regex))

(define glossary-descr #f)

(define (ndict-error err . rest)
  (with-output-to-port
      (current-error-port)
    (lambda ()
      (display err)
      (for-each
       display
       rest)
      (newline))))

(define empty-line-re (make-regexp "^[ \t]*$" regexp/extended))
(define comment-re (make-regexp "#.*$" regexp/extended))
(define entry-re (make-regexp "([a-z][a-z])[ \t]*(.*)" regexp/extended))

(define (prep-line line)
  (cond
   ((not (string? line))
    line)
   ((regexp-exec comment-re line) =>
    (lambda (match)
      (regexp-substitute #f match 'pre)))
   (else
    line)))

(define (empty-line? line)
  (regexp-exec empty-line-re line))

(define (skip-newlines)
  (call-with-current-continuation
   (lambda (return)
     (do ((line (prep-line (read-line)) (prep-line (read-line))))
	 ((eof-object? line) #f)
       (if (not (empty-line? line))
	   (return line))))))

(define (read-entry)
  (let ((x (skip-newlines)))
    (if x
	(let ((entry '()))
	  (do ((line x (prep-line (read-line))))
	      ((or (eof-object? line)
		   (empty-line? line)) entry)
	    (cond
	     ((regexp-exec entry-re line) =>
	      (lambda (match)
		(set! entry (cons
			     (cons
			      (match:substring match 1)
			      (match:substring match 2))
			     entry))))
	     (else
	      (ndict-error "bad entry: " line)))))
	'())))

(define (read-file file)
  (let ((lst (list)))
    (with-input-from-file
	file
      (lambda ()
	(do ((entry (read-entry) (read-entry)))
	    ((null? entry))
	  (set! lst (cons entry lst)))))
    lst))

;;

(define (mapcan fun list)
  (apply (lambda ( . slist)
           (let loop ((elt '())
                      (slist slist))
             (cond
              ((null? slist)
               (reverse elt))
              ((not (car slist))
               (loop elt (cdr slist)))
              (else
               (loop (cons (car slist) elt) (cdr slist))))))
         (map fun list)))

(define (make-dict-article ent)
  (cond
   ((null? (cdr ent))
    (car ent))
   (else
    (let ((str ""))
      (do ((lst ent (cdr lst))
	   (n 1 (1+ n)))
	  ((null? lst))
	(set! str (string-append str "\n"
				 (number->string n) ". "
				 (car lst))))
      str))))

(define (make-dict data from to)
  (let ((dict '()))
    (for-each
     (lambda (entry)
       (let ((src (mapcan (lambda (x)
			    (if (string=? (car x) from)
				(cdr x)
				#f))
			  entry))
	     (dst (mapcan (lambda (x)
			    (if (string=? (car x) to)
				(cdr x)
				#f))
			  entry)))
	 (for-each (lambda (x)
		     (if (not (null? dst))
			 (set! dict (cons
				     (cons x (make-dict-article dst))
				     dict))
			 #f))
		   src)))
     data)
    dict))
	      
;; Callbacks

(define glossary-dict '())

(define (open-module name . rest)
  (let ((from #f)
	(to #f))
    (for-each (lambda (arg)
		(let ((av (string-split arg #\=)))
		  (case (length av)
		    ((2) (cond
			  ((string=? (car av) "from")
			   (set! from (cadr av)))
			  ((string=? (car av) "to")
			   (set! to (cadr av)))
			  (else
			   (glossary-error "Unknown option " (car av)))))
		    (else
		     (glossary-error "Unknown option " (car av))))))
	      (cdr rest))
    (cond
     ((not from)
      (glossary-error "from option is not given")
      #f)
     ((not to)
      (glossary-error "to option is not given")
      #f)
     (else
      (let ((dict (make-dict glossary-dict from to)))
	(cond
	 ((= (length dict) 0)
	  (glossary-error "No dictionary for " from "-" to " pair")
	  #f)
	 (else
;	  (write dict)
	  (list from to name dict))))))))

(defmacro glossary:src (dbh)
  `(list-ref ,dbh 0))

(defmacro glossary:dst (dbh)
  `(list-ref ,dbh 1))

(defmacro glossary:name (dbh)
  `(list-ref ,dbh 2))

(defmacro glossary:db (dbh)
  `(list-ref ,dbh 3))

(define (descr dbh)
  glossary-descr)

(define (info dbh)
  (string-append
   (glossary:name dbh)
   "\n"
   (or (descr dbh) "")
   " Headwords: "
   (number->string (length (glossary:db dbh))) "."
   "\n"
   "Copyright © 2008 Sergey Poznyakoff\n\
\n\
Permission is granted to copy, distribute and/or modify this document\n\
under the terms of the GNU Free Documentation License, Version 1.2 or\n\
any later version published by the Free Software Foundation; with no\n\
Invariant Sections, no Front-Cover and Back-Cover Texts"))

(defmacro rh:define? (rh)
  `(car ,rh))

(defmacro rh:key (rh)
  `(list-ref ,rh 1))

(defmacro rh:result (rh)
  `(list-ref ,rh 2))

(define (match-exact dbh strat key)
  (mapcan (lambda (word)
	    (and (string-ci=? (car word) key)
		 (car word)))
	  (glossary:db dbh)))

(define (match-prefix dbh strat key)
  (mapcan (lambda (word)
	    (and (string-prefix-ci? key (car word))
		 (car word)))
	  (glossary:db dbh)))

(define (match-suffix dbh strat key)
  (mapcan (lambda (word)
	    (and (string-suffix-ci? key (car word))
		 (car word)))
	  (glossary:db dbh)))

(define (match-selector dbh strat key)
  (mapcan (lambda (word)
	    (and (dico-strat-select? strat (car word) key)
		 (car word)))
	  (glossary:db dbh)))

(define strategy-list
  (list (cons "exact"  match-exact)
	(cons "prefix"  match-prefix)
	(cons "suffix"  match-suffix)))

(define (match-default dbh strat word)
  (match-prefix dbh strat word))

(define (match-word dbh strat key)
  (let ((sp (assoc (dico-strat-name strat) strategy-list)))
    (let ((res (cond
		(sp
		 ((cdr sp) dbh strat key))
		((dico-strat-selector? strat)
		 (match-selector dbh strat key))
		(else
		 (match-default dbh strat key)))))
      (if res
	  (list #f key res)
	  #f))))

(define (define-word dbh key)
  (let ((res (mapcan
	      (lambda (word)
		(if (string-ci=? (car word) key)
		    (cdr word)
		    #f))
	      (glossary:db dbh))))
    (if (null? res)
	#f
	(list #t key res))))

(define (output rh n)
  (let ((res (rh:result rh)))
    (cond
     ((rh:define? rh)
      (display (rh:key rh))
      (newline)
      (display (list-ref (rh:result rh) n)))
     (else
      (display (list-ref (rh:result rh) n))))))

(define (result-count rh)
  (length (rh:result rh)))

(define-public (glossary-init name)
  (list (cons "open" open-module)
	(cons "descr" descr)
	(cons "info" info)
	(cons "define" define-word)
	(cons "match" match-word)
	(cons "output" output)
	(cons "result-count" result-count)))

;; Main
(let ((grammar `((info (value #t)))))
  (for-each 
   (lambda (x)
     (cond
      ((pair? x)
       (case (car x)
	 ((info)
	  (set! glossary-descr (cdr x)))
	 ('()
	  (let ((arglist (cdr x)))
	    (cond
	     ((null? (cdr arglist))
	      (set! glossary-dict (read-file (car arglist))))
	     (else
	      (ndict-error "Not enough arguments")
	      (exit 1)))))))))
   (getopt-long (command-line) grammar)))

(dico-register-strat "suffix" "Match word suffixes")