-
Notifications
You must be signed in to change notification settings - Fork 2
/
yomikun.el
1647 lines (1423 loc) · 50.5 KB
/
yomikun.el
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
;;; -*- lexical-binding: t; -*-
;; -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2023 daniel german ([email protected])
;; overlay code based on https://github.com/katspaugh/kuromoji.el
;; used with permission
;; Author: Daniel M German ([email protected])
;; Created: May 1, 2023
;; SPDX-License-Identifier: GPL-3.0-or-later
;; Version: 0.2
;; Homepage: https://github.com/dmgerman/yomikun
;; Package-Requires: ((emacs "27.1") )
(require 'pos-tip)
(require 'emacsql)
(defvar yk-mecab-command "echo" "Shell command to run on buffer contents")
(defvar yk-process-name "jp-process")
;; string used by emacs to delimit end of process
(defvar yk-process-end-st "\nProcess")
(defvar yk-db-status-file "~/yk-status.db")
(defvar yk-db-dict-file nil)
(defvar yk-max-tokens-to-process 10000)
(defvar yk-process-buffer "*yk-process*" "Name of buffer for shell command process")
(defvar yk-report-buffer "*yk-report*" "Name of buffer for report")
(defvar yk-date-format "%Y-%m-%d %H:%M" "Format to use when logging to the status database")
(defvar yk-db-dict nil) ;; instance of the quick dictionary db
(defvar yk-db-status nil) ;; instance of the status db
;; TODO move my own configuration somewhere else
(setq yk-mecab-command "/Users/dmg/bin/osx/m-mecab.sh")
(setq yk-max-tokens-to-process 100000)
(setq yk-db-status-file "~/jp-status.db")
(setq yk-db-dict-file "~/dictionary.db")
(defun yk-db-status-open ()
(interactive)
(when (not (file-exists-p yk-db-status-file))
(signal 'file-error (format "the file does not exist [%s]" yk-db-status-file))
)
;; only open it if it is not open
(when (not yk-db-status)
(setq yk-db-status (emacsql-sqlite yk-db-status-file)))
)
(defun yk-db-dict-open ()
(interactive)
(when (not (file-exists-p yk-db-dict-file))
(signal 'file-error (format "the dictionary file does not exist [%s]" yk-db-dict-file))
)
;; only open it if it is not open
(when (not yk-db-dict)
(setq yk-db-dict (emacsql-sqlite yk-db-dict-file)))
)
(defun yk-db-dict-close ()
(emacsql-close yk-db-dict)
)
(defun yk-db-status-close()
(emacsql-close yk-db-status)
)
(defun yk-get-time-date ()
(format-time-string yk-date-format (current-time))
)
(defun yk-db-status-create ()
"Create status database"
(interactive)
(message "creating status database at [%s]" yk-db-status-file)
(when (file-exists-p yk-db-status-file)
(signal 'file-error (format "the file already exist [%s]" yk-db-status-file))
)
(setq yk-db-status (emacsql-sqlite yk-db-status-file))
(emacsql yk-db-status [:create-table words ([morph mtype surface status date]
(:primary-key [morph mtype surface])
)])
)
(defun yk-db-dict-def (root pronun wtype)
;; try to fin all markers... if not, match
(let* (
;; search first with wtype
(with-all (nth 0
(emacsql yk-db-dict [:select [gloss pos root reading wtype]
:from entries
:where (and (= root $s1) (= reading $s2)
(= wtype $s3))
:limit 1
] root pronun wtype) ))
;; if not found search without it
(without-type (or with-all
(nth 0
(emacsql yk-db-dict [:select [gloss pos root reading wtype]
:from entries
:where (and (= root $s1) (= reading $s2) )
:limit 1
] root pronun) )))
(result (or without-type
(nth 0
(emacsql yk-db-dict [:select [gloss pos root reading wtype]
:from entries
:where (= root $s1)
:limit 1
] root ) )))
)
result
)
)
;; this is a memoization of the compounds queries
;; it does not seem to make an impact worth the memory it uses
;; currently unused
(setq yk-compound-candidates-table (make-hash-table :test 'equal))
(defun yk-compound-prefix-candidates (st)
(or (gethash st yk-compound-candidates-table)
(let
(
(comps (yk-db-compound-prefix-candidates st))
)
(puthash st
comps
yk-compound-candidates-table)
)
)
)
(defun yk-db-compound-prefix-candidates (st)
"Return a list of compounds that have as prefix the string st"
(when (> (length st) 2)
(setq st (concat st "%"))
;; (message " searhing for candidates [%s]" st)
;; remove the list wrapping each string
(mapcar
(lambda (ls) (nth 0 ls))
(emacsql yk-db-dict [:select [compound]
:from compounds
:where (like compound $s1)
:order-by [(desc (length compound))]
] st)
)
)
)
(defun yk-db-compound-exists (st)
"Check if a given compound exists"
(and
(emacsql yk-db-dict [:select [compound]
:from compounds
:where (= compound $s1)
] st)
)
)
;; hash used for memoization of dictionary lookups
;; currently unused
(setq yk-dict-table (make-hash-table :test 'equal))
(defun yk-dict-def (root pronun wtype)
(or (gethash (list root pronun wtype) yk-dict-table)
(let
(
(def (yk-db-dict-def root pronun wtype))
)
(puthash (list root pronun wtype)
def
yk-dict-table)
)
)
)
(defun yk-db-morph-status-get (root wtype surface)
"return the tuple that matches the given root wtype and
surface. The three attribuetes are the primary key, so
return match as a list or nil if not found
"
;; since it is a singleton, remove wrapping list
;; nth returns nil if the list is empty
;; (message "get db [%s %s %s]" root wtype surface)
(or (nth 0;
(emacsql yk-db-status [:select [morph mtype status date]
:from words
:where (and (= morph $s1) (= mtype $s2) (= surface $s3))
] root wtype surface)
)
(list nil nil "unknown" nil))
)
(defun yk-db-morph-status-delete (root wtype surface)
"delete given morph, if it exists"
(emacsql yk-db-status [:delete
:from words
:where (and (= morph $s1) (= mtype $s2) (= surface $s3))
] root wtype surface)
)
(defun yk-db-morph-status-insert (root wtype surface status)
"insert new tuple"
(let (
(today (yk-get-time-date))
)
;
(emacsql yk-db-status [:insert :into words
:values ([$s1 $s2 $s3 $s4 $s5])] root wtype surface status today)
))
(defun yk-db-morph-status-update (root wtype surface status)
"update new tuple by deleting it, then inserting"
(yk-db-morph-status-delete root wtype surface)
(if (not (string-equal status "unknown"))
(yk-db-morph-status-insert root wtype surface status))
)
;; initialize hash table
(setq yk-status-table (make-hash-table :test 'equal))
;; counter for profiling purposes
(setq yk-repeat-counter 0)
(defun yk-morph-status-get (root wtype surface)
"memoized function that checks for the status of a given morph."
;; memoize the status of the given morph
(or (gethash (list root wtype surface) yk-status-table)
(let
(
(status (nth 2
(yk-db-morph-status-get root wtype surface)))
)
(setq yk-repeat-counter (+ yk-repeat-counter 1))
; (message "gone to the db [%s]" status)
(puthash (list root wtype surface)
status
yk-status-table)
)
))
(defun yk-morph-status-set (root wtype surface status)
"stores new status in db if needed, updating memoization table"
; (message "entering")
(let(
(curval (yk-morph-status-get root wtype surface))
)
(unless (and curval
(string-equal curval status)
)
;; cache does not have it or it is different
(yk-db-morph-status-update root wtype surface status)
(puthash (list root wtype surface) status yk-status-table)
)
))
(defun yk-katakana-to-hiragana (str)
"Convert katakana to hiragana. Very rough, but it works"
(mapconcat
(lambda (c)
(if (and (>= c #x30a1) (<= c #x30f6))
(char-to-string (+ c (- #x3041 #x30a1)))
(char-to-string c)))
str ""))
(defun yk-do-region (beg end)
"jk-ize the region"
(interactive "r")
;; remove current info in region
(yk-remove-props-and-overlays beg end)
(if (bufferp yk-process-buffer)
(kill-buffer yk-process-buffer))
;; do the work
(yk-process-region beg end)
)
(defun yk-do-buffer ()
"jk-ize the region"
(interactive)
(yk-do-region (point-min) (point-max))
)
;;;;;;;;;;;;;faces
(defface yk-face-unknown
'((t ( :background "misty rose"
)))
"Face for default unknown text"
:group 'yomikun)
(defface yk-face-learning
'((t ( :background "#EAFFEA"
)))
"Face for default learning text"
:group 'yomikun)
(defface yk-face-ignore
'((t ( :background "gray95"
)))
"Face for default learning text"
:group 'yomikun)
(defface yk-face-compound
'((t ( :underline (:color "red" :style wave :position 3)
)))
"Face for default learning text"
:group 'yomikun)
;; for grammatical entities
(defface yk-face-noun
'((t (:inherit font-lock-string-face
)))
"Face for noun unknown"
:group 'my
)
(defface yk-face-noun-alt
`((((class color) (background light))
(:foreground "darkblue"))
(((class color) (background dark))
(:foreground "darkblue")))
"Face for nouns alt."
:group 'yomikun)
(defface yk-face-verb
'((t (:inherit font-lock-keyword-face
)))
"Face for verb"
:group 'my
)
(defface yk-face-morpheme
`((((class color) (background light))
(:foreground "magenta"))
(((class color) (background dark))
(:foreground "darkgreen")))
"Face for verbs."
:group 'yomikun)
(defface yk-face-adverb
`((((class color) (background light))
(:foreground "purple"))
(((class color) (background dark))
(:foreground "purple")))
"Face for adverbs."
:group 'yomikun)
(defface yk-face-adjective
`((((class color) (background light))
(:foreground "orange"))
(((class color) (background dark))
(:foreground "orange")))
"Face for adjectives."
:group 'yomikun)
(defface yk-face-particle
`((((class color) (background light))
(:foreground "darkgrey"))
(((class color) (background dark))
(:foreground "darkgrey")))
"Face for particles."
:group 'yomikun)
(defface yk-face-punctuation
`((((class color) (background light))
(:foreground "black"))
(((class color) (background dark))
(:foreground "black")))
"Face for particles."
:group 'yomikun)
;; combined unknown and grammar point
(defface yk-face-noun-unknown
'((t (:inherit ( yk-face-noun yk-face-unknown))))
"Face for noun unknown")
(defface yk-face-particle-unknown
'((t (:inherit ( yk-face-particle yk-face-unknown))))
"Face for particle unknown")
(defface yk-face-verb-unknown
'((t (:inherit ( yk-face-verb yk-face-unknown))))
"Face for verb unknown")
(defface yk-face-adverb-unknown
'((t (:inherit ( yk-face-adverb yk-face-unknown))))
"Face for Adverb unknown")
(defface yk-face-punctuation-unknown
'((t (:inherit ( yk-face-punctuation yk-face-unknown))))
"Face for punctuation unknown")
(defface yk-face-morpheme-unknown
'((t (:inherit ( yk-face-morpheme yk-face-unknown))))
"Face for morpheme unknown")
(defface yk-face-adjective-unknown
'((t (:inherit (yk-face-adjective yk-face-unknown)
)))
"Face for adjective unknown")
;; learning
(defface yk-face-noun-learning
'((t (:inherit ( yk-face-noun yk-face-learning))))
"Face for noun learning")
(defface yk-face-particle-learning
'((t (:inherit ( yk-face-particle yk-face-learning))))
"Face for particle learning")
(defface yk-face-verb-learning
'((t (:inherit ( yk-face-verb yk-face-learning))))
"Face for verb learning")
(defface yk-face-adverb-learning
'((t (:inherit ( yk-face-adverb yk-face-learning))))
"Face for Adverb learning")
(defface yk-face-punctuation-learning
'((t (:inherit ( yk-face-punctuation yk-face-learning))))
"Face for punctuation learning")
(defface yk-face-morpheme-learning
'((t (:inherit ( yk-face-morpheme yk-face-learning))))
"Face for morpheme learning")
(defface yk-face-adjective-learning
'((t (:inherit (yk-face-adjective yk-face-learning)
)))
"Face for adjective learning")
;; the lookup tables
(defvar yk-wtype-table '(
("名詞" . yk-face-noun)
("助詞" . yk-face-particle)
("動詞" . yk-face-verb)
("副詞" . yk-face-adverb)
("記号" . yk-face-punctuation)
("助動詞" . yk-face-morpheme)
("形容詞" . yk-face-adjective)
))
(defvar yk-wtype-table-status-unknown '(
("名詞" . yk-face-noun-unknown)
("助詞" . yk-face-particle-unknown)
("動詞" . yk-face-verb-unknown)
("副詞" . yk-face-adverb-unknown)
("記号" . yk-face-punctuation-unknown)
("助動詞" . yk-face-morpheme-unknown)
("形容詞" . yk-face-adjective-unknown)
))
(defvar yk-wtype-table-status-learning'(
("名詞" . yk-face-noun-learning)
("助詞" . yk-face-particle-learning)
("動詞" . yk-face-verb-learning)
("副詞" . yk-face-adverb-learning)
("記号" . yk-face-punctuation-learning)
("助動詞" . yk-face-morpheme-learning)
("形容詞" . yk-face-adjective-learning)
))
(defvar yk-font-table-default-status '(
;; known does not have an entry, since it is not fontified
("unknown" . yk-face-unknown)
("learning" . yk-face-learning)
("ignore" . yk-face-ignore)
))
(defun yk-font-table-wtype-to-use (status)
"This is the core of the fontification.
Depending on the status, return a given table.
For ignore, we don't fonti
Defaults to the unknown table
"
(cond
((string-equal "known" status) yk-wtype-table)
((string-equal "learning" status) yk-wtype-table-status-learning)
((string-equal "ignore" status) nil)
(yk-wtype-table-status-unknown)
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; some high order functions to
;; process using pfun phrases and morphs in the current buffer
;; whe the cmpfun returns tru
;; a phrase is any japanese text between punctuation
(defun yk-morph-do-phrases (cmpfun pfun)
"process each phrase (pfun begin end) that satisfies (cmpfun begin)"
(let ((pos (point-min)))
(while pos
(when (and (get-text-property pos 'yk-morph)
(funcall cmpfun pos))
(funcall pfun pos (next-single-property-change pos 'yk-morph)))
(setq pos (next-single-property-change pos 'yk-morph))
)))
(defun yk-morph-do-morphs-in-region (beg end cmpfun pfun)
"process each morph (pfun begin end) that satisfies (cmpfun begin)"
(let ((pos beg))
(while (and pos
(< pos end))
; (message "just making sure [%s]" pos)
(when (and (get-text-property pos 'begin)
(funcall cmpfun pos))
(funcall pfun pos (next-single-property-change pos 'begin)))
(setq pos (next-single-property-change pos 'begin))
)))
(defun yk-get-tokens-region (beg end)
"return a list of all the tokens in the region"
(let(
(lst (list))
)
(yk-morph-do-morphs-in-region beg end
(lambda (pos) t)
(lambda (pos end)
(setq lst (cons
(list pos
(get-text-property pos 'surface)
(get-text-property pos 'seen)
)
lst))
)
)
(nreverse lst)
)
)
(defun yk-has-japanese-characters-p (str)
"return t if str has any Japanese characters."
(not (string-match-p "\\`[[:ascii:]]*\\'" str))
)
(defun yk-build-potential-candidates (lst len)
;; lst is a list of pairs (surface seen)
; (message "build [%s] [%d]" lst len)
(if (>= (length lst) len)
;;
(let* (
(prefix (string-join
;; use seen to build prefix
(mapcar (lambda (el) (nth 2 el))
(subseq lst 0 (- len 1)))
""
)
)
(suffix (nth (- len 1) lst))
; (patito (message "suffix [%s]" suffix))
(suffixes (list (nth 1 suffix) (nth 2 suffix)))
)
; (message ">candidate prefix [%d] [%s] [suffixes]" len prefix suffixes)
;prefix
;; in case surface is same as root
;; (delete-dups suffixes)
(mapcar
(lambda (el) (concat prefix el))
suffixes
)
)
;; else
nil
)
)
(defun yk-find-compound (lst candidates)
;; see what is the longest substring
;; starting from position 0
;; that is found in candidates
;; return nil or (compound number-of-tokens)
(let* (
(n (length lst))
;; (longest (longest-string candidates))
(max (apply 'max (mapcar 'length candidates)))
(min (apply 'min (mapcar 'length candidates)))
(compound nil)
)
(while (and
(not compound)
(> n 1))
(let* (
(current (yk-build-potential-candidates lst n))
(cur-surface (nth 0 current))
(cur-seen (nth 1 current))
)
; (message " tryin [%s][%s] current [%s]" cur-surface cur-seen current)
; (message " candidates [%s]" candidates)
;; avoid unnecessary tests
(when (or (and (<= (length cur-surface) max) (>= (length cur-surface) min))
(and (<= (length cur-seen) max) (>= (length cur-seen) min))
)
;; see if one matches
(cond
((member cur-seen candidates)
(progn
(setq compound (list cur-seen n))
; (message "Seen Foooooooooooooooooooooo [%s]" compound)
)
)
((member cur-surface candidates)
(progn
(setq compound (list cur-surface n))
; (message "Sir Foooooooooooooooooooooo [%s]" compound)
)
)
)
)
)
(setq n (- n 1))
)
;; return value
;; (if compound
;; (message "Found compound [%s] " compound)
;; )
compound
)
)
(defvar yk-compound-occurences 0)
(defun yk-find-compound-matches (lst)
(let (
(len (length lst))
(not-done t)
(offset 0)
(result nil)
;; experimenting... remove later if unused
(max-cur-compound-len 0)
)
;; add dummy element to we can do the while loop
;; simplifies logic
(setq lst (cons t lst))
(while (and
not-done
(setq lst (cdr lst)) ;; consume the list
)
(let* (
(candidates (yk-build-potential-candidates lst 2))
;; TODO this is slowing down things... but there might be situations
;; where a root ending of a compound of two might match
;; but maybe it is too small to worry about it?
;; but it might affect verbs primarily
;; (surface-matches-p (yk-db-compound-exists (nth 0 candidates)))
(db-candidates (yk-db-compound-prefix-candidates (nth 1 candidates)))
(compound (and db-candidates
(yk-find-compound lst db-candidates)
))
(compound-len (and compound
(nth 1 compound)))
(compound-st (and compound
(nth 0 compound)))
)
; (message "---------------matches %s" surface-matches-p)
; (message "candidates %s" candidates)
; (message "db exist %s" db-candidates)
(when compound
;; (message "Found match offset [%d][%s] [%s] len list [%d]"
;; offset
;; compound compound-len (length lst))
;; (message "start [%s] last [%s]" (car lst) (nth (+ compound-len -1) lst))
;;; save starting point, end point and compound
(push (list (car lst) (nth (+ compound-len -1 ) lst)
compound-st) result)
(if (> compound-len max-cur-compound-len)
(setq max-cur-compound-len compound-len)
)
(if (= compound-len (length lst))
;; no point on continuing. we have a match to the end of the list
;; there are other conditions where it is not possible to find matches
;; but it is already complex enough and the gains might be marginal
(setq not-done nil)
)
)
)
(setq offset (+ offset 1))
; (setq lst (cdr lst))
)
; (if result (message "Compounds: [%s]" result))
result
)
)
(defun yk-mark-as-compound (lst)
;; this function is probably slower than it can be
;; but it not executed a lot
(setq yk-compound-occurences (+ 1 yk-compound-occurences))
(let*(
(beg-compound (car (nth 0 lst)))
(end-token-pos (car (nth 1 lst)) )
(end-compound (get-text-property end-token-pos 'end))
(st (nth 2 lst))
)
(message "beg [%s] end [%s] st [%s] len [%d]" beg-compound end-compound st (length st))
(yk-set-overlay-compound-at-pos beg-compound end-compound)
(dolist (pos (number-sequence beg-compound end-compound))
(let (
(cur-prop (get-text-property pos 'compound))
)
(message "setting [%d] with [%s] current [%s]" pos st cur-prop)
(put-text-property pos (+ 1 pos) 'compound (cons st cur-prop ))
)
)
)
)
(defun yk-process-compounds-in-phrase (beg end)
; (message "sentence [%s]" (buffer-substring beg end))
(let* (
(p-tokens (yk-get-tokens-region beg end))
(matches (yk-find-compound-matches p-tokens))
)
(message "tokens [%s]" p-tokens)
(message "matches [%s]" matches)
(when matches
;; reset overlay for compound
(remove-text-properties beg end '(compound nil ))
;yk-face-compound
; (message "matches [%s]" matches)
(mapc
'yk-mark-as-compound
matches
)
)
matches
)
)
(defun yk-do-all-compounds ()
(interactive)
(setq yk-compound-occurences 0)
(yk-morphs-delete-overlays-at-pos 'yomikun-comp (point-min) (point-max))
(yk-db-dict-open)
(with-silent-modifications
(yk-morph-do-phrases
(lambda (pos) t)
'yk-process-compounds-in-phrase
))
(message "Done. Found [%s] compounds" yk-compound-occurences)
)
(defun yk-morph-do-morphs (cmpfun pfun)
"process each moph(beg end) that satisfies cmpfun(beg)
and call pfun on it"
(let ((pos (point-min)))
(while pos
(when (and (get-text-property pos 'begin)
(funcall cmpfun pos))
(funcall pfun pos (next-single-property-change pos 'begin)))
(setq pos (next-single-property-change pos 'begin))
)))
(defun yk-morph-matches-at (morphProps pos)
(let ((pos (point))
(props (text-properties-at pos))
)
; (progn
(and (string-equal (plist-get morphProps 'root)
(plist-get props 'root)
)
(string-equal (plist-get morphProps 'wtype)
(plist-get props 'wtype)
)
(string-equal (plist-get morphProps 'surface)
(plist-get props 'surface)
)
)
)
; )
)
(defun yk-morphs-delete-overlays-at-pos (name beg end)
"delete overlays between beg and end that have property name equal t"
(message "deleting... overlays name [%s:%s ][%s]" beg end name)
(remove-overlays beg end name t)
)
(defun yk-remove-props-and-overlays (beg end)
(interactive "r")
;; find all props and remove them
;; find all overlays and remove them
(with-silent-modifications
(let ((inhibit-read-only t)) ; allow modifying read-only text
(remove-text-properties beg end '(root nil
wtype nil
surface nil
pronun nil
begin nil
yk-morph nil
end nil
status nil
))
(remove-overlays beg end 'yomikun t)
(remove-overlays beg end 'yomikun-comp t)
)))
(setq yk-test-all-morphs (make-hash-table :test 'equal));(list))
;;(setq yk-status-table (make-hash-table :test 'equal))
(defun yk-morph-get-morph-from-props (props)
"get the morph from the properties"
(list
(plist-get props 'root)
(plist-get props 'wtype)
(plist-get props 'surface)
)
)
(defun yk-morph-get-from-props (props attr)
"get specific attribute from props
this might be redundant, but it is trying to create a layer between
properties and data
"
(plist-get props attr)
)
(defun yk-sort-hash-table (hash-table compare-func)
"Return a sorted list of key-value pairs from HASH-TABLE.
The list is sorted using COMPARE-FUNC to compare elements."
(let (pairs)
(maphash (lambda (key value)
(push (cons key value) pairs))
hash-table)
(sort pairs compare-func)))
;
(defun yk-extract-all-morphs ()
"return a hashtable where the key is the morph, and the value the frequency"
(let
(
(all-morphs (make-hash-table :test 'equal) )
)
(yk-morph-do-morphs
(lambda (beg) (plist-get (text-properties-at beg) 'root)) ;; process all morphs
(lambda (beg end) ;; add morphs to the hashtable
(let*
(
(props (text-properties-at beg))
(morph (yk-morph-get-morph-from-props props))
)
(message "%s" props)
;; increase their counter by 1
(puthash morph
(+ (gethash morph all-morphs 0) 1)
all-morphs)
))
)
; (message "[%s]" all-morphs)
(yk-sort-hash-table all-morphs
(lambda (a b) (> (cdr a) (cdr b)))
)
)
)
(defun yk-report-all-morphs ()
"report the frequency of morphs"
(interactive)
(let (
(morphs (yk-extract-all-morphs))
; (buffer (create... yk-report-buffer))
)
(mapc
(lambda (m) (message "morphs [%s]" m))
morphs
)
(message "total: %d" (length morphs))
)
)
(defun yk-update-status-at-position (beg end wtype newstatus)
;;
;; replace the overlays at the given position
;; according to the new status
;;
(yk-set-overlay-wtype-at-pos beg end wtype newstatus )
)
(defun yk-replace-all-overlays (props newstatus)
;; ;
;; replace all the overlays of the given morph in all the document
;; according to the new status
;;
(yk-morph-do-morphs
(lambda (beg) (yk-morph-matches-at props beg )););
(lambda (beg end) (yk-update-status-at-position beg
end
(plist-get props 'wtype)
newstatus
)
)
)
)
(defun yk-morph-new-status (props new-status)
;; mark the morph in props as new status
;; in the database and in the database
;;
(let* (
(root (plist-get props 'root))
(wtype (plist-get props 'wtype))
(old-status (plist-get props 'status))
(surface (plist-get props 'surface))
)
; (message "after let [%s] [%s] [%s]-> [%s] " root wtype status new-status)
; (message " begin end [%s] [%s]" beg end)
(if (not (string-equal old-status new-status))
(progn
(message "setting new status [%s] old [%s]" new-status old-status)
(message " root [%s] wtype [%s] surface [%s]" root wtype surface)
; we need to set the status of that word everywhere...
(yk-morph-status-set root wtype surface new-status)
;; now we have to process the document to find instances of the word
;; ... there is a need for a redo buffer
;; but that is expensive, soo simply scan the document
;; and find any instances of the morph
;; and change its property
;; first remove all overlays in the file
(yk-replace-all-overlays props new-status)
)
(message "morph (%s %s %s)already has [%s] status" root wtype surface new-status)
)
))
;; (defun yk-mark-morph-as-known (beg end)
;; )
;; (defun yk-morph-set-known ()
;; (interactive)
;; (let* (
;; (pos (point))
;; (props (text-properties-at pos))
;; )
;; (if (and props
;; (plist-get props 'root)
;; )
;; (progn ;
;; (yk-morph-new-status props "known")
;; (yk-morph-do-morphs
;; (lambda (beg) (yk-morph-matches-at props beg )););
;; 'yk-mark-as-known
;; )
;; )
;; )
;; )
;; )