File: listdict.scm

package info (click to toggle)
dico 2.2-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,492 kB
  • ctags: 10,331
  • sloc: ansic: 42,330; sh: 40,809; python: 5,400; lex: 2,022; tcl: 1,434; makefile: 1,112; yacc: 672; lisp: 461; awk: 300; perl: 175; php: 59; cpp: 44; fortran: 25; asm: 19; sed: 16; xml: 8
file content (150 lines) | stat: -rw-r--r-- 4,156 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
;;;; This file is part of GNU Dico.
;;;; Copyright (C) 2008, 2010, 2012, 2014 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 (listdict)
  #:use-module (guile-user)
;;  #:use-module (ice-9 syncase)
  #:use-module (ice-9 format)
  #:use-module (srfi srfi-1))

(define-syntax db:get
  (syntax-rules (info descr name lang corpus)
    ((db:get dbh name)
     (list-ref dbh 0))
    ((db:get dbh descr)
     (list-ref dbh 1))
    ((db:get dbh info)
     (list-ref dbh 2))
    ((db:get dbh lang)
     (list-ref dbh 3))
    ((db:get dbh corpus)
     (list-tail dbh 4))))

(define (open-module name . rest)
  (let ((args rest))
    (cond
     ((null? args)
      (format (current-error-port) "open-module: not enough arguments\n")
      #f)
     ((not (null? (cdr args)))
      (format (current-error-port) "open-module: too many arguments ~A\n" rest)
      #f)
     (else
      (let ((db (with-input-from-file
		    (car args)
		  (lambda () (read)))))
	(cond
	 ((list? db) (cons name db))
	 (else
	  (format (current-error-port) "open-module: ~A: invalid format\n"
		  (car args))
	  #f)))))))

(define (descr dbh)
  (db:get dbh descr))

(define (info dbh)
  (db:get dbh info))

(define (lang dbh)
  (db:get dbh lang))

(define (define-word dbh word)
  (let ((res (filter-map (lambda (elt)
			   (and (string-ci=? word (car elt))
				elt))
			 (db:get dbh corpus))))
    (and res (cons #t res))))

(define (match-exact dbh strat word)
  (filter-map (lambda (elt)
		(and (string-ci=? word (car elt))
		     (car elt)))
	      (db:get dbh corpus)))

(define (match-prefix dbh strat word)
  (filter-map (lambda (elt)
		(and (string-prefix-ci? word (car elt))
		     (car elt)))
	      (db:get dbh corpus)))

(define (match-suffix dbh strat word)
  (filter-map (lambda (elt)
		(and (string-suffix-ci? word (car elt))
		     (car elt)))
	      (db:get dbh corpus)))

(define (match-selector dbh strat key)
  (filter-map (lambda (elt)
		(and (dico-strat-select? strat (car elt) key)
		     (car elt)))
	  (db:get dbh corpus)))

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

(define match-default match-prefix)

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

(define (output rh n)
  (if (car rh)
      ;; Result comes from DEFINE command
      (let ((res (list-ref (cdr rh) n)))
	(cond
	 ((string=? (dico-current-markup) "html")
	  (format #t "<dt>~A</dt>~%<dd>~A</dd>"
		  (car res) (cdr res)))
	 (else
	  (display (car res))
	  (newline)
	  (display (cdr res)))))
      ;; Result comes from MATCH command
      (display (list-ref (cdr rh) n))))

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

(define (result-headers rh hdr)
  (list (cons "Content-Type" "text/plain; charset=UTF-8")
	(cons "Content-Transfer-Encoding" "quoted-printable")))

(define-public (listdict-init arg)
  (list (cons "open" open-module)
        (cons "descr" descr)
        (cons "info" info)
	(cons "lang" lang)
        (cons "define" define-word)
        (cons "match" match-word)
        (cons "output" output)
        (cons "result-count" result-count)
	(cons "result-headers" result-headers)))
;;
(dico-register-strat "suffix" "Match word suffixes")
(dico-register-markup "html")