aboutsummaryrefslogtreecommitdiff
path: root/cgi-bin/dict.cgi.in
blob: 7a2cc10dd92c982e839711275810ec4c86bbbde7 (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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
#!/usr/local/bin/guile -s
!#
;;;; Greek Dictionary Web Engine
;;;; Copyright (C) 2004 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 2 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, write to the Free Software
;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
;;;;

;; FIXME: These should be set by configure
(set! %load-path (append %load-path (list "/usr/local/share/guile-sql")))
(set! %load-path (append %load-path (list "/usr/local/share/guile-gettext")))
(use-modules (www cgi))
(use-modules (sql))
(use-modules (gettext))
(cgi:init)

;;; User-definable variables
(define base-dir "=PREFIX=")
(define html-dir "=HTMLDIR=")
(define sysconf-dir "=SYSCONFDIR=")
(define locale-dir "=LOCALEDIR=")
(define ref-loc #f)
  
(define dict-cgi-path "cgi-bin/dict.cgi")        ;; Path to the cgi (relative
                                                 ;; to the Base HREF)

(define config-file-name "ellinika.conf")
(define template-file-name "dict.html")
(define target-language "el_GR")

(define word-forms-reference '())

(define sql-iface "mysql")       ;; SQL interface ("mysql" or "postgres")
(define sql-host "localhost")    ;; SQL server hostname or a path to the UNIX
                                 ;; socket
(define sql-port 3306)           ;; SQL port number (0 for sockaddr_un
                                 ;; connection)
(define sql-database "ellinika") ;; Name of the database
(define sql-username "gray")     ;; Database user name
(define sql-password "")         ;; Password for that user name

(define match-list-columns 4)    ;; Number of colums in fuzzy search output
;;; End of user-definable variables


;;; Load the site defaults
(let ((rc-file (string-append sysconf-dir "/" config-file-name)))
  (if (file-exists? rc-file)
    (load rc-file)))

(define (language-code lang)
  (cond
   ((string-index lang #\_) =>
    (lambda (len)
      (substring lang 0 len)))
   (else
    lang)))

(define (template-file lang)
  (string-append html-dir "/" (language-code lang) "/" template-file-name))

;;; Load the language-specific defaults
(cond
 ((cgi:value "LANG") =>
  (lambda (x)
    (if (and (file-exists? (template-file x))
	     (false-if-exception (setlocale LC_ALL x)))
	(set! target-language x)))))

;;; Initialize i18n
(setlocale LC_ALL target-language)
(bindtextdomain "=PACKAGE=" locale-dir)
(bind_textdomain_codeset "=PACKAGE=" "UTF-8")
(textdomain "=PACKAGE=")

(define (make-cgi-name . rest)
  (apply
   string-append
   (cons
    dict-cgi-path
    (let ((arglist (let ((lang (cgi:value "LANG")))
		     (do ((ilist (if lang
				     (cons "LANG" (cons lang rest))
				     rest) (cdr ilist))
			  (i 1 (1+ i))
			  (olist '()))
			 ((null? ilist) (if (null? olist)
					    olist
					    (reverse (cdr olist))))
		       (set! olist (cons (car ilist) olist))
		       (set! olist (cons 
				    (if (odd? i) "=" "&")
				    olist))))))
      (if (null? arglist)
	  arglist
	  (cons "?" arglist))))))
  
;; Τα μέρη του λογου
(define part-of-speech
  (list (cons "κανένα μέρος του λόγου" #f)
        (cons "ρήμα"                   "(dict.pos=\"μετ.\" OR dict.pos=\"αμετ.\" OR dict.pos=\"μετ.,αμετ.\")") 
	(cons "μεταβατικό"            "dict.pos=\"μετ.\"")
	(cons "αμετάβατο"             "dict.pos=\"αμετ.\"")
	(cons "άρθρο"                  "dict.pos=\"άρθρο\"")
	(cons "αριθμός"                "dict.pos=\"αριθ.\"") 
	(cons "επίθετο"                "dict.pos=\"επίθ.\"") 
	(cons "επίρρημα"               "dict.pos=\"επίρρ.\"")  
	(cons "επιφώνημα"              "dict.pos=\"επιφ.\"")   
	(cons "μετοχή"                 "dict.pos=\"μετοχή\"")
	(cons "μόριο"                   "dict.pos=\"μόριο\"")
	(cons "πρόθεση"                "dict.pos=\"πρόθ.\"") 
	(cons "σύνδεσμος"              "dict.pos=\"σύνδ.\"") 
	(cons "ουσιαστικό"             "(dict.pos=\"ο\" OR dict.pos=\"η\" OR dict.pos=\"το\")")))

;; Protect occurences of " in a string.
;; Usual backslash escapes do not work in INPUT widgets, so I
;; change all quotation marks to "
;; Possibly not the better solution, though...
(define (protect string)
  (list->string
   (apply append
	  (map
	   (lambda (x)
	     (if (eq? x #\")
		 (list #\& #\# #\3 #\4 #\;)
		 (list x)))
	   (string->list string)))))

(define (get-topic-list)
  (let ((conn (sql-connect sql-iface sql-host sql-port sql-database
			   sql-username sql-password)))
    (cond
     (conn
      (let ((result (sql-query conn "SELECT ident,title FROM topic")))
	(sql-connect-close conn)
	result))
     (else
      #f))))


(define (main-form)
  (display "<FORM ACTION=\"")
  (display (make-cgi-name))
  (display "\" METHOD=POST>
<TABLE BORDER=0>
<TR>
 <TD>")
  (display (_"Εισάγετε τη λέξη"))
  (display "
 </TD>
 <TD>
  <INPUT size=36 NAME=\"key\" TABINDEX=\"1\"")
  (let ((value (cgi:value "key")))
    (if value
	(begin
	  (display "VALUE=\"")
	  (display (protect value))
	  (display "\""))))
  (display ">
 </TD>
</TR>
<TR>
 <TD>")

  (display (_"Επιλέξτε το μέρος του λόγου"))
  (display "</TD><TD>")
  
  (let ((selected-choice (or (let ((s (cgi:value "POS")))
			       (if s
				   (string->number s)
				   #f))
			     0))
	(index 0))
    
    (display "<SELECT NAME=\"POS\" TABINDEX=\"2\">")
    
    (for-each
     (lambda (x)
       (let ((name (car x)))
	 (display "<OPTION VALUE=")
	 (display index)
	 (if (= index selected-choice)
	     (display " selected"))
	 (display ">")
	 (display name)
	 (set! index (1+ index))))
     part-of-speech)
    (display "</SELECT>"))  

  (display "
 </TD> 
</TR>
<TR>
 <TD>")

  (display (_"Επιλέξτε το θέμα"))
  (display "</TD><TD>")
  (let ((topic-list (get-topic-list)))
    (if topic-list
	(let ((selected-choice (or (let ((s (cgi:value "TOPIC")))
				     (if s
					 (string->number s)
					 #f))
				   0)))
	  (display "<SELECT NAME=\"TOPIC\" TABINDEX=\"3\">")
	  (display "<OPTION VALUE=0>κανένα θέμα")
	  (for-each
	   (lambda (x)
	     (let ((id (car x))
		   (name (car (cdr x))))
	       (display "<OPTION VALUE=")
	       (display id)
	       (if (eq? (string->number id) selected-choice)
		   (display " selected"))
	       (display ">")
	       (display name)))
	   topic-list)
	  (display "</SELECT>"))))

  (display "
 </TD> 
</TR>
<TR>
 <TD colspan=\"2\" align=center>
  <INPUT TYPE=\"submit\" NAME=\"search\" VALUE=\"")
  (display (_"Αναζήτηση"))
  (display "\" TABINDEX=\"4\">
 </TD>
</TR>
</TABLE>
</FORM>
<P>"))


(define greek-postfix-map
  (list
   (cons #\: (list (cons "ι" "ϊ") (cons "υ" "ϋ")
		   (cons "ί" "ΐ") (cons "ύ" "ΰ")
		   (cons "θ" "ϊ") (cons "θ" "ϋ")))
   (cons #\' (list (cons "α" "ά") (cons "Α" "Ά")
		   (cons "ε" "έ") (cons "Ε" "Έ")
		   (cons "η" "ή") (cons "Η" "Ή")
		   (cons "ι" "ί") (cons "Ι" "Ί")
		   (cons "ϊ" "ΐ") (cons "Ϊ" "Ϊ")
		   (cons "ο" "ό") (cons "Ο" "Ό")
		   (cons "υ" "ύ") (cons "Υ" "Ύ")
		   (cons "θ" "ύ") (cons "Θ" "Ύ")
		   (cons "ϋ" "ΰ") (cons "Ϋ" "Ϋ")
		   (cons "ω" "ώ") (cons "Ω" "Ώ")))
   (cons #\s (list (cons "κ" "ξ") (cons "π" "ψ")))))

(define greek-kbd-map
  (list (cons #\a "α")
	(cons #\A "Α")
	(cons #\b "β")
	(cons #\B "Β")
	(cons #\g "γ")
	(cons #\G "Γ")
	(cons #\d "δ")
	(cons #\D "Δ")
	(cons #\e "ε")
	(cons #\E "Ε")
	(cons #\z "ζ")
	(cons #\Z "Ζ")
	(cons #\h "η")
	(cons #\H "Η")
	(cons #\u "θ")
	(cons #\U "Θ")
	(cons #\i "ι")
	(cons #\I "Ι")
	(cons #\k "κ")
	(cons #\K "Κ")
	(cons #\l "λ")
	(cons #\L "Λ")
	(cons #\m "μ")
	(cons #\M "Μ")
	(cons #\n "ν")
	(cons #\M "Ν")
	(cons #\j "ξ")
	(cons #\J "Ξ")
	(cons #\o "ο")
	(cons #\O "Ο")
	(cons #\p "π")
	(cons #\P "Π")
	(cons #\r "ρ")
	(cons #\R "Ρ")
	(cons #\s "σ")
	(cons #\S "Σ")
	(cons #\w "ς")
	(cons #\W "Σ")
	(cons #\t "τ")
	(cons #\T "Τ")
	(cons #\y "υ")
	(cons #\Y "Υ")
	(cons #\f "φ")
	(cons #\F "Φ")
	(cons #\x "χ")
	(cons #\X "Χ")
	(cons #\c "ψ")
	(cons #\C "Ψ")
	(cons #\v "ω")
	(cons #\V "Ω")))


(define (after-thita? c)
  (member c (list #\a #\e #\i #\o #\y #\v)))

;;; Given input string in Greek transliteration, convert it to
;;; an equivalent Greek word in UTF-8 encoding. The input string is
;;; supposed to follow the traditional Greek keyboard layout:
;;;
;;;     +----------------------------------------------------------------+
;;;     | 1! | 2@ | 3# | 4$ | 5% | 6^ | 7& | 8* | 9( | 0) | -_ | =+ | `~ |
;;;     +----------------------------------------------------------------+
;;;       | ·― | ςΣ | εΕ | ρΡ | τΤ | υΥ | θΘ | ιΙ | οΟ | πΠ | [{ | ]} |
;;;       +------------------------------------------------------------+
;;;        | αΑ | σΣ | δΔ | φΦ | γΓ | ηΗ | ξΞ | κΚ | λΛ | ΄¨ | '" | \| |
;;;        +-----------------------------------------------------------+
;;;          | ζΖ | χΧ | ψΨ | ωΩ | βΒ | νΝ | μΜ | ,; | .: | /? |
;;;          +-------------------------------------------------+
;;;		    +-----------------------------+
;;;		    |          space bar          |
;;;		    +-----------------------------+
;;;
;;;
;;; The followin escape sequences are recognized:
;;;
;;;   '\ks' -> 'ξ' 
;;;   '\ps' -> 'ψ'
;;;   '\th' -> 'θ'
;;;
;;; Additionally some spell fixing heuristics is applied:
;;;
;;;   's' at the end of the word -> 'ς'
;;;   'w' anywhere but at the end of the word -> 'ω'
;;;   'ks' -> 'ξ'
;;;   'ps' -> 'ψ'
;;;   "th" -> 'θ' unless followed by a consonant
;;;   "u'" -> 'ύ'
;;; 
;;; FIXME: The case of less obvious spelling errors, like e.g. 'ou' -> 'ου'
;;; will be handled by later spelling corrections if fuzzy search is
;;; enabled
(define (translate-kbd str)
  (apply
   string-append
   (do ((sl (string->list str) (cdr sl))
	(l '()))
       ((null? sl) (reverse l))
     (letrec ((decode-kbd-map
	       (lambda ()
		 (let ((g (assoc
			   (let ((c (car sl)))
			     (cond
			      ((and (char=? c #\w) (not (null? (cdr sl))))
			       #\v)
			      ((and (char=? c #\s) (null? (cdr sl)))
			       #\w)
			      (else
			       c)))
			   greek-kbd-map)))
		   (if g
		       (set! l (cons (cdr g) l))
		       (if (char=? (car sl) #\\)
			   (cond
			    ((> (length sl) 2)
			     (cond
			      ((char=? (car (cdr (cdr sl))) #\s)
			       (let ((c (car (cdr sl))))
				 (cond
				  ((char=? c #\k)
				   (set! sl (cdr (cdr sl)))
				   (set! l (cons "ξ" l)))
				  ((char=? c #\p)
				   (set! sl (cdr (cdr sl)))
				   (set! l (cons "ψ" l)))
				  (else
				   (set! sl (cdr sl))))))
			      ((and (char=? (car (cdr sl)))
				    (char=? (car (cdr (cdr sl))) #\h))
			       (set! sl (cdr (cdr sl)))
			       (set! l (cons "θ" l)))))

			    (else
			     (set! l (cons (string (car sl)) l))))))))))
       (if (null? l)
	   (decode-kbd-map)
	   (cond
	    ((char=? (car sl) #\h)
	     (if (and (not (null? (cdr sl)))
		      (after-thita? (car (cdr sl))))
		 (set-car! l "θ")
		 (decode-kbd-map)))
	    ((assoc (car sl) greek-postfix-map) =>
	     (lambda (cmap)
	       (let ((x (assoc (car l) (cdr cmap))))
		 (if x
		     (set-car! l (cdr x))
		     (decode-kbd-map)))))
	    (else
	     (decode-kbd-map))))))))
	       

;; Translate the input string to UTF-8 if necessary.
(define (translate-input input)
  (if (< (char->integer (string-ref input 0)) 127)
      (translate-kbd input)
      input))



(define transcription-list
  (list
   (cons  "μπ" "b" )
   (cons  "γγ" "g" )
   (cons  "γκ" "g" )
   (cons  "γχ" "g" )
   (cons  "ντ" "d" )
   (cons  "αι" "e" )
   (cons  "αί" "e" )
   (cons  "αυ" "au")
   (cons  "αύ" "au")
   (cons  "ου" "ou")
   (cons  "ού" "ou")
   (cons  "ευ" "eu")
   (cons  "εύ" "eu")
   (cons  "οι" "i" )
   (cons  "ει" "i" )
   (cons  "εί" "i" )
   (cons  "υι" "i" )
   
   (cons  "α" "a" )
   (cons  "Α" "a" )
   (cons  "Ά" "a" )
   (cons  "ά" "a" )
   (cons  "β" "b" )
   (cons  "Β" "b" )
   (cons  "γ" "g" )
   (cons  "Γ" "g" )
   (cons  "δ" "d" )
   (cons  "Δ" "d" )
   (cons  "ε" "e" )
   (cons  "Ε" "e" )
   (cons  "Έ" "e" )
   (cons  "έ" "e" )
   (cons  "ζ" "z" )
   (cons  "Ζ" "z" )
   (cons  "η" "i" )
   (cons  "Η" "i" )
   (cons  "Ή" "i" )
   (cons  "ή" "i" )
   (cons  "θ" "t" )
   (cons  "Θ" "t" )
   (cons  "ι" "i" )
   (cons  "Ι" "i" )
   (cons  "Ί" "i" )
   (cons  "ί" "i" )
   (cons  "κ" "k" )
   (cons  "Κ" "k" )
   (cons  "λ" "l" )
   (cons  "Λ" "l" )
   (cons  "μ" "m" )
   (cons  "Μ" "m" )
   (cons  "ν" "n" )
   (cons  "Ν" "n" )
   (cons  "ξ" "x" )
   (cons  "Ξ" "x" )
   (cons  "ο" "o" )
   (cons  "Ο" "o" )
   (cons  "Ό" "o" )
   (cons  "ό" "o" )
   (cons  "π" "p" )
   (cons  "Π" "p" )
   (cons  "ρ" "r" )
   (cons  "Ρ" "r" )
   (cons  "σ" "s" )
   (cons  "Σ" "s" )
   (cons  "ς" "s" )
   (cons  "τ" "t" )
   (cons  "Τ" "t" )
   (cons  "υ" "ι" )
   (cons  "Υ" "ι" )
   (cons  "Ύ" "ι" )
   (cons  "ύ" "ι" )
   (cons  "φ" "f" )
   (cons  "Φ" "f" )
   (cons  "χ" "h" )
   (cons  "Χ" "h" )
   (cons  "ψ" "P" )
   (cons  "Ψ" "P" )
   (cons  "ω" "o" )
   (cons  "Ω" "o" )
   (cons  "Ώ" "o" )
   (cons  "ώ" "o" )
   (cons  "Ϊ" "i" )
   (cons  "ΐ" "i" )
   (cons  "Ϋ" "i" )
   (cons  "ΰ" "i" )))

(define (greek-to-transcription str)
  (let ((len (string-length str)))
    (do ((i 0)
	 (sl '()))
	((= i len) (apply string-append (reverse sl)))
      (set! sl (cons 
		(cond
		 ((and (<= (+ i 4) len)
		       (assoc (substring str i (+ i 4)) transcription-list)) =>
		       (lambda (x) 
			 (set! i (+ i 4))
			 (cdr x)))
		 ((and (<= (+ i 2) len)
                       (assoc (substring str i (+ i 2)) transcription-list)) =>
		  (lambda (x)
		    (set! i (+ i 2))
		    (cdr x)))
		 (else
		  (set! i (1+ i))
		  (substring str (- i 1) i)))
		sl)))))
  



(define (encode-string str)
  (apply
   string-append
   (map
    (lambda (x)
      (let ((n (char->integer x)))
	(if (or (and (> n 97) (< n 122))
		(and (> n 65) (< n 90))
		(and (> n 48) (< n 57)))
	   (string x)
	   (let ((s (number->string n 16)))
	     (string-append "%"
			    (if (< n 16)
				"0" "")
			    s)))))
    (string->list str))))

(define (decode-string str)
  (do ((i 0)
       (sl '()))
      ((= i (string-length str)) (list->string (reverse sl)))
    (let ((c (string-ref str i)))
      (set! sl
	    (cons
	     (cond
	      ((char=? c #\%)
	       (set! i (+ i 3))
	       (integer->char
		(string->number (substring str (- i 2) i) 16)))
	      (else
	       (set! i (1+ i))
	       c))
	     sl)))))
	    
;;
(define (replace-tilde word sentence)
  (apply
   string-append
   (let loop ((lst '())
	      (str sentence))
     (cond
      ((string-index str #\~) =>
       (lambda (x)
	 (loop
	  (append lst (list (substring str 0 x) word))
	  (substring str (1+ x)))))
      ((string-null? str)
       lst)
      (else
       (append lst (list str)))))))

;;
(define (display-results rlist)
  (let ((x (car rlist)))
    (display "<TABLE BORDER=0>")
    (display "<TR><TD>")
    (display (car x))
    (display "</TD>")
    (cond
     ((list-ref x 3)
      (display "<TD>")
      (let ((href (assoc (list-ref x 2) word-forms-reference)))
	(cond
	 (href
	  (display "<A HREF=\"")
	  (cond
	   (ref-loc
	    (display ref-loc)
	    (display "/")))
	  (display (language-code target-language))
	  (display "/")
	  (display (cdr href))
	  (display (encode-string (car x)))
	  (display "\">")
	  (display (list-ref x 3))
	  (display "</A>"))
	 (else
	  (display (list-ref x 3)))))
      (display "</TD>")))
    (display "<TD>")
    (display (list-ref x 2))
    (display "</TD></TR>"))
  (for-each
   (lambda (x)
     (display "<TR><TD>")
     (display (1+ (string->number (list-ref x 4))))
     (display "</TD><TD>")
     (display (replace-tilde (car x) (list-ref x 5)))
     (display ";</TD></TR>"))
   rlist)
  (display "</TABLE>")
  (newline))

(define (display-cross-reference word)
  (display "<A HREF=\"")
  (display (make-cgi-name "IDENT" (encode-string word)))
  (display "\">")
  (display word)
  (display "</A>"))

(define (display-xref rlist text)
  (display text)
  (let ((n 0))
    (for-each
     (lambda (x)
       (if (> n 0)
	   (display ", "))
       (set! n (1+ n))
       (display-cross-reference (car x)))
     rlist))
  (display ";"))

(define (sort-result input-list)
  (let ((output-list '())
	(current-element '()))
    (for-each
     (lambda (x)
       (cond
	((or (null? current-element)
	     (=  (string->number (cadr x))
		 (string->number (cadr (car current-element)))))
	 (set! current-element (cons x current-element)))
	(else
	 (set! output-list (cons (reverse current-element) output-list))
	 (set! current-element (list x)))))
     input-list)
    (cons (reverse current-element) output-list)))


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

(define (fuzzy-search conn key theme pos)
  (let ((result (sql-query conn
			   (cond
			    ((> (string->number theme) 0)
			     (string-append
			      "SELECT dict.word FROM dict,topic_tab WHERE "
			      (cond
			       ((not (string-null? key))
				(string-append "dict.sound LIKE \""
					       (greek-to-transcription key)
					       "%\" AND "))
			       (else
				""))
			      "topic_tab.topic_ident="
			      theme
			      " AND topic_tab.word_ident=dict.ident "
			      (if (> (string->number pos) 0)
				  (let ((pos-entry
					 (list-ref part-of-speech (string->number pos))))
				    (string-append "AND "
						   (cdr pos-entry)))
				  "")
			      " order by word"))
			    (else
			     (string-append
			      "SELECT word FROM dict WHERE sound like \""
			      (greek-to-transcription key)
			      "%\" "
			      (if (> (string->number pos) 0)
				  (let ((pos-entry (list-ref part-of-speech
							     (string->number pos))))
				    (string-append "AND "
						   (cdr pos-entry)))
				  "")
			      " ORDER BY WORD"))))))
    (cond
     ((null? result)
      (search-failure key))
     (else
      (display "<TABLE WIDTH=\"100%\">")
      (let* ((result-length (length result))
	     (lim (1+ (inexact->exact (/ 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 (dict-search)
  (let ((keyval (if (cgi:value "IDENT")
		    (decode-string (cgi:value "IDENT"))
		    (cgi:value "key")))
	(theme (or (cgi:value "TOPIC") "0"))
	(pos (or (cgi:value "POS") "0")))
    (cond
     ((and keyval
	   (not (string-null? keyval))
	   (= (string->number theme) 0)
	   (= (string->number pos) 0))
      (let ((conn (sql-connect
		   sql-iface sql-host sql-port sql-database
		   sql-username sql-password)))
	(cond
	 ((not conn)
	  (format #t "<H1>~A</H1>\n"
		  "ΣΦΆΛΜΑ: σύνδεση με το λέξικο απέτυχε."))
	 (else
	  (display "<HR>")
	  (let* ((key (translate-input keyval))
		 (result (sql-query
			  conn
			  (string-append
			   "SELECT dict.word,dict.ident,dict.pos,dict.forms,articles.subindex,articles.meaning from dict,articles where dict.word=\""
			   key
			   "\" and dict.ident=articles.ident order by dict.ident, articles.subindex"))))
	    
	    (cond
	     ((null? result)
	      (fuzzy-search conn key theme pos))
	     (else
	      (for-each
	       (lambda (entry)
		 (display-results entry)
		 (let ((ant (sql-query
			     conn
			     (string-append
			      "SELECT dict.word FROM dict,antonym WHERE antonym.ident="
			      (cadr (car entry))
			      " AND dict.ident=antonym.antonym ORDER BY word"))))
		   (if (and ant (not (null? ant)))
		       (display-xref ant
				     (if (= (length ant) 1)
					 (_"Антоним: ") (_"Антонимы: ")))))
		 (display "<P>")
		 (let ((x (sql-query
			   conn
			   (string-append
			    "SELECT dict.word FROM dict,xref WHERE xref.ident="
			    (cadr (car entry))
			    " AND dict.ident=xref.xref ORDER BY word"))))
		   (if (and x (not (null? x)))
		       (display-xref x (_"См. также ")))))
	       (sort-result result))))
	    (sql-connect-close conn))))))
     ((or (> (string->number theme) 0) (> (string->number pos) 0))
      (let ((conn (sql-connect
		   sql-iface sql-host sql-port sql-database
		   sql-username sql-password)))
	(cond
	 ((not conn)
	  (format #t "<H1></H1>\n"
		  (_"ΣΦΆΛΜΑ: σύνδεση με το λέξικο απέτυχε.")))
	 (else
	  (display "<HR>")
	  (fuzzy-search conn "" theme pos)
	  (sql-connect-close conn))))))))

;;;

(define (dict-html)
  (do ((line (read-line) (read-line)))
      ((eof-object? line) #f)
    (cond
     ((string=? line "@@dict@@")
      (main-form)
      (dict-search))
     (else
      (display line)
      (newline)))))  
  
;;; Main
(display "Content-type: text/html; charset=utf-8\r\n\r\n")

(with-input-from-file
    (template-file target-language)
  dict-html)


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

Return to:

Send suggestions and report system problems to the System administrator.