-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpaw-kagome.el
277 lines (244 loc) · 11.3 KB
/
paw-kagome.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
;;; paw-kagome.el -*- lexical-binding: t; -*-
(require 'json)
(defcustom paw-kagome-program
(cond
((string-equal system-type "android")
"/data/data/com.termux/files/home/go/bin/kagome")
(t
"kagome"))
"Executable used to access the kagome."
:type 'file
:group 'kagome)
(defvar paw-kagome-running-process nil)
;;;###autoload
(defun paw-kagome-kill-process ()
(interactive)
(when (process-live-p paw-kagome-running-process )
(kill-process paw-kagome-running-process)
(setq paw-kagome-running-process nil)))
;;;###autoload
(defun paw-kagome-segment ()
"Segments a STRING of Japanese text using Kagome and logs the result asynchronously."
(interactive)
(paw-kagome-kill-process)
(let* ((original-output-buffer (get-buffer "*kagome-output*"))
(output-buffer (if (buffer-live-p original-output-buffer)
(progn (kill-buffer original-output-buffer)
(get-buffer-create "*kagome-output*") )
(get-buffer-create "*kagome-output*") ))
(kagome-process (make-process
:name "Kagome"
:buffer output-buffer
:noquery t
:command `(,paw-kagome-program "-json")
:filter 'paw-kagome-process-filter
:sentinel 'paw-kagome-process-sentinel-whole-buffer)))
(setq paw-kagome-running-process kagome-process)
(process-send-string kagome-process (concat (buffer-substring-no-properties (point-min) (point-max)) "\n"))
(process-send-eof kagome-process)))
;;;###autoload
(defun paw-kagome-segment-blocking ()
"Synchronously segment Japanese text in the current buffer using Kagome."
(interactive)
(shell-command-on-region (point-min) (point-max) "kagome -json" t t)
(goto-char (point-min))
(let* ((json-array-type 'list)
(json-object-type 'plist)
(json-responses (json-read))
(segmented-text (mapconcat (lambda (resp) (plist-get resp :surface))
json-responses
;; " "
paw-non-ascii-word-separator)))
(erase-buffer)
(insert segmented-text))
(message "Segmentation completed."))
(defun paw-kagome-process-filter (proc string)
"Accumulates the strings received from the Kagome process."
(when (buffer-live-p (process-buffer proc))
(with-current-buffer (process-buffer proc)
(insert string))))
(defun paw-kagome-process-sentinel (proc _event)
"Handles the Kagome process termination event."
(when (eq (process-status proc) 'exit)
(let* ((json-object-type 'plist)
(json-array-type 'list)
(buffer-content (with-current-buffer (process-buffer proc)
(buffer-string)))
(json-responses (json-read-from-string buffer-content)))
(dolist (resp json-responses)
(let* ((start (plist-get resp :start))
(end (plist-get resp :end))
(surface (plist-get resp :surface))
(cls (plist-get resp :class)) ;; 'class' is a built-in function
(pos (plist-get resp :pos))
(base-form (plist-get resp :base_form))
(reading (plist-get resp :reading))
(pronunciation (plist-get resp :pronunciation))
(features (plist-get resp :features)))
(message "start: %s, end: %s, surface: %s, class: %s, pos: %s, base_form: %s, reading: %s, pronunciation: %s, features: %s"
start end surface cls pos base-form reading pronunciation features)))
(let ((segmented-text (mapconcat
(lambda (resp) (plist-get resp :surface))
json-responses
;; " "
paw-non-ascii-word-separator)))
(message "Segmented text: %s" segmented-text)))))
(defun paw-kagome-process-sentinel-whole-buffer (proc _event)
"Handles the Kagome process termination event."
(when (eq (process-status proc) 'exit)
(let* ((json-object-type 'plist)
(json-array-type 'list)
(buffer-content (with-current-buffer (process-buffer proc)
(buffer-string)))
(json-responses (json-read-from-string buffer-content)))
;; (dolist (resp json-responses)
;; (let* ((start (plist-get resp :start))
;; (end (plist-get resp :end))
;; (surface (plist-get resp :surface))
;; (cls (plist-get resp :class)) ;; 'class' is a built-in function
;; (pos (plist-get resp :pos))
;; (base-form (plist-get resp :base_form))
;; (reading (plist-get resp :reading))
;; (pronunciation (plist-get resp :pronunciation))
;; (features (plist-get resp :features)))
;; (message "start: %s, end: %s, surface: %s, class: %s, pos: %s, base_form: %s, reading: %s, pronunciation: %s, features: %s"
;; start end surface cls pos base-form reading pronunciation features)))
(let ((segmented-text (mapconcat
(lambda (resp) (plist-get resp :surface))
json-responses
;; " "
paw-non-ascii-word-separator
)))
(with-current-buffer (current-buffer)
(erase-buffer)
(insert segmented-text))))))
(defun paw-kagome-process-output (string)
(let* ((json-array-type 'list)
(json-object-type 'plist)
(json (json-read-from-string string))
(surface-strings (mapcar (lambda (j) (plist-get j 'surface)) json)))
(mapconcat 'identity surface-strings paw-non-ascii-word-separator)))
(defvar kagome-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c s g") 'paw-kagome-segment)
map)
"Keymap for Kagome minor mode.")
(defun paw-kagome-command (string &optional sentinel)
"Segments a STRING of Japanese text using Kagome and logs the result asynchronously."
(unless (executable-find paw-kagome-program)
(error "kagome is not found, please install via 'go install github.com/ikawaha/kagome/v2@latest' first."))
(paw-kagome-kill-process)
(let* ((original-output-buffer (get-buffer "*kagome-output*"))
(output-buffer (if (buffer-live-p original-output-buffer)
(progn (kill-buffer original-output-buffer)
(get-buffer-create "*kagome-output*") )
(get-buffer-create "*kagome-output*") ))
(kagome-process (make-process
:name "Kagome"
:buffer output-buffer
:noquery t
:command `(,paw-kagome-program "-json")
:filter 'paw-kagome-process-filter
:sentinel (if sentinel sentinel 'paw-kagome-process-sentinel))))
(setq paw-kagome-running-process kagome-process)
(process-send-string kagome-process (concat string "\n"))
(process-send-eof kagome-process)))
;;;###autoload
(defun paw-kagome-segment-file-blocking ()
"Segments Japanese text in current buffer using Kagome synchronously."
(interactive)
(let* ((pre-string (buffer-substring-no-properties (point-min) (point-max)))
(input-temp-file-path (make-temp-file "kagome" nil ".org"))
(file-code (with-temp-file input-temp-file-path
(insert pre-string)
42))
(buffer-content (with-temp-buffer
(shell-command (format "kagome -file %s -json" input-temp-file-path) t t)
(buffer-string)))
(post-string (with-temp-buffer
(let* ((json-object-type 'plist)
(json-array-type 'list)
(json-responses (paw-kagome-parse-json-sequence buffer-content))
(segmented-text (let (result)
(dolist (ele json-responses)
(setq result (concat (mapconcat (lambda (el) (plist-get el :surface)) ele paw-non-ascii-word-separator) "\n\n" result ) ))
result)))
segmented-text))))
(erase-buffer)
(insert post-string)
;; (message post-string)
(goto-char (point-min))
(message "Segmentation finished.")))
(defun paw-kagome-parse-json-sequence (json-string)
(with-temp-buffer
(insert json-string)
(goto-char (point-min))
(let ((result nil)) ;; list to hold parsed JSON arrays
;; Search forward for '['
(while (search-forward "[" nil t)
(backward-char) ;; Move point back to beginning of [
(let ((json-array (json-read)))
(if json-array
(push json-array result))))
result))) ;; Return reversed list to maintain original order
(defun paw-kagome-command-blocking (string)
"Segments a STRING of Japanese text using Kagome synchronously and return the result."
(unless (executable-find paw-kagome-program)
(error "kagome is not found, please install via 'go install github.com/ikawaha/kagome/v2@latest' first."))
(let* ((buffer-content (with-temp-buffer
(insert string)
(shell-command-on-region (point-min) (point-max) "kagome -json" t t)
(buffer-string))))
(with-temp-buffer
(let* ((json-object-type 'plist)
(json-array-type 'list)
(json-responses (json-read-from-string buffer-content))
(segmented-text (mapconcat
(lambda (resp) (plist-get resp :surface))
json-responses
paw-non-ascii-word-separator)))
segmented-text))))
(defun paw-kagome-shr-insert (text)
(when (and (not (bolp))
(get-text-property (1- (point)) 'image-url))
(insert "\n"))
(cond
((eq shr-folding-mode 'none)
(let ((start (point)))
(insert text)
(save-restriction
(narrow-to-region start (point))
(shr--translate-insertion-chars)
(goto-char (point-max)))))
(t
(let ((font-start (point)))
(when (and (string-match-p "\\`[ \t\n\r]" text)
(not (bolp))
(not (eq (char-after (1- (point))) ? )))
(insert " "))
(let ((start (point))
(bolp (bolp)))
(insert (paw-kagome-command-blocking text ))
(save-restriction
(narrow-to-region start (point))
(goto-char start)
(when (looking-at "[ \t\n\r]+")
(replace-match "" t t))
(while (re-search-forward "[\t\n\r]+" nil t)
(replace-match " " t t))
(goto-char start)
(while (re-search-forward " +" nil t)
(replace-match " " t t))
(shr--translate-insertion-chars)
(goto-char (point-max)))
;; We may have removed everything we inserted if it was just
;; spaces.
(unless (= font-start (point))
;; Mark all lines that should possibly be folded afterwards.
(when bolp
(shr-mark-fill start))
(when shr-use-fonts
(put-text-property font-start (point)
'face
(or shr-current-font 'shr-text)))))))))
(provide 'paw-kagome)