-
Notifications
You must be signed in to change notification settings - Fork 37
/
dict.lisp
1900 lines (1682 loc) · 83.8 KB
/
dict.lisp
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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; ichiran dictionary module
;; based on JMDict
(in-package #:ichiran/dict)
(defvar *jmdict-path* #p"foobar")
(defvar *jmdict-data* #p"foobar")
(load-settings :keep-connection t)
(defgeneric get-kana (obj)
(:documentation "most popular kana representation"))
(defgeneric get-kanji (obj)
(:documentation "most popular kanji representation"))
(defgeneric get-text (obj)
(:documentation "most popular text representation (kanji or kana)")
(:method (obj) (text obj)))
(defgeneric word-type (obj)
(:documentation "returns :kanji or :kana or :gap")
(:method (obj) :gap))
(defclass entry ()
((seq :reader seq :col-type integer :initarg :seq)
(content :reader content :col-type string :initarg :content)
(root-p :reader root-p :col-type boolean :initform nil :initarg :root-p)
(n-kanji :accessor n-kanji :col-type integer :initform 0 :initarg :n-kanji)
(n-kana :accessor n-kana :col-type integer :initform 0 :initarg :n-kana)
(primary-nokanji :reader primary-nokanji :col-type boolean :initform nil)
)
(:metaclass dao-class)
(:keys seq))
(deftable entry
(!dao-def))
(defmethod print-object ((obj entry) stream)
(print-unreadable-object (obj stream :type t :identity nil)
(format stream "~a ~a:~a" (seq obj) (n-kanji obj) (n-kana obj))))
(defmethod get-kana ((obj entry))
(text (car (select-dao 'kana-text (:and (:= 'seq (seq obj)) (:= 'ord 0))))))
(defmethod get-text ((obj entry))
(text (car (select-dao (if (> (n-kanji obj) 0) 'kanji-text 'kana-text)
(:and (:= 'seq (seq obj)) (:= 'ord 0))))))
(defmethod get-kanji ((obj entry))
(when (> (n-kanji obj) 0)
(text (car (select-dao 'kanji-text (:and (:= 'seq (seq obj)) (:= 'ord 0)))))))
(defun recalc-entry-stats (&rest entries)
(query (:update 'entry :set
'n-kanji (:select (:count 'id) :from 'kanji-text :where (:= 'kanji-text.seq 'entry.seq))
'n-kana (:select (:count 'id) :from 'kana-text :where (:= 'kana-text.seq 'entry.seq))
:where (:in 'entry.seq (:set entries)))))
(defun recalc-entry-stats-all ()
(query (:update 'entry :set
'n-kanji (:select (:count 'id) :from 'kanji-text :where (:= 'kanji-text.seq 'entry.seq))
'n-kana (:select (:count 'id) :from 'kana-text :where (:= 'kana-text.seq 'entry.seq)))))
(defun entry-digest (entry)
(list (seq entry) (get-text entry) (get-kana entry)))
(defclass simple-text ()
((conjugations :accessor word-conjugations :initform nil)
(hintedp :accessor hintedp :initarg :hintedp :initform nil)
))
(defmethod print-object ((obj simple-text) stream)
(print-unreadable-object (obj stream :type t :identity nil)
(format stream "~a ~a" (seq obj) (text obj))))
(defparameter *disable-hints* nil)
(defmethod get-kana :around ((obj simple-text))
(or (unless (or *disable-hints* (hintedp obj))
(let ((*disable-hints* t))
(get-hint obj)))
(call-next-method)))
(defclass kanji-text (simple-text)
((id :reader id :col-type serial)
(seq :reader seq :col-type integer :initarg :seq)
(text :reader text :col-type string :initarg :text)
(ord :reader ord :col-type integer :initarg :ord)
(common :reader common :col-type (or db-null integer) :initarg :common)
(common-tags :reader common-tags :col-type string :initform "" :initarg :common-tags)
(conjugate-p :reader conjugate-p :col-type boolean :initform t :initarg :conjugate-p)
(nokanji :reader nokanji :col-type boolean :initform nil :initarg :nokanji)
(best-kana :accessor best-kana :col-type (or db-null string) :initform :null :initarg :best-kana)
)
(:metaclass dao-class)
(:keys id))
(deftable kanji-text
(!dao-def)
(!index 'seq)
(!index 'ord)
(!index 'text)
(!index 'common)
(!foreign 'entry 'seq :on-delete :cascade))
(defmethod get-kanji ((obj kanji-text))
(text obj))
(defmethod get-kana ((obj kanji-text))
(let ((bk (best-kana-conj obj)))
(if (eql bk :null)
(get-kanji-kana-old obj)
bk)))
(defun get-kanji-kana-old (obj)
"old get-kana, used when everything else fails"
(loop with regex = (kanji-regex (text obj))
and kts = (select-dao 'kana-text (:= 'seq (seq obj)) 'ord)
for kt in kts
for tkt = (text kt)
if (ppcre:scan regex tkt) do (return tkt)
finally (return (text (car kts)))))
(defmethod word-type ((obj kanji-text)) :kanji)
(defclass kana-text (simple-text)
((id :reader id :col-type serial)
(seq :reader seq :col-type integer :initarg :seq)
(text :reader text :col-type string :initarg :text)
(ord :reader ord :col-type integer :initarg :ord)
(common :reader common :col-type (or db-null integer) :initarg :common)
(common-tags :reader common-tags :col-type string :initform "" :initarg :common-tags)
(conjugate-p :reader conjugate-p :col-type boolean :initform t :initarg :conjugate-p)
(nokanji :reader nokanji :col-type boolean :initform nil :initarg :nokanji)
(best-kanji :accessor best-kanji :col-type (or db-null string) :initform :null :initarg :best-kanji)
)
(:metaclass dao-class)
(:keys id))
(deftable kana-text
(!dao-def)
(!index 'seq)
(!index 'ord)
(!index 'text)
(!index 'common)
(!foreign 'entry 'seq :on-delete :cascade))
(defmethod get-kana ((obj kana-text))
(text obj))
(defmethod get-kanji ((obj kana-text))
(let ((bk (best-kanji-conj obj)))
(unless (eql bk :null) bk)))
(defmethod word-type ((obj kana-text)) :kana)
(defmethod common ((obj entry) &aux (seq (seq obj)))
(query (:select (:max 'common) :from (:as (:union
(:select 'common :from 'kanji-text :where (:= 'seq seq))
(:select 'common :from 'kana-text :where (:= 'seq seq))) 'tmp))
:single))
(defclass sense ()
((id :reader id :col-type serial)
(seq :reader seq :col-type integer :initarg :seq)
(ord :reader ord :col-type integer :initarg :ord))
(:metaclass dao-class)
(:keys id))
(deftable sense
(!dao-def)
(!index 'seq)
(!foreign 'entry 'seq :on-delete :cascade))
(defclass gloss ()
((id :reader id :col-type serial)
(sense-id :reader sense-id :col-type integer :initarg :sense-id)
(text :reader text :col-type string :initarg :text)
(ord :reader ord :col-type integer :initarg :ord)
)
(:documentation "English meaning")
(:metaclass dao-class)
(:keys id))
(defmethod print-object ((obj gloss) stream)
(print-unreadable-object (obj stream :type t :identity nil)
(format stream "(~a) ~a" (ord obj) (text obj))))
(deftable gloss
(!dao-def)
(!index 'sense-id)
(!foreign 'sense 'sense-id 'id :on-delete :cascade))
(defclass sense-prop ()
((id :reader id :col-type serial)
(tag :reader tag :col-type string :initarg :tag)
(sense-id :reader sense-id :col-type integer :initarg :sense-id)
(text :reader text :col-type string :initarg :text)
(ord :reader ord :col-type integer :initarg :ord)
(seq :reader seq :col-type integer :initarg :seq)
)
(:documentation "sense properties")
(:metaclass dao-class)
(:keys id))
(defmethod print-object ((obj sense-prop) stream)
(print-unreadable-object (obj stream :type t :identity nil)
(format stream "~a:~a" (tag obj) (text obj))))
(deftable sense-prop
(!dao-def)
(!index 'sense-id 'tag)
(!index 'tag 'text)
(!index 'seq 'tag 'text)
(!foreign 'entry 'seq :on-delete :cascade)
(!foreign 'sense 'sense-id 'id :on-delete :cascade))
(defclass restricted-readings ()
((id :reader id :col-type serial)
(seq :reader seq :col-type integer :initarg :seq)
(reading :reader reading :col-type string :initarg :reading)
(text :reader text :col-type string :initarg :text))
(:metaclass dao-class)
(:keys id))
(defmethod print-object ((obj restricted-readings) stream)
(print-unreadable-object (obj stream :type t :identity nil)
(format stream "~a -> ~a" (reading obj) (text obj))))
(deftable restricted-readings
(!dao-def)
(!index 'seq 'reading)
(!foreign 'entry 'seq :on-delete :cascade))
(defclass conjugation ()
((id :reader id :col-type serial)
(seq :reader seq :col-type integer :initarg :seq)
(from :reader seq-from :col-type integer :initarg :from)
(via :reader seq-via :col-type (or integer db-null) :initform :null :initarg :via)
)
(:documentation "conjugation link")
(:metaclass dao-class)
(:keys id))
(deftable conjugation
(!dao-def)
(!index 'seq)
(!index 'from)
(!foreign 'entry 'seq :on-delete :cascade)
(!foreign 'entry 'from 'seq :on-delete :cascade))
(defmethod print-object ((obj conjugation) stream)
(print-unreadable-object (obj stream :type t :identity nil)
(let ((via (seq-via obj)))
(if (eql via :null)
(format stream "~a -> ~a" (seq-from obj) (seq obj))
(format stream "~a -> ~a -> ~a" (seq-from obj) (seq-via obj) (seq obj))))))
(defclass conj-prop ()
((id :reader id :col-type serial)
(conj-id :reader conj-id :col-type integer :initarg :conj-id)
(conj-type :reader conj-type :col-type integer :initarg :conj-type)
(pos :reader pos :col-type string :initarg :pos)
(neg :reader conj-neg :col-type (or db-null boolean) :initarg :neg)
(fml :reader conj-fml :col-type (or db-null boolean) :initarg :fml))
(:metaclass dao-class)
(:keys id))
(deftable conj-prop
(!dao-def)
(!index 'conj-id)
(!foreign 'conjugation 'conj-id 'id :on-delete :cascade))
(defun conj-info-short (obj)
(format nil "[~a] ~a~@[~[ Affirmative~; Negative~]~]~@[~[ Plain~; Formal~]~]"
(pos obj)
(get-conj-description (conj-type obj))
(case (conj-neg obj) ((nil) 0) ((t) 1))
(case (conj-fml obj) ((nil) 0) ((t) 1))
))
(defun conj-prop-json (obj)
(let ((js (jsown:new-js
("pos" (pos obj))
("type" (get-conj-description (conj-type obj)))))
(neg (conj-neg obj))
(fml (conj-fml obj)))
(unless (or (not neg) (eql neg :null))
(jsown:extend-js js ("neg" neg)))
(unless (or (not fml) (eql fml :null))
(jsown:extend-js js ("fml" fml)))
js))
(defun delete-duplicate-props ()
(query "DELETE FROM conj_prop cp1 USING conj_prop cp2
WHERE cp1.id < cp2.id AND cp1.conj_id = cp2.conj_id
AND cp1.conj_type = cp2.conj_type
AND cp1.pos = cp2.pos
AND cp1.neg IS NOT DISTINCT FROM cp2.neg
AND cp1.fml IS NOT DISTINCT FROM cp2.fml"))
(defmethod print-object ((obj conj-prop) stream)
(print-unreadable-object (obj stream :type t :identity nil)
(princ (conj-info-short obj) stream)))
(defclass conj-source-reading ()
((id :reader id :col-type serial)
(conj-id :reader conj-id :col-type integer :initarg :conj-id)
(text :reader text :col-type string :initarg :text)
(source-text :reader source-text :col-type string :initarg :source-text)
)
(:metaclass dao-class)
(:keys id))
(deftable conj-source-reading
(!dao-def)
(!index 'conj-id 'text)
(!foreign 'conjugation 'conj-id 'id :on-delete :cascade))
(defmethod print-object ((obj conj-source-reading) stream)
(print-unreadable-object (obj stream :type t :identity nil)
(format stream "~a -> ~a" (source-text obj) (text obj))))
(defstruct conj-data seq from via prop src-map)
(defcache :no-conj-data *no-conj-data*
;; seq's that DON'T have conj data are calculated here
;; both because there's less of them, and it's generally safer when new conjs are added
(let ((no-conj-data (make-hash-table :size 200000)))
(dolist (seq (query (:select 'entry.seq :from 'entry
:left-join (:as 'conjugation 'c) :on (:= 'entry.seq 'c.seq)
:where (:is-null 'c.seq)) :column))
(setf (gethash seq no-conj-data) t))
no-conj-data))
(defun no-conj-data (seq)
(nth-value 1 (gethash seq (ensure :no-conj-data))))
(defun get-conj-data (seq &optional from/conj-ids texts)
"from/conj-ids can be either from which word to find conjugations or a list of conj-ids
texts is a string or list of strings, if supplied, only the conjs that have src-map with this text will be collected
"
(when (or (eql from/conj-ids :root) (no-conj-data seq))
(return-from get-conj-data nil))
(unless (listp texts)
(setf texts (list texts)))
(loop for conj in (cond
((null from/conj-ids)
(select-dao 'conjugation (:= 'seq seq)))
((listp from/conj-ids)
(select-dao 'conjugation (:and (:= 'seq seq) (:in 'id (:set from/conj-ids)))))
(t (select-dao 'conjugation (:and (:= 'seq seq) (:= 'from from/conj-ids)))))
for src-map = (if texts
(query (:select 'text 'source-text :from 'conj-source-reading
:where (:and (:= 'conj-id (id conj)) (:in 'text (:set texts)))))
(query (:select 'text 'source-text :from 'conj-source-reading
:where (:= 'conj-id (id conj)))))
when (or (not texts) src-map)
nconcing (loop for prop in (select-dao 'conj-prop (:= 'conj-id (id conj)))
collect (make-conj-data :seq (seq conj) :from (seq-from conj)
:via (let ((via (seq-via conj)))
(if (eql via :null) nil via))
:prop prop
:src-map src-map
))))
(defun get-original-text-once (conj-datas texts)
(unless (listp texts)
(setf texts (list texts)))
(unless (listp conj-datas)
(setf conj-datas (list conj-datas)))
(loop for conj-data in conj-datas
nconc (loop for (txt src-txt) in (conj-data-src-map conj-data)
if (find txt texts :test 'equal) collect src-txt)))
(defun get-original-text* (conj-datas texts)
(unless (listp texts)
(setf texts (list texts)))
(unless (listp conj-datas)
(setf conj-datas (list conj-datas)))
(loop for conj-data in conj-datas
nconc
(let ((src-text (loop for (txt src-txt) in (conj-data-src-map conj-data)
if (find txt texts :test 'equal) collect src-txt)))
(if (not (conj-data-via conj-data))
(mapcar (lambda (txt) (list txt (conj-data-from conj-data))) src-text)
(let ((new-cd (get-conj-data (conj-data-via conj-data) (conj-data-from conj-data))))
(get-original-text* new-cd src-text))))))
(defgeneric get-original-text (reading &key conj-data)
(:documentation "Returns unconjugated text(s) for reading")
(:method ((reading simple-text) &key conj-data)
(let ((orig-texts (get-original-text* (or conj-data (word-conj-data reading)) (text reading)))
(table (case (word-type reading) (:kanji 'kanji-text) (:kana 'kana-text))))
(loop for (txt seq) in orig-texts
nconc (select-dao table (:and (:= 'seq seq) (:= 'text txt)))))))
;;;;
(defprepared query-parents-kanji
(:select 'kt.id 'conj.id
:from (:as 'kanji-text 'kt)
(:as 'conj-source-reading 'csr)
(:as 'conjugation 'conj)
:where (:and
(:= 'conj.seq '$1)
(:= 'conj.id 'csr.conj-id)
(:= 'csr.text '$2)
(:= 'kt.seq (:case ((:not-null 'conj.via) 'conj.via)
(:else 'conj.from)))
(:= 'kt.text 'csr.source-text))))
(defprepared query-parents-kana
(:select 'kt.id 'conj.id
:from (:as 'kana-text 'kt)
(:as 'conj-source-reading 'csr)
(:as 'conjugation 'conj)
:where (:and
(:= 'conj.seq '$1)
(:= 'conj.id 'csr.conj-id)
(:= 'csr.text '$2)
(:= 'kt.seq (:case ((:not-null 'conj.via) 'conj.via)
(:else 'conj.from)))
(:= 'kt.text 'csr.source-text))))
(defun best-kana-conj (obj &aux (wc (word-conjugations obj)))
(cond ((and (or (not wc) (eql wc :root))
(not (eql (best-kana obj) :null)))
(best-kana obj))
(t (let* ((parents (query-parents-kanji (seq obj) (text obj))))
(loop for (pid cid) in parents
for parent-kt = (get-dao 'kanji-text pid)
for parent-bk = (best-kana-conj parent-kt)
unless (or (eql parent-bk :null) (and wc (or (eql wc :root) (not (find cid wc)))))
do (let ((readings (query (:select 'text :from 'conj-source-reading
:where (:and (:= 'conj-id cid)
(:= 'source-text parent-bk)))
:column)))
(when readings
(return
(if (= (length readings) 1)
(car readings)
(let ((km (kanji-cross-match (text parent-kt) parent-bk (text obj))))
(or (car (member km readings :test 'equal))
(loop with regex = (kanji-regex (text obj))
for rd in readings
if (ppcre:scan regex rd) do (return rd)
finally (return (car readings)))))))))
finally (return :null))))))
(defun best-kanji-conj (obj &aux (wc (word-conjugations obj)))
(cond ((and (or (not wc) (eql wc :root))
(not (eql (best-kanji obj) :null)))
(best-kanji obj))
((or (nokanji obj) (= (n-kanji (get-dao 'entry (seq obj))) 0))
:null)
(t (let* ((parents (query-parents-kana (seq obj) (text obj))))
(loop for (pid cid) in parents
for parent-bk = (best-kanji-conj (get-dao 'kana-text pid))
unless (or (eql parent-bk :null) (and wc (or (eql wc :root) (not (find cid wc)))))
do (let* ((readings (query (:select 'text :from 'conj-source-reading
:where (:and (:= 'conj-id cid)
(:= 'source-text parent-bk)))
:column))
(matching-readings
(some (lambda (reading) (and (kanji-match reading (text obj)) reading))
readings)))
(when matching-readings
(return matching-readings)))
finally (return :null))))))
;;;
;; > (query (:select (:max (:length 'text)) :from 'kana-text) :single)
;; 37
;; > (query (:select (:max (:length 'text)) :from 'kanji-text) :single)
;; 27
(defparameter *max-word-length* 50)
(defparameter *substring-hash* nil)
(defun find-word (word &key root-only)
(when (<= (length word) *max-word-length*)
(multiple-value-bind (inits present-p) (and *substring-hash* (gethash word *substring-hash*))
(if (and present-p (not root-only))
(loop for init in inits collect (apply 'make-instance init))
(let ((table (if (test-word word :kana) 'kana-text 'kanji-text)))
(if root-only
(query-dao table (:select 'wt.* :from (:as table 'wt) :inner-join 'entry :on (:= 'wt.seq 'entry.seq)
:where (:and (:= 'text word)
'root-p)))
(select-dao table (:= 'text word))))))))
(defun find-substring-words (str &key sticky)
(let ((substring-hash (make-hash-table :test 'equal))
kana-keys kanji-keys)
(loop
for start from 0 below (length str)
unless (member start sticky)
do (loop for end from (1+ start) upto (min (length str) (+ start *max-word-length*))
unless (member end sticky)
do (let ((part (subseq str start end)))
(setf (gethash part substring-hash) nil)
(if (test-word part :kana) (push part kana-keys) (push part kanji-keys)))))
(loop
for table in '(kana-text kanji-text)
for keys in (mapcar 'remove-duplicates (list kana-keys kanji-keys))
when keys
do (loop for kt in (query (:select '* :from table :where (:in 'text (:set keys))) :plists)
do (push (cons table kt) (gethash (getf kt :text) substring-hash))))
substring-hash))
(defun find-words-seqs (words seqs)
"generalized version of find-word-seq from dict-grammar"
(unless (listp words)
(setf words (list words)))
(unless (listp seqs)
(setf seqs (list seqs)))
(loop for word in words
if (test-word word :kana)
collect word into kana-words
else
collect word into kanji-words
finally
(let ((kw (when kanji-words (select-dao 'kanji-text (:and (:in 'text (:set kanji-words)) (:in 'seq (:set seqs))))))
(rw (when kana-words (select-dao 'kana-text (:and (:in 'text (:set kana-words)) (:in 'seq (:set seqs)))))))
(return (nconc kw rw)))))
(defun word-readings (word)
(let* ((kana-seq (query (:select 'seq :from 'kana-text :where (:= 'text word)) :column))
(readings
(if kana-seq (list word)
(let* ((kanji-seq (query (:select 'seq :from 'kanji-text
:where (:= 'text word)) :column)))
(query (:order-by
(:select 'text :from 'kana-text :where
(:in 'seq (:set kanji-seq)))
'id) :column)))))
(values readings (mapcar #'ichiran:romanize-word readings))))
;; Proxy text (kanji-text or kana-text with changed spelling)
(defclass proxy-text (simple-text)
((text :reader text :initarg :text)
(kana :reader get-kana :initarg :kana)
(source :reader source :initarg :source)))
(defgeneric true-text (obj)
(:documentation "Returns true text for reading")
(:method (obj) (text obj))
(:method ((obj proxy-text)) (true-text (source obj))))
(defgeneric true-kana (obj)
(:method (obj) (get-kana obj))
(:method ((obj proxy-text)) (true-kana (source obj))))
(defgeneric true-kanji (obj)
(:method (obj) (get-kanji obj))
(:method ((obj proxy-text)) (true-kanji (source obj))))
(defmethod word-conjugations ((obj proxy-text))
(word-conjugations (source obj)))
(defmethod (setf word-conjugations) (value (obj proxy-text))
(setf (word-conjugations (source obj)) value))
(defmethod seq ((obj proxy-text))
(seq (source obj)))
(defmethod common ((obj proxy-text))
(common (source obj)))
(defmethod ord ((obj proxy-text))
(ord (source obj)))
(defmethod nokanji ((obj proxy-text))
(nokanji (source obj)))
(defmethod word-type ((obj proxy-text))
(word-type (source obj)))
(defmethod get-original-text ((reading proxy-text) &key conj-data)
(get-original-text (source reading) :conj-data conj-data))
(defun find-word-as-hiragana (str &key exclude finder)
(let* ((as-hiragana (as-hiragana str))
(words (and (string/= str as-hiragana) (if finder
(funcall finder as-hiragana)
(find-word as-hiragana :root-only t)))))
(when words
(let ((str (copy-seq str)))
(loop for w in words
unless (find (seq w) exclude)
collect (make-instance 'proxy-text
:source w
:text str
:kana str))))))
;; Compound words (2 or more words squished together)
(defclass compound-text ()
((text :reader text :initarg :text)
(kana :reader get-kana :initarg :kana)
(primary :reader primary :initarg :primary)
(words :reader words :initarg :words)
(score-base :initform nil :initarg :score-base)
(score-mod :reader score-mod :initarg :score-mod)
))
(defmethod seq ((obj compound-text))
(mapcar #'seq (words obj)))
(defmethod print-object ((obj compound-text) stream)
(print-unreadable-object (obj stream :type t :identity nil)
(format stream "~a ~a" (seq obj) (text obj))))
(defmethod common ((obj compound-text))
(common (primary obj)))
(defmethod ord ((obj compound-text))
(ord (primary obj)))
(defmethod word-type ((obj compound-text)) (word-type (primary obj)))
(defgeneric adjoin-word (word1 word2 &key text kana score-mod score-base)
(:documentation "make compound word from 2 words"))
(defmethod adjoin-word :around (word1 word2 &key text kana score-mod score-base)
(call-next-method word1 word2
:text (or text (concatenate 'string (get-text word1) (get-text word2)))
:kana (or kana (concatenate 'string (get-kana word1) (get-kana word2)))
:score-mod (or score-mod 0)
:score-base score-base))
(defmethod adjoin-word ((word1 simple-text) (word2 simple-text) &key text kana score-mod score-base)
(make-instance 'compound-text
:text text :kana kana :primary word1 :words (list word1 word2)
:score-mod score-mod :score-base score-base))
(defmethod adjoin-word ((word1 compound-text) (word2 simple-text) &key text kana score-mod &allow-other-keys)
(with-slots ((s-text text) (s-kana kana) (s-words words) (s-score-mod score-mod)) word1
(setf s-text text s-kana kana
s-words (append s-words (list word2))
s-score-mod (funcall (if (listp s-score-mod) 'cons 'list) score-mod s-score-mod)))
word1)
(defgeneric word-conj-data (word)
(:documentation "conjugation data for word"))
(defmethod word-conj-data ((word simple-text))
(get-conj-data (seq word) (word-conjugations word) (true-text word)))
(defmethod word-conj-data ((word compound-text))
(word-conj-data (car (last (words word)))))
(defmethod word-conjugations ((word compound-text))
(word-conjugations (car (last (words word)))))
(defmethod (setf word-conjugations) (value (word compound-text))
(setf (word-conjugations (car (last (words word)))) value))
(defgeneric score-base (word)
(:method ((word compound-text))
(with-slots (score-base primary) word
(or score-base primary))))
(defstruct segment
start end word (score nil) (info nil) (top nil) (text nil))
(defmethod get-text ((segment segment))
(or (segment-text segment)
(setf (segment-text segment) (text (segment-word segment)))))
(defun length-multiplier (length power len-lim)
"len^power until len-lim, goes linear after"
(cond ((<= length len-lim) (expt length power))
(t (* length (expt len-lim (1- power))))))
(defparameter *length-coeff-sequences*
'((:strong 1 8 24 40 60)
(:weak 1 4 9 16 25 36)
(:tail 4 9 16 24)
(:ltail 4 12 18 24)))
(declaim (inline length-multiplier-coeff))
(declaim (ftype (function ((integer 0 10000) (member :strong :weak :tail :ltail)) (integer 0 120000)) length-multiplier-coeff))
(defun length-multiplier-coeff (length class)
(declare (optimize (speed 3) (debug 1) (safety 1)))
(let ((coeffs (assoc class *length-coeff-sequences*)))
(declare (type list coeffs))
(if (< 0 length (length coeffs))
(elt coeffs length)
(* length (the (integer 0 1000) (/ (the integer (car (last coeffs))) (1- (length coeffs))))))))
(defun kanji-break-penalty (kanji-break score &key info text use-length score-mod)
(let ((end (cond ((cdr kanji-break) :both)
((eql (car kanji-break) 0) :beg)
(t :end)))
(bonus 0) (ratio 2)
(posi (and info (getf info :posi))))
(when info
(cond ((or (intersection (getf info :seq-set) *no-kanji-break-penalty*)
;; do not penalize second word of で/す break
(and (eql end :beg) (alexandria:starts-with #\す text)))
(return-from kanji-break-penalty score))
((intersection '("vs-s" "v5s") posi :test 'equal)
;; these words have する endings which might lead to bad splits
(let ((suru-suffix (find :suru (get-suffixes text) :key 'second)))
(when suru-suffix
(let* ((offset (- (mora-length text) (mora-length (car suru-suffix))))
(suffix-score (calc-score (third suru-suffix)
:use-length (and use-length (- use-length offset))
:score-mod score-mod)))
(return-from kanji-break-penalty (min score (+ suffix-score 50)))))))
((and (eql end :beg) (member "num" posi :test 'equal))
(incf bonus 5))
((and (eql end :beg) (intersection
'("suf" "n-suf") posi :test 'equal))
(incf bonus 10))
((and (eql end :end) (member "pref" posi :test 'equal))
(incf bonus 12))
))
(if (>= score *score-cutoff*)
(max *score-cutoff* (+ (ceiling score ratio) bonus))
score)))
(defgeneric apply-score-mod (score-mod score len)
(:documentation "apply score-mod")
(:method ((score-mod integer) score len)
(* score score-mod len))
(:method ((score-mod function) score len)
(funcall score-mod score))
(:method ((score-mod list) score len)
(reduce '+ score-mod :key (lambda (sm) (apply-score-mod sm score len)))))
(defcache :is-arch *is-arch-cache*
(let* ((is-arch-cache (make-hash-table))
(a1 (query
(:select 'sense.seq :from 'sense
:left-join (:as 'sense-prop 'sp) :on (:and (:= 'sp.sense-id 'sense.id)
(:= 'sp.tag "misc")
(:in 'sp.text (:set "arch" "obsc" "rare")))
:group-by 'sense.seq :having (:every (:not-null 'sp.id)))
:column))
(a2 (query (:select (:distinct 'seq) :from 'conjugation :where (:in 'from (:set a1))) :column)))
(dolist (seq (union a1 a2))
(setf (gethash seq is-arch-cache) t))
is-arch-cache))
(defun is-arch (seq)
(nth-value 1 (gethash seq (ensure :is-arch))))
(defun get-non-arch-posi (seq-set)
(query
(:select 'sp1.text :distinct
:from (:as 'sense-prop 'sp1)
:left-join (:as 'sense-prop 'sp2)
:on (:and (:= 'sp1.sense-id 'sp2.sense-id)
(:= 'sp2.tag "misc")
(:in 'sp2.text (:set "arch" "obsc" "rare")))
:where (:and (:in 'sp1.seq (:set seq-set))
(:= 'sp1.tag "pos")
(:is-null 'sp2.id)))
:column))
;; *skip-words* *(semi-/non-)final-prt* *weak-conj-forms* *skip-conj-forms* are defined in dict-errata.lisp
(defun calc-score (reading &key final use-length (score-mod 0) kanji-break &aux ctr-mode)
(declare (optimize (speed 3) (debug 3) (safety 1)))
(declare (type (or null (integer 0 10000)) use-length))
(typecase reading
(compound-text
(let ((args (list (score-base reading)
:use-length (mora-length (text reading))
:score-mod (score-mod reading))))
(multiple-value-bind (score info) (apply 'calc-score args)
(setf (getf info :conj) (word-conj-data reading))
(when kanji-break
(setf score (apply 'kanji-break-penalty kanji-break score :info info :text (text (car args)) (cdr args))))
(return-from calc-score
(values score info)))))
(counter-text
(setf ctr-mode t)))
(let* ((score 1) (prop-score 0)
(kanji-p (eql (word-type reading) :kanji))
(katakana-p (and (not kanji-p) (> (the fixnum (count-char-class (true-text reading) :katakana-uniq)) 0)))
(text (text reading))
(n-kanji (count-char-class text :kanji))
;(kanji-prefix (kanji-prefix text))
(len (max 1 (the fixnum (mora-length text))))
(seq (the (or null fixnum) (seq reading)))
(ord (ord reading))
(entry (and seq (get-dao 'entry seq)))
(conj-only (let ((wc (word-conjugations reading))) (and wc (not (eql wc :root)))))
(root-p (or ctr-mode (and (not conj-only) (root-p entry))))
(conj-data (word-conj-data reading))
(secondary-conj-p (and conj-data
(or (every 'conj-data-via conj-data)
;; if this is nil, delete all secondary conjugations from conj data
(and (setf conj-data (delete-if 'conj-data-via (the cons conj-data))) nil))))
(conj-of (mapcar #'conj-data-from conj-data))
(conj-props (mapcar 'conj-data-prop conj-data))
(conj-types (mapcar 'conj-type conj-props))
(conj-types-p (or root-p use-length
(notevery (lambda (prop)
(test-conj-prop prop *weak-conj-forms*))
conj-props)))
(seq-set (and seq (cons seq conj-of))) ;;(if root-p (list seq) (cons seq conj-of)))
(sp-seq-set (if (and seq root-p (not use-length)) (list seq) seq-set))
(prefer-kana
(select-dao 'sense-prop (:and (:in 'seq (:set sp-seq-set))
(:= 'tag "misc") (:= 'text "uk"))))
(is-arch (every 'is-arch sp-seq-set))
(posi (if ctr-mode (list "ctr")
(get-non-arch-posi seq-set)))
(common (if conj-only :null (common reading)))
(common-of common)
(common-p (not (eql common :null)))
(particle-p (member "prt" posi :test 'equal))
(semi-final-particle-p (member seq *semi-final-prt*))
(non-final-particle-p (member seq *non-final-prt*))
(pronoun-p (member "pn" posi :test 'equal))
(cop-da-p (intersection seq-set *copulae*))
(long-p (> len
(cond
((and kanji-p (not prefer-kana)
(or (and root-p (not conj-data))
(and use-length (member 13 conj-types))))
2)
((and common-p (< 0 (the fixnum common) 10)) 2)
((and (intersection '(3 9) conj-types) (not use-length)) 4)
(t 3))))
(no-common-bonus (or particle-p
(not conj-types-p)
(and (not long-p) (equal posi '("int")))))
(primary-p nil)
(use-length-bonus 0)
split-info)
(declare (type (integer 0 1000000) score prop-score use-length-bonus)
(type (integer 0 10000) len ord n-kanji)
(type (or fixnum (eql :null)) common)
(type string text))
(when (or (intersection seq-set *skip-words*)
(and (not final) (member seq *final-prt*))
(and (not root-p) (skip-by-conj-data conj-data)))
(return-from calc-score 0))
(when (and conj-data (not (and (= ord 0) common-p)))
(let ((conj-of-data (loop for ot in (get-original-text reading :conj-data conj-data)
collect (list (common ot) (ord ot)))))
(when conj-of-data
(unless common-p
(let ((conj-of-common (mapcan (lambda (row) (unless (eql (car row) :null) (list (car row)))) conj-of-data)))
(declare (type list conj-of-common))
(when conj-of-common
(setf common 0 common-p t common-of (car (sort conj-of-common #'compare-common))))))
(let ((conj-of-ord (reduce 'min conj-of-data :key 'second)))
(declare (type fixnum conj-of-ord))
(when (< conj-of-ord ord) (setf ord conj-of-ord))))))
(unless is-arch
(setf primary-p
(or (not entry)
(and prefer-kana conj-types-p
(not kanji-p)
(or (not (primary-nokanji entry))
(nokanji reading)))
(and (or (= ord 0) cop-da-p)
(or kanji-p conj-types-p)
(or (and kanji-p (not prefer-kana))
(and common-p pronoun-p)
(= (the fixnum (n-kanji entry)) 0)))
(and prefer-kana kanji-p (= ord 0)
(not (query (:select 'id :from 'sense
:where (:and (:in 'id (:set (mapcar 'sense-id prefer-kana)))
(:= 'ord 0))))))
)))
(when primary-p
(incf score (cond (long-p 10)
((and secondary-conj-p (not kanji-p)) 2)
((and common-p conj-types-p) 5)
((or prefer-kana (not entry) (= (the fixnum (n-kanji entry)) 0)) 3)
(t 2))))
(when (and particle-p (or final (not semi-final-particle-p)))
(incf score 2)
(when common-p
(incf score (+ 2 len)))
(when (and final (not non-final-particle-p))
(cond (primary-p (incf score 5))
(semi-final-particle-p (incf score 2)))))
(when (and common-p (not no-common-bonus))
(let ((common-bonus
(cond
((and secondary-conj-p (not use-length)) (if (and kanji-p primary-p) 4 2))
((or long-p cop-da-p (and root-p (or kanji-p (and primary-p (> len 2)))))
(cond ((= common 0) 10)
((not primary-p) (max (- 15 common) 10))
(t (max (- 20 common) 10))))
(kanji-p 8)
(primary-p 4)
((or (> len 2) (< 0 common 10)) 3)
(t 2))))
(declare (type fixnum common-bonus))
(when (and (>= common-bonus 10) (find 10 conj-types))
(decf common-bonus 4))
(incf score common-bonus)))
(when long-p
(setf score (max len score)))
(when kanji-p
(setf score (max (if is-arch 3 5) score))
(when (and long-p (or (> n-kanji 1) (> len 4)))
(incf score 2)))
(when ctr-mode
(setf score (max 5 score)))
(setf prop-score score)
(setf score (* prop-score (+ (length-multiplier-coeff len (if (or kanji-p katakana-p) :strong :weak))
(if (> n-kanji 1) (* (1- n-kanji) 5) 0))))
(when use-length
(incf use-length-bonus
(* prop-score (length-multiplier-coeff (- use-length len)
(if (and (> len 3) (or kanji-p katakana-p)) :ltail :tail))))
(incf use-length-bonus
(the (integer 0 10000) (apply-score-mod score-mod prop-score (- use-length len))))
(incf score use-length-bonus))
(unless ctr-mode
(multiple-value-bind (split score-mod-split) (get-split reading conj-of)
(declare (type (or null (integer -10000 10000)) score-mod-split))
(cond
((member :score split)
(incf score score-mod-split)
(setf split-info score-mod-split))
((member :pscore split)
(let ((new-prop-score (max 1 (+ prop-score score-mod-split))))
(setf score (ceiling (* score new-prop-score) prop-score)
prop-score new-prop-score)))
(split
(setf score
(+ score-mod-split
(loop with nparts = (length split)
for part in split
for cnt of-type fixnum from 1
for last = (= cnt nparts)
for ptext of-type vector = (text part)
for plen of-type fixnum = (length ptext)
for slen of-type fixnum = plen then (+ slen plen)
for pmlen of-type (integer 0 10000) = (mora-length (text part))
for smlen of-type (integer 0 10000) = pmlen then (+ smlen pmlen)
for tpart = (if (and last (> slen (length text)))
(let ((new-len (max 1 (+ plen (- (length text) slen)))))
(make-instance 'proxy-text :source part :text (subseq ptext 0 new-len) :kana ""))
part)
for part-score = (calc-score tpart
:final (and final last)
:use-length (when (and last use-length)
(+ pmlen (- use-length smlen)))
:score-mod (if last score-mod 0))
collect part-score into part-scores
finally
(setf split-info (cons score-mod-split part-scores))
(return (the fixnum (reduce '+ part-scores))))))))))
(let ((info (list :posi posi :seq-set (if ctr-mode seq-set (cons seq conj-of))
:conj conj-data
:common (and common-p common-of)
:score-info (list prop-score kanji-break use-length-bonus split-info)
:kpcl (list (or kanji-p katakana-p) primary-p common-p long-p))))
(when kanji-break (setf score (kanji-break-penalty kanji-break score
:info info :text text :use-length use-length :score-mod score-mod)))
(values score info))))
(defun gen-score (segment &key final kanji-break)
(setf (values (segment-score segment) (segment-info segment))
(calc-score (segment-word segment) :final final :kanji-break kanji-break))
segment)
(defun find-sticky-positions (str)
"words cannot start or end after sokuon and before yoon characters"
(loop with modifiers = (append *modifier-characters* *iteration-characters*)
and str-len = (length str)
for pos from 0 below str-len
for char = (char str pos)
for char-class = (gethash char *char-class-hash* char)
if (and (eql char-class :sokuon)
(not (= pos (1- str-len)))
(let ((char (char str (1+ pos))))
(member (gethash char *char-class-hash* char) *kana-characters*))) collect (1+ pos)
else if (and (member char-class modifiers)
(not (and (= pos (1- str-len)) (eql char-class :long-vowel))))
collect pos))