aboutsummaryrefslogtreecommitdiff
path: root/src/ellinika/conjugator.scm
blob: 25ae2554f102841c287a3b7c6027782c2a1db4f2 (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
;;;; Modern Greek Verb Conjugator.
;;;; This file is part of Ellinika project.
;;;; Copyright (C) 2011 Sergey Poznyakoff
;;;;
;;;; Ellinika 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.
;;;;
;;;; Ellinika 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/>.

(define-module (ellinika conjugator))

(use-modules (srfi srfi-1)
	     (ellinika elmorph)
             (ellinika i18n)
	     (ellinika tenses)
	     (gamma sql))

(use-syntax (ice-9 syncase))

(define (my-sql-query conn query)
;  (format #t "Q: ~A~%" query)
  (let ((res (sql-query conn query)))
;    (format #t "R: ~A~%" res)
    res))

;; Verb info
;;  #:verb  - Verb in dictionary form
;;  #:conj  - Conjugation class
;;
;; Verb structure:
;;  (class verb flag assoc)
;; class      - Verb class
;; verb       - the verb itself
;; properties - associative list of properties
;; attested

(define (verb-set! verb key value)
;  (format #t "VERB ~A KEY ~A VALUE ~A~%" verb key value)
  (case key
    ((#:conj)
     (list-set! verb 0 value))
    ((#:verb)
     (list-set! verb 1 value))
    ((#:attested)
     (list-set! verb 3 (append (list-ref verb 3) (list value))))
    (else
     (let ((container (assoc key (list-ref verb 2))))
       (if container
	   (set-cdr! container value)
	   (list-set! verb 2 (append (list-ref verb 2)
				     (list
				      (cons key value)))))))))


(define (verb-get verb key)
  (case key
    ((#:conj)
     (list-ref verb 0))
    ((#:verb)
     (list-ref verb 1))
    ((#:attested)
     (list-ref verb 3))
    (else
     (assoc-ref (list-ref verb 2) key))))

   
(define (guess-verb-class verb)
  (cond
   ;; FIXME
   ((elstr-suffix? verb "άω")    "B1")
   ((elstr-suffix? verb "ώ")     "B2")
   ;; FIXME: deponentia?
   (else                         "A")))

(define (create-basic-verb-info conn verb proplist . rest)
;  (format #t "PROPLIST ~A~%" proplist)
  (let ((class (if (null? rest)
		   ""
		   (string-append " AND conj='" (car rest) "'"))))
    (let ((vdb (my-sql-query
		conn
		(string-append
		 "SELECT conj FROM verbclass WHERE verb='" (force-string verb) "'"
		 class))))
      (cond
       ((and vdb (not (null? vdb)));FIXME
	(list (caar vdb) verb proplist '(class)))
       ((elstr-suffix? verb "άω")
	(create-basic-verb-info conn (elstr-append
				      (elstr-trim verb -2) "ώ") "B1"))
       ((null? rest)
	(list (guess-verb-class verb) verb proplist '()))
       (else
	(list (car rest) verb '() '()))))))

(define (load-verb-info conn verb voice mood tense)
;  (format #t "LOAD ~A~%" verb)
  (let ((verbprop (my-sql-query
		   conn
		   (string-append
		    "SELECT property,value FROM verbtense WHERE "
		    "verb=\"" verb "\" AND voice=\"" voice
		    "\" AND mood=\"" mood "\" AND tense=\"" tense "\""))))
    (create-basic-verb-info conn verb
			    (if (null? verbprop)
				'()
				(map
				 (lambda (elt)
				   (let ((name (car elt))
					 (value (cadr elt)))
				     (if (string=? name "override")
					 (cons #:override
					       (string-split value #\,))
					 (cons (symbol->keyword
						(string->symbol name))
					       value))))
				 verbprop)))))
    
	
(define (thema-aoristoy-mesapathitikis-A root)
  (cond
   ((elstr-suffix? root "αίν")
    (elstr-append (elstr-trim root -3) "ανθ")) ;; FIXME: Also αθ, ηθ
   ((and
     (elstr-suffix? root "ν")
     (logand (elstr-char-prop-bitmask root -2) elmorph:vowel))
    (elstr-append (elstr-trim root -1) "θ")) ;; FIXME: also στ, νθ, θ           
   ((and
     (elstr-suffix? root "δ" "θ" "ζ" "ν") ;; FIXME: see above
     (logand (elstr-char-prop-bitmask root -2) elmorph:vowel))
    (elstr-append (elstr-trim root -1) "στ"))
   ((elstr-suffix? root "γγ" "σσ" "ττ" "χν" "γ" "ζ" "κ" "χ") =>
    (lambda (suf)
      (elstr-append (elstr-trim root (- 0 (elstr-length (string->elstr suf))))
		    "χτ"))) ;; also χθ
   ((elstr-suffix? root "π" "β" "φ" "πτ" "φτ") =>
    (lambda (suf)
      (elstr-append (elstr-trim root (- 0 (elstr-length (string->elstr suf))))
		    "φτ"))) ;; also φθ
   ((elstr-suffix? root "αύ" "εύ") =>
      (lambda (suf)
	(elstr-append root "τ")))
   ((elstr-suffix? root "άρ" "ίρ")
    ((elstr-append root "ιστ")))
   ((logand (elstr-char-prop-bitmask root -1) elmorph:vowel)
    (elstr-append root "θ"))
   (else
    #f)))

(define (thema-aoristoy-mesapathitikis-B root conj-aor)  
  (let ((root-aor (elstr-trim (list-ref conj-aor 0) -1)))
    (cond
     ((elstr-suffix? root-aor "σ")
      (elstr-append root
		    (elstr-slice root-aor -2 1)
		    "θ"))
     ((elstr-suffix? root-aor "ξ")
      (elstr-append root
		    (elstr-slice root-aor -2 1)
		    "χτ"))
     ((elstr-suffix? root-aor "ψ")
      (elstr-append root
		    (elstr-slice root-aor -2 1)
		    "φτ"))
     (else
      (elstr-append root "ηθ")))))

(define (lookup-verb-info conn verb voice thema)
  (my-sql-query
   conn
   (string-append
		 "SELECT root FROM irregular_root \
WHERE verb='" verb "' AND voice='" voice "' AND thema='" thema "'")))

(define (verb-A-root verb)
  (cond
   ((elstr-suffix? verb "ω")
    (elstr-trim verb -1))
   ((elstr-suffix? verb "ομαι")
    (elstr-trim verb -4))
   (else
    (error "cannot handle ~A~%" verb))))

(define (complement-verb-info conn vinfo verb voice thema)
;  (format #t "COMPLEMENT ~A~%" thema)
  (let ((elverb (string->elstr verb))
	(result (let ((tmpres (lookup-verb-info conn verb voice thema)))
		  (if (and (null? tmpres) (string=? thema "sub"))
		      (lookup-verb-info conn verb voice "aor")
		      tmpres))))
    (verb-set! vinfo #:root
	       (cond
		((not (null? result))
		 (verb-set! vinfo #:attested 'root)
		 (caar result))
		((string=? (verb-get vinfo #:conj) "A")
		 (let ((root (verb-A-root elverb)))
		   (cond
		    ((string=? thema "pres")
		     (verb-set! vinfo #:attested 'root)
		     root)
		    ((or (string=? thema "aor") (string=? thema "sub"))
		     (if (string=? voice "act")
			 (elstr-thema-aoristoy root)
			 (thema-aoristoy-mesapathitikis-A root)))
		    (else
		     #f))))
		((string=? (verb-get vinfo #:conj) "A-depon")
		 (let ((root (verb-A-root elverb)))
		   (cond
		    ((string=? thema "pres")
		     (verb-set! vinfo #:attested 'root)
		     root)
		    ((or (string=? thema "aor") (string=? thema "sub"))
		     #f) ; FIXME
		    (else
		     #f))))		 
		((string=? (verb-get vinfo #:conj) "B1")
		 (let ((root  (if (elstr-suffix? elverb "άω")
				  (elstr-trim elverb -2)
				  (elstr-trim elverb -1))))
		   (cond
		    ((or (string=? voice "act") (string=? thema "pres"))
		     (verb-set! vinfo #:attested 'root)
		     root)
		    ((or (string=? thema "aor") (string=? thema "sub"))
		     (thema-aoristoy-mesapathitikis-B
		      root
		      (list-ref
		       (conjugate conn verb "act" "ind" "Αόριστος")
		       0)))
		    (else
		     #f))))
		((string=? (verb-get vinfo #:conj) "B2")
		 (let ((root (elstr-trim elverb -1)))
		   (cond
		    ((or (string=? voice "act") (string=? thema "pres"))
		     (verb-set! vinfo #:attested 'root)
		     root)
		    ((or (string=? thema "aor") (string=? thema "sub"))
		     (elstr-append root "ηθ")) ;; FIXME: guesswork
		    (else
		     #f))))
		(else
		 #f)))))

(define-syntax conj-info
  (syntax-rules ()
    ((conj-info #:thema v)
     (list-ref v 0))
    ((conj-info #:suffix v)
     (list-ref v 1))
    ((conj-info #:accmap v)
     (list-ref v 2))
    ((conj-info #:particle v)
     (list-ref v 3))
    ((conj-info #:aux v)
     (list-ref v 4))
    ((conj-info #:auxtense v)
     (list-ref v 5))
    ((conj-info #:fold v)
     (list-ref v 6))
    ((conj-info #:flect v)
     (list-tail v 7))
    ((conj-info #:sing 1 v)
     (list-ref v 8))
    ((conj-info #:sing 2 v)
     (list-ref v 9))
    ((conj-info #:sing 3 v)
     (list-ref v 10))
    ((conj-info #:plur 1 v)
     (list-ref v 11))
    ((conj-info #:plur 1 v)
     (list-ref v 12))
    ((conj-info #:plur 1 v)
     (list-ref v 13))))

(define-syntax conj-info-set!
  (syntax-rules ()
    ((conj-info-set! #:particle v val)
     (list-set! v 3 val))
    ((conj-info-set! #:suffix v)
     (list-set! v 1 val))
    ((conj-info-set! #:accmap v)
     (list-set! v 2 val)) ))

(define (get-conj-info conn conj voice mood tense)
  (let ((answer (my-sql-query
		 conn
		 (string-append
      "SELECT c.thema,c.suffix,c.accmap,c.particle,c.aux,c.auxtense,c.fold AS fold,\
f.sing1,f.sing2,f.sing3,f.plur1,f.plur2,f.plur3 \
FROM conjugation c, verbflect f \
WHERE c.conj='" conj "' AND c.voice='" voice "' AND c.mood='" mood
"' AND c.tense='" tense "' AND c.flect = f.ident ORDER by fold"))))
    (if (null? answer)
	#f
	answer)))

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

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

(define (accented-syllable-0 str)
  (let ((syl (elstr-accented-syllable str))
	(len (elstr-number-of-syllables str)))
    (if (= syl 0)
	syl
	(+ (- len syl) 1))))

;; (define (get-property conj vinfo key default)
;;   (if ((override (verb-get vinfo
;; 			   (symbol->keyword
;; 			    (string->symbol
;; 			     (string-append
;; 			      (symbol->string (keyword->symbol key))
;; 			      "-override"))))))
;;       (if override
;; 	  (let ((t (conj-info key conj)))
;; 	    (if t
;; 		(or (verb-get vinfo key)
;; 		    t)
;; 		(or (verb-get vinfo key)
;; 		    (conj-info key conj)
;; 		    default))))))


(define (get-suffix conj vinfo)
  (let ((override (verb-get vinfo #:override)))
    (if (and override
	     (member "suffix" override))
	(let ((t (conj-info #:suffix conj)))
	  (if t
	      (or (verb-get vinfo #:suffix)
		  t)
	      ""))
	(or (verb-get vinfo #:suffix)
	    (conj-info #:suffix conj)
	    ""))))
	

(define (get-accmap conj vinfo)
  (let ((override (verb-get vinfo #:override)))
    (if (and override
	     (member "accmap" override))
	(let ((t (conj-info #:accmap conj)))
	  (if t
	      (or (verb-get vinfo #:accmap)
		  t)))
	(or (verb-get vinfo #:accmap)
	    (conj-info #:accmap conj)
	    "000000"))))
	
(define (apply-flect conj vinfo verb)
;  (format #t "VINFO ~A~%" vinfo)
  (let ((root (verb-get vinfo #:root))
	(suffix (get-suffix conj vinfo))
	(accmap (string->list (get-accmap conj vinfo)))
	(augment ""))
;    (format #t "ROOT ~A, ACCMAP ~S, SUFFIX: ~A~%" root accmap suffix)
    (cond
     ((> (length accmap) 6)
      (set! accmap (list-head accmap 6))
      (set! augment (or (verb-get vinfo #:augment) "ε"))))
;    (format #t "AUGMENT ~A ~A~%" vinfo (verb-get  vinfo #:augment))
    (let ((forms
	   (map
	    (lambda (flect acc)
	      (cond
	       ((not flect) #f)
	       ((char=? acc #\0)
		(let* ((rs (force-elstr root))
		       (suf (elstr-deaccent (elstr-append suffix flect)))
		       (result (elstr-append rs suf))
		       (nsyl (elstr-number-of-syllables result))
		       (acc-syl (+ (- nsyl 
				      (let ((n (accented-syllable-0 rs)))
					(if (= 0 n)
					    (accented-syllable-0 verb)
					    n))) 1)))
		  (cond
		   ((= nsyl 1)
		    (elstr-deaccent result))
		   ((> acc-syl 3)
		    (elstr-set-accent result 3)) ; FIXME 
		   (else
		    (elstr-set-accent result acc-syl)))))
	       ((char=? acc #\f)
		(elstr-append
		 (elstr-deaccent (elstr-append root suffix))
		 flect))
	       ((char=? acc #\s)
		(let ((nsyl (elstr-number-of-syllables flect)))
		  (elstr-set-accent
		   (elstr-append root suffix flect)
		   (if (< nsyl 2)
		       (+ nsyl 1)
		       3))))
	       ((char=? acc #\-)
		#f)
	       ((char-numeric? acc)
		(let ((num (- (char->integer acc) (char->integer #\0))))
		  (let ((obj (elstr-append
			      root suffix flect)))
		    (if (and augment (= (+ (elstr-number-of-syllables obj) 1)
					num))
			(set! obj (elstr-append augment obj)))
		    (let ((nsyl (elstr-number-of-syllables obj)))
		      (elstr-set-accent! obj (cond
					      ((< num nsyl) num)
					      ((< nsyl 3) nsyl)
					      (else 3)))
		      obj))))
	       (else
		(error "invalid accent character" acc))))
	    (conj-info #:flect conj)
	    accmap)))
      (if (conj-info #:particle conj)
	  (map
	   (lambda (w)
	     (if w
		 (string-append
		  (conj-info #:particle conj) " " (force-string w))
		 #f))
	   forms)
	  (map force-string forms)))))

(define (individual-verb conn verb voice mood tense)
  (let ((res (my-sql-query
	      conn
	      (string-append
	       "SELECT f.sing1,f.sing2,f.sing3,f.plur1,f.plur2,f.plur3 \
FROM individual_verb i,verbflect f \
WHERE i.verb='" verb "' AND i.voice='" voice "' AND i.mood='" mood
"' AND i.tense = '" tense "' AND i.ident=f.ident"))))
    (if (not (null? res))
	(append (car res)
		(list "I"
		      '(class root)))
	#f)))

(define (merge-conjugated-forms lista listb)
  (map
   (lambda (a b)
     (or a b))
   lista listb))

(define (conjugate conn verb voice mood tense . rest)
  (cond
   ((individual-verb conn verb voice mood tense) =>
    (lambda (res)
      (list res)))
   (else
    (let* ((vinfo (load-verb-info conn verb voice mood tense))
	   (conj-list (get-conj-info conn
				     (verb-get vinfo #:conj)
				     voice mood tense)))
      (if (not conj-list)
	  (list (list #f #f #f #f #f #f) #f #f)
	  (map car
	   (fold-right
	    (lambda (elt prev)
;	      (format #t "ELT ~A~%" elt)
	      (if (null? prev)
		  (list elt)
		  (let ((top (car prev)))
		    (if (let ((a (cdr elt))
			      (b (cdr top)))
			  (and (string? a) (string? b) (string=? a b)))
			(cons (cons
			       (merge-conjugated-forms (car top) (car elt))
			       (cdr top))
			      (cdr prev))
			(cons elt prev)))))
	    '()
	    (map
	     (lambda (conj)
;	       (format #t "CONJ ~S~%" conj)
	       (if (member #:nopart rest)
		   (conj-info-set! #:particle conj #f))
	       (cons
		(cond
		 ((string=? (conj-info #:thema conj) "synt")
		  (let* ((verb-conj
			  (car (conjugate conn verb voice "sub" "Αόριστος"
					  #:nopart)))
			 (form (list-ref verb-conj 2))
			 (part (conj-info #:particle conj)))
		    (cond
		     (form
;		      (format #t "FORM ~A FROM ~A~%" form verb-conj);;FIXME
		      (append
		       (map
			(lambda (aux flag)
			  (if (char=? flag #\-)
			      #f
			      (elstr->string
			       (if part
				   (elstr-append part " " aux " " form)
				   (elstr-append aux " " form)))))
			(conjugation:table
			 (car (conjugate conn
					 (conj-info #:aux conj) "act" "ind"
					 (conj-info #:auxtense conj))))
			(string->list (or (verb-get vinfo #:accmap)
					  (conj-info #:accmap conj)
					  "000000")))
		       (list (verb-get vinfo #:conj)
			     (conjugation:attested verb-conj))))
		     (else
		      #f))))
		 (else
		  (let ((thema (string-split (conj-info #:thema conj) #\:)))
;		    (format #t "THEMA ~A~%" thema)
		    (complement-verb-info conn vinfo verb
					  (if (null? (cdr thema))
					      voice
					      (car (cdr thema)))
					  (car thema))
;		    (format #t "VINFO ~A~%" vinfo)
		    (append (apply-flect conj vinfo verb)
			    (list (verb-get vinfo #:conj)
				  (verb-get vinfo #:attested))))))
		(conj-info #:fold conj)))
	     conj-list))))))))

(define-public (conjugator conn verb voice mood tense)
  (conjugate conn verb voice mood tense))

(define-public (conjugation:table conj)
  (cond
   ((not conj)
    #f)
   (else
    (list-head conj 6))))
       

(define-public (conjugation:class conj)
  (cond
   ((not conj)
    #f)
   (else
    (list-ref conj 6))))

(define-public (conjugation:attested conj)
  (cond
   ((not conj)
    #f)
   (else  (list-ref conj 7))))

(define-public (empty-conjugation? conj)
  (or
   (not conj)
   (call-with-current-continuation
    (lambda (return)
      (for-each
       (lambda (x)
	 (if x
	     (return #f)))
       conj)
      (return #t)))))

Return to:

Send suggestions and report system problems to the System administrator.