summaryrefslogtreecommitdiffabout
path: root/src/cgi-bin/conj.scm4
blob: a8d84f1d1506b3e7c6068a02ced90a083a857597 (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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
;;;; Greek Dictionary Web Engine
;;;; Copyright (C) 2004, 2005, 2006, 2007, 2010, 2011 Sergey Poznyakoff
;;;; 
;;;; This program 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 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
;;;;

;;; Tailor this statement to your needs if necessary.
(set! %load-path (cons "GUILE_SITE" %load-path))

(use-modules ifelse(IFACE,[CGI],(www cgi),(guile-user))
	     (srfi srfi-1)
	     (ice-9 rdelim)
	     (ice-9 optargs)
	     (xmltools dict)
	     (ellinika config)
	     (ellinika elmorph)
	     (ellinika tenses)
	     (ellinika conjugator)
	     (ellinika sql)
             (ellinika i18n)
	     (ellinika xlat)
	     (ellinika cgi))

ifelse(IFACE,[CGI],(cgi:init))

(define conj-template-file-name "conj.html")

(ellinika-cgi-init conj-template-file-name)

(define (sql-error-handler key func fmt fmtargs data)
  (format #t "<h1 class=\"error\">~A</h1>\n"
	  (_ "ΣΦΆΛΜΑ: σύνδεση με το λέξικο απέτυχε."))
  (apply format (current-error-port) fmt fmtargs))

;;;
(define (dict-connect)
  (if (not ellinika:sql-conn)
      (ellinika:sql-connect ellinika-sql-connection)))


(define (main-form)
  (format #t "<form action=\"~A\" method=\"post\">"
	  (make-cgi-name cgi-script-name))
  (display "\
<table class=\"noframe\">
<tr>
 <td>")
  (display (_"Εισάγετε το ρήμα"))
  (display "
 </td>
 <td>
  <input size=\"36\" name=\"key\" tabindex=\"1\"")
  (let ((value (cgi:value "key")))
    (if value
	(begin
	  (display " value=\"")
	  (display (cgi-protect-quotes value))
	  (display "\""))))
  (display " />
 </td>
 <td>
  <input type=\"submit\" name=\"conjugate\" value=\"")
    (display (_"Κλίση"))
    (display "\" tabindex=\"2\" />
 </td>
</tr>
</table>
</form>
"))

(define tense-driver-list
  '(("ind" 3 5)
    ("sub" 3)
    ("imp" 3)))


(define unattested '()) ; List of unattested flags

(define (table-header count tenses)
  (display "\
  <table class=\"frame align-center\">
   <thead class=\"std\">
   <tr>")
  (for-each
   (lambda (tense)
     (let ((tense-name (car tense))
	   (att (fold
		 (lambda (conj prev)
		   (let ((att (conjugation:attested conj)))
		     (if att
			 (append att prev)
			 prev)))
		 '()
		 (cdr tense))))
       (display "<th>")
       (cond
       ((not (member 'stem att))
	(display "<a href=\"#stem-na\">?</a>&nbsp;")
	(if (not (member 'stem unattested))
	    (set! unattested (cons 'stem unattested)))))
       (display tense-name)
       (display "</th>")
       (newline)))
   tenses)
  (display "</tr></thead>"))

(define (table-footer)
  (display "</table>"))

(define (transpose mtx)
  (let* ((w (length (car mtx)))
	 (res (make-list w)))
    (do ((i 0 (1+ i)))
	((= i w))
      (list-set! res i (map
			(lambda (row)
			  (list-ref row i))
			mtx)))
    res))

(define (compact-conj-list conj)
  (map
   (lambda (x)
     (fold-right
      (lambda (elt prev)
	(if (member elt prev)
	    prev
	    (cons elt prev)))
      '()
      x))
   conj))

(define (concat-unique lst)
  (fold
   (lambda (elt prev)
     (if prev
	 (string-append prev ",<br>" elt)
	 elt))
   #f
   lst))


(define (format-tenses count tense-list voice mood)
  (let ((prosopa (if (string=? mood "imp")
		     '(2 5)
		     '(1 2 3 4 5 6))))
    (for-each
     (lambda (row pers class)
       (cond
	((member pers prosopa)
	 (format #t "<tr class=\"~A\">" class)
	 (for-each
	  (lambda (x)
	    (let ((val (concat-unique x)))
	      (format #t "<td>~A</td>" (if val val "--"))))
	  row)
	 (display "</tr>"))))
     (transpose
      (map
       (lambda (conj)
	 (compact-conj-list (transpose (map conjugation:table conj))))
       tense-list))
     '(1 2 3 4 5 6)
     '("odd" "even" "odd" "even" "odd" "even"))))

(define (show-conjugation:mood voice mood tense-list)
  (format #t "<div class=\"subsection\"><h3>~A</h3>"
	  (ellinika-conjugation-term mood))
  (for-each
   (lambda (count)
     (let ((tenses (list-head tense-list count)))
       (table-header count tenses)
       (format-tenses count (map cdr tenses) voice mood)
       (table-footer)
       (set! tense-list (list-tail tense-list count))
       (if (not (null? tense-list))
	   (display "<br/><br/>"))))
   (assoc-ref tense-driver-list mood))
  (display "</div>"))

(define (show-conjugation:voice voice)
  (format #t "<div class=\"section\"><h2>~A</h2>"
	  (ellinika-conjugation-term (car voice)))
  (for-each
   (lambda (mood-tenses)
     (show-conjugation:mood voice (car mood-tenses) (cdr mood-tenses)))
   (cdr voice))

  (display "</div>"))

(define (conjugate-all verb)
  (map
   (lambda (voice)
     (cons voice
	   (map
	    (lambda (mood-tenses)
	      (let ((mood (car mood-tenses)))
		(cons mood
		      (map
		       (lambda (tense)
			 (cons tense
			       (conjugator verb voice mood tense)))
		       (cdr mood-tenses)))))
	    ellinika-tense-list)))
   '("act" "pas")))

(define (force-string str)
  (if (elstr? str)
      (elstr->string str)
      str))

(define (error-message fmtstr . fmtargs)
  (display "<h2 class=\"error\">")
  (apply format #t fmtstr fmtargs)
  (display "</h2>"))

(define (class-attested? result)
  (call-with-current-continuation
   (lambda (return)
     (for-each
      (lambda (voice)
	(for-each
	 (lambda (mood)
	   (for-each
	    (lambda (tense-list)
	      (for-each
	       (lambda (tense)
		 (if (and (not (empty-conjugation? tense))
			  (conjugation:attested tense))
		     (return (member 'class (conjugation:attested tense)))))
	       (cdr tense-list)))
	    (cdr mood)))
	 (cdr voice)))
      result)
     (return #f))))
	  
(define (show-conjugation verb)
  (catch #t
	 (lambda ()
	   (let ((descr (ellinika:sql-query
			 "SELECT articles.meaning\
 FROM dict,articles\
 WHERE dict.word=~Q AND dict.ident=articles.ident\
  AND articles.lang=~Q AND (dict.pos & 1048576) = 1048576\
 ORDER BY articles.subindex\
 LIMIT 1"
			 verb
			 (language-code target-language))))
	     (cond
	      ((and descr (not (null? descr)))
	       (format #t "<h2 class=\"verb\"><a href=\"~A\">~A - ~A</a></h2>"
		       (make-cgi-name
			(cgi-program-name "dict") "ident" (dict:encode-string verb))
		       verb (caar descr)))
	      (else
	       (format #t
		       "<h2 class=\"verb\">~A - <span class=\"error\">(~A)</span></h2>"
		       verb
		       (_ "δεν βρέθηκε στο λέξικο")))))
	   (let ((result (conjugate-all verb)))
	     (if (not (class-attested? result))
		 (format #t
			 "<div class=\"unattested\"><p>~A</p></div>"
			 (_ "Η συζυγία αυτό του ρήματος δεν επιβεβαιώνεται από τη βάση δεδοµένων")))
	     (for-each
	      (lambda (voice)
		(show-conjugation:voice voice))
	      result)))
	 (lambda (key . args)
	   (case key
	     ((conjugator-error)
	      (let-optional
	       args
	       (subkey fmtstr fmtargs)
	       (case subkey
		 ((conjugator-error-input)
		  (error-message (_  "Μη έγκυρη είσοδος")))
		 (else
		  (error-message "CONJUGATOR ERROR: ~A ~A"
				 subkey (apply format #f fmtstr fmtargs))))))
	     ((misc-error)
	      (let-optional
	       args
	       (func-name fmtstr fmtargs sys-err)
	       (error-message "MISC ERROR in ~A: ~A"
			      func-name
			      (apply format #f fmtstr
				     (map force-string fmtargs)))))
	     (else
	      (error-message "OTHER ERROR: ~S ~S" key args))))))

(define (show-conjugation-simple verb)
  (for-each
   (lambda (voice)
     (show-conjugation:voice voice))
   (conjugate-all verb)))

(define (search-failure key)
  (display "<h2>")
  (format #t (_"Συγγνώμη, η λέξη \"~A\" δεν βρέθηκε στο λέξικο.") key)
  (display "</h2>"))

(define (display-cross-reference word)
  (display "<a href=\"")
  (display (make-cgi-name cgi-script-name "key" (dict:encode-string word)))
  (display "\">")
  (display word)
  (display "</a>"))

(define (show-best-matches key)
  (let ((result (ellinika:sql-query
		 "SELECT DISTINCT word\
 FROM dict\
 WHERE sound LIKE ~Q\
 AND (pos & 1048576) <> 0 ORDER BY 1"
		 (ellinika:sounds-like key))))
    (cond
     ((null? result)
      (search-failure key))
     ((= (length result) 1)
      (show-conjugation (caar result)))
     (else
      (format #t
	      "<div class=\"error\"><p>~A</p></div>"
	      (_ "Στην λέξη εισαγωγής δεν υπάρχει τόνος. Μήπος θέλατε να κλίσετε ένα απ'αυτά τα ρήματα:")) 
      (display "<table width=\"100%\" class=\"noframe\">")
      (let* ((result-length (length result))
	     (lim (1+ (quotient result-length match-list-columns))))
	(do ((i 0 (1+ i)))
	    ((= i lim) #f)
	  (display "<tr>")	  
	  (do ((j i (+ j lim)))
	      ((>= j result-length) #f)
	    (display "<td>")
	    (display-cross-reference (car (list-ref result j)))
	    (display "</td>"))
	  (display "</tr>")))	  
      (display "</table>")))))


(define (do-conj)
  (let ((keyval (cgi:value "key")))
    (if (and keyval (not (string-null? keyval)))
	(let ((input (ellinika:translate-input
		      (let ((keyval keyval))
			(cond
			 ((string-suffix? "o'" keyval)
			  (string-set! keyval
				       (- (string-length keyval) 2) #\v))
			 ((string-suffix? "o" keyval)
			  (string-set! keyval
				       (- (string-length keyval) 1) #\v)))
			keyval))))
	  (cond
	   ((not (elstr-suffix? input "ω" "ώ" "ομαι" "αμαι"))
	    (format #t
		    "<div class=\"error\"><p>~A</p></div>"
		    (_ "Αυτή η λέξη δεν είναι ρήμα στο πρώτο ενικό πρόσωπο της οριστικής του ενεστώτα.")))
	   ((= (elstr-accented-syllable input) 0)
	    (show-best-matches input))
	   (else
	    (show-conjugation input)))))))

(define (print-footnote id sign text)
  (format #t "<p><a name=\"~A\">~A</a>&nbsp;&nbsp;~A</p>~%"
	  id sign text))

(define (footnotes)
  (display "<div class=\"footnote\">")
  (for-each
   (lambda (flag)
     (case flag
       ((class)
	(print-footnote "class-na" "*"
			"Conjugation class of this verb is not attested"))
       ((root)
	(print-footnote "stem-na" "?"
			(_ "Το θέμα αυτού του χρόνου δεν επιβεβαιώνεται από τη βάση δεδοµένων")))))
   unattested)
  (display "</div>"))

(define (conj-html)
  (sql-catch-failure
   (let ((explist (list
		   (cons "@@args@@"
			 (lambda ()
			   (for-each
			    (lambda (name)
			      (cond
			       ((string=? name "lang"))
			       (else
				(let ((v (cgi:value name)))
				  (cond ((and v (not (string-null? v)))
					 (display "&amp;")
					 (display name)
					 (display "=")
					 (display v)))))))
			    (cgi:names))))
		   (cons "@@conj@@"
			 (lambda ()
			   (dict-connect)
			   (main-form)
			   (do-conj)
			   (if (not (null? unattested))
			       (footnotes)))))))
     
     (do ((line (read-line) (read-line)))
	 ((eof-object? line) #f)
       (expand-template explist line)
       (newline))
     (ellinika:sql-disconnect))))
  
;;; Main
ifelse(IFACE,[CGI],(display ["Content-type: text/html; charset=utf-8\r\n\r\n"]))

(with-input-from-file
    (template-file target-language conj-template-file-name)
  conj-html)


;;;; Local variables:
;;;; mode: Scheme
;;;; buffer-file-coding-system: utf-8
;;;; End:

Return to:

Send suggestions and report system problems to the System administrator.