aboutsummaryrefslogtreecommitdiff
path: root/src/cgi-bin/conj.scm4
blob: 253de1d5ee07f944f988e562aace33a5164d4588 (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
;;;; 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 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 (ellinika:translate-input 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 (show-conjugation:tense verb voice mood tense)
  
  #t)

(define (table-header count tense-names)
  (display "\
  <table class=\"frame align-center\">
   <thead class=\"std\">
   <tr>")
  (for-each
   (lambda (tense)
     (format #t "<th>~A</th>~%" tense))
   tense-names)
  (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 "," 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 (map car 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 (show-conjugation verb)
  (catch #t
	 (lambda ()
	   (for-each
	    (lambda (voice)
	      (show-conjugation:voice voice))
	    (conjugate-all verb)))
	 (lambda (key . args)
	   (case key
	     ((conjugator-error)
	      (let-optional
	       args
	       (subkey fmtstr fmtargs)
	       (case subkey
		 ((conjugator-error-input)
		  (error-message "Invalid input"))
		 (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 (do-conj)
  (let ((keyval (cgi:value "key")))
    (if (and keyval (not (string-null? keyval)))
	(show-conjugation (ellinika:translate-input keyval)))))

(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))))))
     
     (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.