| 1 |
;;; skk-bayesian.el -- Bayesian estimation for SKK -*- coding: iso-2022-jp -*- |
| 2 |
|
| 3 |
;; Copyright (C) 2004 Kenichi Kurihara <[email protected]> |
| 4 |
|
| 5 |
;; Author: Kenichi Kurihara <[email protected]> |
| 6 |
;; Maintainer: SKK Development Team |
| 7 |
;; URL: https://github.com/skk-dev/ddskk |
| 8 |
;; Keywords: japanese, mule, input method, bayesian estimation |
| 9 |
|
| 10 |
;; This file is part of Daredevil SKK. |
| 11 |
|
| 12 |
;; This program is free software: you can redistribute it and/or |
| 13 |
;; modify it under the terms of the GNU General Public License as |
| 14 |
;; published by the Free Software Foundation, either version 3 of |
| 15 |
;; the License, or (at your option) any later version. |
| 16 |
|
| 17 |
;; This program is distributed in the hope that it will be |
| 18 |
;; useful, but WITHOUT ANY WARRANTY; without even the implied |
| 19 |
;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 |
;; PURPOSE. See the GNU General Public License for more details. |
| 21 |
|
| 22 |
;; You should have received a copy of the GNU General Public License |
| 23 |
;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 |
|
| 25 |
;;; Commentary: |
| 26 |
|
| 27 |
;; skk-study $B$,D>A0$NMzNr$N$_$r;HMQ$9$k$N$G!"$3$l$r3HD%$7$?$$$H;W$C$?$N(B |
| 28 |
;; $B$,A4$F$NF05!$G$9!#(BSKK $B$H$=$N%3%_%e%K%F%#$K46<U$7$^$9!#(B |
| 29 |
|
| 30 |
|
| 31 |
;; <$BF0:n(B> |
| 32 |
;; $BNc(B: (skk-bayesian-context-len = 5 $B$N;~(B) |
| 33 |
;; $B!V$=$NI~$r!"!W$N8e$K!"$-(Br $B$rJQ49$9$k>u67$K$*$$$F!"(B |
| 34 |
;; entry $B$,!"(B("$B@Z(B" "$BCe(B" "$B;B(B") $B$G$"$k>u67$r9M$($k!#(B |
| 35 |
;; $B$3$N(B enrty $B$r0J2<$N3NN($r7W;;$9$k$3$H$G!"%=!<%H$9$k!#(B |
| 36 |
|
| 37 |
;; Prob( word="$B@Z(B" | p_1="$B!"(B", p_2="$B$r(B", p_3="$BI~(B", p_4="$B$N(B", p_5="$B$=(B" ) |
| 38 |
;; Prob( word="$BCe(B" | p_1="$B!"(B", p_2="$B$r(B", p_3="$BI~(B", p_4="$B$N(B", p_5="$B$=(B" ) |
| 39 |
;; Prob( word="$B;B(B" | p_1="$B!"(B", p_2="$B$r(B", p_3="$BI~(B", p_4="$B$N(B", p_5="$B$=(B" ) |
| 40 |
|
| 41 |
;; $B3X=,$9$Y$-%Q%i%a!<%?$N?t$r8:$i$9$?$a!"$3$N3NN(%b%G%k$r0J2<$N$h$&$J(B |
| 42 |
;; $B:.9gJ,I[$G$"$k$H2>Dj$9$k!#(B |
| 43 |
|
| 44 |
;; Prob( word="$B@Z(B" | p_1="$B!"(B", p_2="$B$r(B", p_3="$BI~(B", p_4="$B$N(B", p_5="$B$=(B" ) |
| 45 |
;; ~= \sum_{i=1}^5 w_i * Prob( word="$B@Z(B" | p_i ) |
| 46 |
|
| 47 |
;; $B$?$@$7!"(Bw_i $B$O:.9gJ,I[$N=E$_$G$"$k!#(B |
| 48 |
|
| 49 |
|
| 50 |
;; <$B2]Bj(B> |
| 51 |
;; 1. bskk $B$,C1=c$K:n$i$l$F$$$k$N$G!"JQ49$NMzNr$,Bg$-$/$J$C$?;~$K!"F0:n(B |
| 52 |
;; $BB.EY$HI,MW$J%a%b%j$NNL$,?4G[!#(B |
| 53 |
;; 2. $B:.9gJ,I[$N=E$_(B w_i $B$O8=:_!"(Bw_1, w_2, ..., w_n $B$KBP$7$F!"(B |
| 54 |
;; w_i : w_j = (n-i) : (n-j) |
| 55 |
;; $B$H$J$k$h$&$KCM$r7h$a$F$$$k!#K\Mh!"$$$:$l$b1#$lJQ?t$H$7$F!"(BEM$B%"%k(B |
| 56 |
;; $B%4%j%:%`(B, VBA $BEy$K$h$j3X=,$9$Y$-$+$b$7$l$J$$!#(B |
| 57 |
;; 3. skk-bayesian-context-len $B$OJQ?t$K$7$F$$$k$N$G!"%f!<%6$,7hDj$G$-$k(B |
| 58 |
;; $B$,!"M}A[E*$K$O%b%G%k$N?dDjLdBj$H$H$i$($F!"3X=,%G!<%?$+$i7hDj$9$Y(B |
| 59 |
;; $B$-$@$m$&!#$^$?!"$"$kDxEY!"3X=,$7$?8e$K(B skk-bayesian-context-len$B$r(B |
| 60 |
;; $BBg$-$$CM$KJQ99$9$k$N$O!"?dDj$K0-1F6A$rM?$($=$&!#(B |
| 61 |
;; 4. 2$B$H(B3$B$K=E$J$k$,!"Cx:n8"$N?4G[$r$7$J$/$F$b$h$$%3!<%Q%9$+$i!"3X=,$r9T$$(B |
| 62 |
;; skk-bayesian-context-len $B$H(B $B:.9gJ,I[$N=E$_$r7hDj$7$?$$!#(B |
| 63 |
;; 5. bskk $B$H$N%W%m%H%3%k$,AG?M=-$$!#(B |
| 64 |
|
| 65 |
|
| 66 |
;; <$B;H$$J}(B> |
| 67 |
;; ~/.skk $B$K!"(B(require 'skk-bayesian) $B$H=q$$$F2<$5$$!#(B |
| 68 |
;; skk-study $B$H$NJ;MQ$O5!G=$,=E$J$k$N$G!"$*4+$a$G$-$^$;$s!#(B |
| 69 |
|
| 70 |
;; $B$^$?!"(Bbskk $B$O!"%5%V%W%m%;%9$+%5!<%P$H$7$F;HMQ$7$^$9!#(B |
| 71 |
;; *$B%5%V%W%m%;%9(B |
| 72 |
;; $B%5%V%W%m%;%9$H$7$F;HMQ$9$k$K$O!"(Bbskk $B$r4D6-JQ?t(B PATH $B$NDL$C$?>l=j$K(B |
| 73 |
;; $BCV$/$@$1$G$9!#(B |
| 74 |
;; $BLdBj$O!"$$$/$D$b(B emacs $B$r5/F0$9$k$H(B ~/.skk-bayesian $B$O:G8e$K99?7$7$?(B |
| 75 |
;; emacs $B$K0M$k$N$G!"B>$N(B emacs $B$G$N3X=,%G!<%?$OJ]B8$5$l$^$;$s!#(B |
| 76 |
;; *$B%5!<%P(B |
| 77 |
;; bskk $B$r%5!<%P$H$7$F;HMQ$9$k$K$O!"(Bemacs $B$,(B skk-bayesian.el $B$rFI$_9~(B |
| 78 |
;; $B$`A0$K!"(B |
| 79 |
;; % bskk -f ~/.skk-bayesian -s |
| 80 |
;; $B$H<B9T$7$F!"5/F0$7$F$*$/I,MW$,$"$j$^$9!#(B |
| 81 |
;; $B%5!<%P$r=*N;$5$;$kJ}K!$O!"(B |
| 82 |
;; % kill -TERM {bskk $B$N(B PID} |
| 83 |
;; $B$G$9!#(B |
| 84 |
;; ~/.skk $B$K$O!"(B(setq skk-bayesian-prefer-server t) $B$r=q$$$F2<$5$$!#(B |
| 85 |
|
| 86 |
;; <$B;EMM$N3P$(=q$-(B> |
| 87 |
;; $B3F4X?t$H!"(Bbskk $B$N%3%a%s%HFb$N(B Specifications $B$K=q$+$l$F$$$k!#(B |
| 88 |
|
| 89 |
|
| 90 |
;;; Code: |
| 91 |
|
| 92 |
(require 'skk-autoloads nil 'noerror) |
| 93 |
(require 'skk-vars) |
| 94 |
(require 'skk-macs) |
| 95 |
|
| 96 |
(defgroup skk-bayesian nil "SKK Bayesian estimation group" |
| 97 |
:prefix "skk-bayesian-" |
| 98 |
:group 'skk) |
| 99 |
|
| 100 |
;;; |
| 101 |
;;; variables for skk-bayesian |
| 102 |
;;; |
| 103 |
(defcustom skk-bayesian-prefer-server nil |
| 104 |
"*non-nil $B$J$i$P!"(B`skk-bayesian-host' $B$N(B `skk-bayesian-port' $B$K@\B3$9$k!#(B |
| 105 |
$B$=$&$G$J$1$l$P!"(Bbskk $B$r%5%V%W%m%;%9$H$7$FN)$A>e$2$k!#(B" |
| 106 |
:type 'boolean |
| 107 |
:group 'skk-bayesian) |
| 108 |
|
| 109 |
(defcustom skk-bayesian-port 51178 |
| 110 |
"*`skk-bayesian-host' $B$K@\B3$9$k%]!<%HHV9f!#(B |
| 111 |
$B%5!<%P$K@\B3$9$k$K$O(B `skk-bayesian-prefer-server' $B$,(B non-nil $B$G$"$kI,MW$,$"$k!#(B" |
| 112 |
:type 'integer |
| 113 |
:group 'skk-bayesian) |
| 114 |
|
| 115 |
(defcustom skk-bayesian-host "localhost" |
| 116 |
"*`skk-bayesian-prefer-server' $B$,(B non-nil $B$N;~$K@\B3$9$k%[%9%HL>!#(B" |
| 117 |
:type 'string |
| 118 |
:group 'skk-bayesian) |
| 119 |
|
| 120 |
(defcustom skk-bayesian-context-len 20 |
| 121 |
"*$B3X=,$dM=B,$K;HMQ$9$k!"JQ498l$ND>A0$NJ8;z?t!#(B" |
| 122 |
:type 'integer |
| 123 |
:group 'skk-bayesian) |
| 124 |
|
| 125 |
(defcustom skk-bayesian-history-file |
| 126 |
(if skk-user-directory |
| 127 |
(expand-file-name "bayesian" skk-user-directory) |
| 128 |
(convert-standard-filename "~/.skk-bayesian")) |
| 129 |
"*$BMzNr$r5-O?$9$k%U%!%$%kL>!#(B |
| 130 |
`skk-bayesian-prefer-server' $B$,(B non-nil $B$N;~$K$N$_;HMQ$5$l$k!#(B" |
| 131 |
:type 'file |
| 132 |
:group 'skk-bayesian) |
| 133 |
|
| 134 |
(defcustom skk-bayesian-debug nil |
| 135 |
"*non-nil $B$J$i$P!"%G%P%C%0MQ$N%a%C%;!<%8$rI=<($9$k!#(B" |
| 136 |
;; bskk $B$N%m%0(B = $HOME/tmp/bskk.log |
| 137 |
:type 'boolean |
| 138 |
:group 'skk-bayesian) |
| 139 |
|
| 140 |
(defcustom skk-bayesian-max-commands-to-wait-for 15 |
| 141 |
"*$B3NDj8l$r3X=,$9$k$^$G$KBT$D%3%^%s%I$N?t!#(B |
| 142 |
$B3NDj$N8e$K(B `skk-bayesian-max-commands-to-wait-for' $B2s$N%3%^%s%I(B |
| 143 |
$B$N$&$A$K3NDj8l(B($BAw$j2>L>$r4^$`(B)$B$,JQ99$5$l$J$1$l$P!"$=$N3NDj8l$rJ]B8(B |
| 144 |
$B$9$k!#(B`skk-bayesian-max-commands-to-wait-for' $B$,%<%m0J2<$J$i$P!"3NDj8e!"(B |
| 145 |
$BD>$A$KMzNr$KJ]B8$9$k!#(B" |
| 146 |
:type 'integer |
| 147 |
:group 'skk-bayesian) |
| 148 |
|
| 149 |
(defcustom skk-bayesian-corpus-make nil |
| 150 |
"*nin-nil $B$J$i$P!"(Bcorpus $B$r(B `skk-bayesian-corpus-file' $B$K:n@.$9$k!#(B" |
| 151 |
:type 'boolean |
| 152 |
:group 'skk-bayesian) |
| 153 |
|
| 154 |
(defcustom skk-bayesian-corpus-file |
| 155 |
(if skk-user-directory |
| 156 |
(expand-file-name "corpus" skk-user-directory) |
| 157 |
(convert-standard-filename "~/.skk-corpus")) |
| 158 |
"*corpus $B$rJ]B8$9$k%U%!%$%k!#(B" |
| 159 |
:type 'file |
| 160 |
:group 'skk-bayesian) |
| 161 |
|
| 162 |
;; internal variables |
| 163 |
(defvar skk-bayesian-last-context nil "*$B3NDj8l$ND>A0$NJ8;zNs!#(B") |
| 164 |
(defvar skk-bayesian-number-of-command-after-kakutei 0 |
| 165 |
"*$BA02s$N3NDj$+$i8=:_$^$G$N%3%^%s%I$N2s?t!#(B") |
| 166 |
(defvar skk-bayesian-pending-data-alist nil "*non-nil $B$J$i$P(B pending $BCf!#(B") |
| 167 |
(defvar skk-bayesian-process nil) |
| 168 |
(defvar skk-bayesian-corpus-buffer nil) |
| 169 |
(defvar skk-bayesian-corpus-last-sorted-entry nil |
| 170 |
"*$BA02s(B skk-bayesian-search $B$GJV$7$?(B entry") |
| 171 |
|
| 172 |
;; constants |
| 173 |
(defconst skk-bayesian-command-sort "#sort\n") |
| 174 |
(defconst skk-bayesian-command-add "#add\n") |
| 175 |
(defconst skk-bayesian-command-save "#save\n") |
| 176 |
(defconst skk-bayesian-coding-system 'euc-jp) |
| 177 |
(defconst skk-bayesian-corpus-buffer-name " *skk-corpus*") |
| 178 |
|
| 179 |
|
| 180 |
;;; |
| 181 |
;;; functions |
| 182 |
;;; |
| 183 |
(defmacro skk-bayesian-debug-message (STRING &rest ARGS) |
| 184 |
`(if skk-bayesian-debug |
| 185 |
(message ,STRING ,@ARGS))) |
| 186 |
|
| 187 |
(defsubst skk-bayesian-process-live-p () |
| 188 |
"`skk-bayesian-process' $B$,(B non-nil $B$+$D$=$N%W%m%;%9$,<B9TCf$J$i(B t $B$rJV$9!#(B" |
| 189 |
(and skk-bayesian-process |
| 190 |
;; $B%M%C%H%o!<%/%W%m%;%9$J$i!"(Bopen, $BDL>o$N%5%V%W%m%;%9$J$i!"(Brun$B!#(B |
| 191 |
;; $B$3$l$i$O!"GSB>E*!#(B |
| 192 |
(memq (process-status skk-bayesian-process) '(open run)))) |
| 193 |
|
| 194 |
(defsubst skk-bayesian-make-pending-data-alist |
| 195 |
;; henkan-point $B$O3NDj8l$N:G=i$NJ8;z$N0LCV$N(B marker |
| 196 |
(word okurigana midasi buffer henkan-point context) |
| 197 |
(setq skk-bayesian-pending-data-alist |
| 198 |
(if (and word midasi buffer henkan-point context) |
| 199 |
;; $BFC$K(B henkan-point $B$,(B nil $B$K$J$j0W$$$h$&$@!#(B |
| 200 |
;; okurigana $B$O!"(Bnil $B$G$b$h$$!#(B |
| 201 |
(list (cons 'word word) |
| 202 |
(cons 'okurigana okurigana) |
| 203 |
(cons 'midasi midasi) |
| 204 |
(cons 'buffer buffer) |
| 205 |
(cons 'henkan-point henkan-point) |
| 206 |
(cons 'context context))))) |
| 207 |
|
| 208 |
(defsubst skk-bayesian-get-pending-data-alist (key) |
| 209 |
(if (memq key (list 'word 'okurigana 'midasi 'buffer 'henkan-point 'context)) |
| 210 |
(cdr (assq key skk-bayesian-pending-data-alist)) |
| 211 |
(error (concat "Error; invalid key=" (prin1-to-string 'key))))) |
| 212 |
|
| 213 |
(defsubst skk-bayesian-read-process-output (input) |
| 214 |
"INPUT $B$r(B `skk-bayesian-process' $B$KAw$k!#$=$N8e!"(B\\n $B$,(B `skk-bayesian-process' $B$N%P%C%U%!$K=PNO$5$l$k$^$GBT$A!"(B\\n $B$,=PNO$5$l$?;~E@$G!"%P%C%U%!$rI>2A$7JV$9!#(B" |
| 215 |
(when input |
| 216 |
(skk-bayesian-init) |
| 217 |
(with-current-buffer (process-buffer skk-bayesian-process) |
| 218 |
(delete-region (point-min) (point-max)) |
| 219 |
(process-send-string skk-bayesian-process input) |
| 220 |
(while (not (and (> (point-max) 1) |
| 221 |
(eq (char-after (1- (point-max))) ?\n))) |
| 222 |
(accept-process-output skk-bayesian-process 0 5)) |
| 223 |
(goto-char (point-min)) |
| 224 |
(condition-case err |
| 225 |
(read (current-buffer)) |
| 226 |
(error (skk-message "Error while reading the out put of bskk; %s" |
| 227 |
"bskk $B$N=PNO$NFI$_9~$_Cf$K%(%i!<(B; %s" |
| 228 |
(error-message-string err)) |
| 229 |
nil))))) |
| 230 |
|
| 231 |
(defun skk-bayesian-make-context (henkan-buffer) |
| 232 |
;; $B$b$7(B"$B"'(B"$B$,$"$l$P!"(B`skk-bayesian-context-len' $B$ND9$5$NJ8;zNs$rJV$9!#(B |
| 233 |
;; $B$J$1$l$P!"(Bnil$B!#(B |
| 234 |
(let ((raw-text |
| 235 |
(with-current-buffer henkan-buffer |
| 236 |
(let ((kakutei-symbol-point |
| 237 |
(save-excursion |
| 238 |
;; 100 $BJ8;zA0$^$G$7$+"'$r8!:w$7$J$$(B |
| 239 |
(search-backward "$B"'(B" (max (point-min) (- (point) 100)) t)))) |
| 240 |
(if kakutei-symbol-point |
| 241 |
(buffer-substring-no-properties |
| 242 |
(max (- kakutei-symbol-point skk-bayesian-context-len) |
| 243 |
(point-min)) |
| 244 |
kakutei-symbol-point)))))) |
| 245 |
(if raw-text |
| 246 |
(with-temp-buffer |
| 247 |
(let ((min (point-min))) |
| 248 |
(insert raw-text) |
| 249 |
;; $BJ8;zNs$+$i2~9T$r(B join-line $B$G=|$/!#(B |
| 250 |
;; $BC"$7!"F|K\8l$NCf$N2~9T$O6uGr$,F~$k$N$G!"$=$l$r=|$/!#(B |
| 251 |
(while (not (eq min (point))) |
| 252 |
(goto-char (point-max)) |
| 253 |
(join-line) |
| 254 |
;; from skk-viper.el |
| 255 |
(let ((char-after (char-after (progn (skip-chars-forward " ") |
| 256 |
(point)))) |
| 257 |
(char-before (char-before (progn (skip-chars-backward " ") |
| 258 |
(point))))) |
| 259 |
(when (and char-after char-before |
| 260 |
(or (skk-jisx0208-p char-after) |
| 261 |
(skk-jisx0213-p char-after)) |
| 262 |
(or (skk-jisx0208-p char-before) |
| 263 |
(skk-jisx0213-p char-before))) |
| 264 |
(while (looking-at " ") |
| 265 |
(delete-char 1)))))) |
| 266 |
(buffer-string)) |
| 267 |
nil))) |
| 268 |
|
| 269 |
(defun skk-bayesian-search (henkan-buffer midasi okurigana entry) |
| 270 |
;; $B%=!<%H$7$?!"(Bentry $B$rJV$9(B |
| 271 |
;; $B0z?t$NNc(B |
| 272 |
;; entry : ("$B;B(B" "$B@Z(B" "$BCe(B") |
| 273 |
;; midasi: $B$-(Br |
| 274 |
;; okurigana: $B$k(B |
| 275 |
(setq skk-bayesian-last-context nil) |
| 276 |
(if (= 1 (length entry)) |
| 277 |
entry |
| 278 |
(let ((context (skk-bayesian-make-context henkan-buffer)) |
| 279 |
;; $BKvHx$N(B "/" $B$OB?J,ITMW$@$,(B |
| 280 |
(entry-str (concat (mapconcat #'identity entry "/") "/")) |
| 281 |
sorted-entry) |
| 282 |
;; send context to skk-bayesian-process |
| 283 |
(setq sorted-entry |
| 284 |
(skk-bayesian-read-process-output (concat skk-bayesian-command-sort |
| 285 |
entry-str "\n" |
| 286 |
context "\n"))) |
| 287 |
;; send debugging messages |
| 288 |
(skk-bayesian-debug-message "Search: entry-str=%s" entry-str) |
| 289 |
(skk-bayesian-debug-message "Search: context=%s" context) |
| 290 |
(skk-bayesian-debug-message "Search: sorted-entry=%s" sorted-entry) |
| 291 |
;; return sorted-entry or entry |
| 292 |
(if (and sorted-entry |
| 293 |
(listp sorted-entry)) |
| 294 |
(progn |
| 295 |
(setq skk-bayesian-last-context context |
| 296 |
skk-bayesian-corpus-last-sorted-entry sorted-entry) |
| 297 |
sorted-entry) |
| 298 |
entry)))) |
| 299 |
|
| 300 |
(defun skk-bayesian-update (henkan-buffer midasi okurigana word purge) |
| 301 |
(when skk-bayesian-last-context ;; entry $B$NMWAG$,(B 1 $B$N;~$O!"(Bnil |
| 302 |
(if (and skk-bayesian-corpus-make |
| 303 |
skk-bayesian-corpus-last-sorted-entry |
| 304 |
(not (string= word (car skk-bayesian-corpus-last-sorted-entry)))) |
| 305 |
;; $BBh0l8uJd$,4V0c$$$@$C$?;~(B |
| 306 |
(skk-bayesian-corpus-append 'bad-inference |
| 307 |
skk-bayesian-last-context |
| 308 |
midasi |
| 309 |
okurigana |
| 310 |
(car skk-bayesian-corpus-last-sorted-entry))) |
| 311 |
(add-hook 'post-command-hook 'skk-bayesian-check-modification-after-kakutei) |
| 312 |
(if skk-bayesian-pending-data-alist |
| 313 |
;; pending $B$7$F$$$?$N$rJ]B8(B |
| 314 |
(skk-bayesian-add-to-history)) |
| 315 |
;; pending $B3+;O(B |
| 316 |
(skk-bayesian-debug-message "Update: pending... word=%s" word) |
| 317 |
(setq skk-bayesian-number-of-command-after-kakutei -1);; $B3NDj$K(B 1 $B2s$+$+$k$N$G(B -1 |
| 318 |
(skk-bayesian-make-pending-data-alist |
| 319 |
word |
| 320 |
okurigana |
| 321 |
midasi |
| 322 |
henkan-buffer |
| 323 |
(with-current-buffer henkan-buffer |
| 324 |
;; skk-get-last-henkan-datum $B$O!"(Bbuffer-local $B$JJQ?t$rMQ$$$F$$$k!#(B |
| 325 |
;; skk-get-last-henkan-datum $B$O!"(Bskk-update-end-function $B$rFI$s(B |
| 326 |
;; $B$@8e$K99?7$5$l$k!#$3$3$G$O;H$($J$$!#(B |
| 327 |
(if skk-undo-kakutei-word-only |
| 328 |
(point-marker) |
| 329 |
(save-excursion |
| 330 |
(skk-bayesian-debug-message (prin1-to-string (point-marker))) |
| 331 |
(forward-char |
| 332 |
(- 0 |
| 333 |
(length okurigana) |
| 334 |
;; word $B$,Cm<a$r4^$s$G$$$k:]!"%P%C%U%!$KA^F~$5$l$kJ8;zNs$N(B |
| 335 |
;; $BD9$5$h$j$bD9$/$J$C$F$7$^$&$N$G!"(Bpoint $B$N0LCV$K$h$C$F$O(B |
| 336 |
;; beginning-of-buffer $B$N%(%i!<$H$J$k!#$3$3$GCm<a$r@Z$j<N$F$?(B |
| 337 |
;; word $B$ND9$5$r<hF@$7$F$*$1$P$=$NLdBj$O$J$$!#(B |
| 338 |
(length (car (skk-treat-strip-note-from-word word))))) |
| 339 |
(point-marker)))) |
| 340 |
skk-bayesian-last-context))) |
| 341 |
|
| 342 |
(defun skk-bayesian-check-modification-after-kakutei () |
| 343 |
;; skk-bayesian-max-commands-to-wait-for $B2s?t%3%^%s%I$,<B9T$5$l$l$P!"(B |
| 344 |
;; skk-bayesian-add-to-history $B$r<B9T$9$k!#(B |
| 345 |
(when skk-bayesian-pending-data-alist |
| 346 |
(setq skk-bayesian-number-of-command-after-kakutei |
| 347 |
(1+ skk-bayesian-number-of-command-after-kakutei)) |
| 348 |
(when (<= skk-bayesian-max-commands-to-wait-for |
| 349 |
skk-bayesian-number-of-command-after-kakutei) |
| 350 |
(skk-bayesian-add-to-history)))) |
| 351 |
|
| 352 |
(defun skk-bayesian-add-to-history () |
| 353 |
"`skk-bayesian-last-kakutei-word' $B$r!"(Bbskk $B$NMzNr$KDI2C$9$k!#$b(B |
| 354 |
$B$7!"(B`skk-bayesian-last-kakutei-word' $B$,JQ498e$K=$@5$5$l$F$$$?>l9g(B |
| 355 |
$B$ODI2C$7$J$$!#;29M(B:`skk-bayesian-max-commands-to-wait-for'$B!#(B" |
| 356 |
;; $B0MB8$7$F$$$kJQ?t(B |
| 357 |
;; skk-bayesian-pending-data-alist |
| 358 |
;; $BCm0U(B |
| 359 |
;; skk-get-last-henkan-datum $B$O!"?7$7$$3NDj$,(B pending $BCf$K5/$3$k$N$G!";H$($J$$!#(B |
| 360 |
(if (not (skk-bayesian-process-live-p)) |
| 361 |
(setq skk-bayesian-pending-data-alist nil)) |
| 362 |
(when (and skk-bayesian-pending-data-alist |
| 363 |
(buffer-live-p (skk-bayesian-get-pending-data-alist 'buffer))) |
| 364 |
(with-current-buffer (skk-bayesian-get-pending-data-alist 'buffer) |
| 365 |
(let* ((kakutei-word (skk-bayesian-get-pending-data-alist 'word)) |
| 366 |
(okurigana (skk-bayesian-get-pending-data-alist 'okurigana)) |
| 367 |
(kakutei-with-okuri (concat kakutei-word okurigana)) |
| 368 |
(word-len (length kakutei-with-okuri)) |
| 369 |
(midasi (skk-bayesian-get-pending-data-alist 'midasi)) |
| 370 |
;; henkan-point $B$O!"Aw$j2>L>$,$"$k>l9g$O!"Aw$j2>L>$N(B point |
| 371 |
(start (marker-position (skk-bayesian-get-pending-data-alist |
| 372 |
'henkan-point))) |
| 373 |
(end (+ start word-len)) |
| 374 |
(current-word (if (and (<= (point-min) start) (<= end (point-max))) |
| 375 |
(buffer-substring-no-properties start end))) |
| 376 |
(context (skk-bayesian-get-pending-data-alist 'context))) |
| 377 |
;; kakutei-word $B$,JQ99$5$l$F$$$k$+(B |
| 378 |
(if (not (string= current-word kakutei-with-okuri)) |
| 379 |
(progn |
| 380 |
(skk-bayesian-debug-message "Add: kakutei-word has been modified") |
| 381 |
(if skk-bayesian-corpus-make |
| 382 |
(skk-bayesian-corpus-append 'modified context midasi okurigana |
| 383 |
kakutei-word))) |
| 384 |
(skk-bayesian-debug-message "Add: context=%s" context) |
| 385 |
(skk-bayesian-debug-message "Add: kakutei-word=%s" kakutei-word) |
| 386 |
(if (skk-bayesian-read-process-output (concat skk-bayesian-command-add |
| 387 |
kakutei-word "\n" |
| 388 |
context "\n")) |
| 389 |
(skk-bayesian-debug-message "Add: done") |
| 390 |
(skk-bayesian-debug-message "Add: failed")) |
| 391 |
(if skk-bayesian-corpus-make |
| 392 |
(skk-bayesian-corpus-append 'positive context midasi okurigana |
| 393 |
kakutei-word))))) |
| 394 |
(setq skk-bayesian-pending-data-alist nil))) |
| 395 |
|
| 396 |
(defun skk-bayesian-save-history () |
| 397 |
"Save skk-bayesian history to `skk-bayesian-history-file'." |
| 398 |
(interactive) |
| 399 |
(if skk-bayesian-pending-data-alist |
| 400 |
;; pending $B$7$F$$$?$N$rJ]B8(B |
| 401 |
(skk-bayesian-add-to-history)) |
| 402 |
(when (skk-bayesian-process-live-p) |
| 403 |
(skk-message "skk-bayesian $B$NMzNr$rJ]B8$7$F$$$^$9(B..." |
| 404 |
"saving skk-bayesian history...") |
| 405 |
(if (skk-bayesian-read-process-output skk-bayesian-command-save) |
| 406 |
(skk-message "skk-bayesian $B$NMzNr$rJ]B8$7$F$$$^$9(B...$B40N;(B" |
| 407 |
"saving skk-bayesian history...done") |
| 408 |
(skk-message "skk-bayesian $B$NMzNr$rJ]B8$7$F$$$^$9(B...$B<:GT(B" |
| 409 |
"saving skk-bayesian history...failed")))) |
| 410 |
|
| 411 |
(defun skk-bayesian-restart-process () |
| 412 |
(if (skk-bayesian-process-live-p) (skk-bayesian-kill-process)) |
| 413 |
(let ((proc-buf (get-buffer-create (if skk-bayesian-debug |
| 414 |
"*skk-bayesian*" |
| 415 |
" *skk-bayesian*"))) |
| 416 |
(proc-name "skk-bayesian")) |
| 417 |
(skk-message "$B%W%m%;%9(B bskk $B$r5/F0$7$F$$$^$9(B..." |
| 418 |
"Launching a process, bskk...") |
| 419 |
(setq skk-bayesian-process |
| 420 |
(or (and skk-bayesian-prefer-server |
| 421 |
(condition-case err |
| 422 |
(open-network-stream proc-name |
| 423 |
proc-buf |
| 424 |
skk-bayesian-host skk-bayesian-port) |
| 425 |
(error (skk-bayesian-debug-message |
| 426 |
"Error: %s\n%s" |
| 427 |
(error-message-string err) |
| 428 |
"Running bskk as a sub process") |
| 429 |
nil))) |
| 430 |
(if skk-bayesian-debug |
| 431 |
(start-process proc-name |
| 432 |
proc-buf |
| 433 |
"ruby" "-S" "bskk" |
| 434 |
"-f" skk-bayesian-history-file |
| 435 |
"-v" "-d") |
| 436 |
(start-process proc-name |
| 437 |
proc-buf |
| 438 |
"ruby" "-S" "bskk" |
| 439 |
"-f" skk-bayesian-history-file)))) |
| 440 |
(if skk-bayesian-process |
| 441 |
(skk-message "$B%W%m%;%9(B bskk $B$r5/F0$7$F$$$^$9(B...$B40N;(B" |
| 442 |
"Launching a process, bskk...done") |
| 443 |
(skk-message "$B%W%m%;%9(B bskk $B$r5/F0$7$F$$$^$9(B...$B<:GT(B" |
| 444 |
"Launching a process, bskk...failed"))) |
| 445 |
(set-process-coding-system skk-bayesian-process |
| 446 |
skk-bayesian-coding-system |
| 447 |
skk-bayesian-coding-system) |
| 448 |
(if (eval-when-compile (featurep 'emacs)) |
| 449 |
(set-process-query-on-exit-flag skk-bayesian-process nil) |
| 450 |
(process-kill-without-query skk-bayesian-process))) |
| 451 |
|
| 452 |
(defun skk-bayesian-kill-process () |
| 453 |
"Kill skk-bayesian process." |
| 454 |
(interactive) |
| 455 |
(when skk-bayesian-process |
| 456 |
(let ((status (process-status skk-bayesian-process)) |
| 457 |
msg) |
| 458 |
(cond ((memq status '(open connect)) |
| 459 |
(delete-process skk-bayesian-process) |
| 460 |
(setq msg "close connection")) |
| 461 |
((eq status 'run) |
| 462 |
(signal-process (process-id skk-bayesian-process) 15) |
| 463 |
(setq msg "send SIGTERM=15"))) |
| 464 |
(message "Killed skk-bayesian process. %s" msg)) |
| 465 |
(setq skk-bayesian-process nil))) |
| 466 |
|
| 467 |
(defun skk-bayesian-init () |
| 468 |
"Set up skk-bayesian process." |
| 469 |
(interactive) |
| 470 |
(when (not (skk-bayesian-process-live-p)) |
| 471 |
(skk-bayesian-restart-process))) |
| 472 |
|
| 473 |
(provide 'skk-bayesian) |
| 474 |
|
| 475 |
(add-to-list 'skk-search-end-function 'skk-bayesian-search) |
| 476 |
(add-to-list 'skk-update-end-function 'skk-bayesian-update) |
| 477 |
(add-hook 'kill-emacs-hook |
| 478 |
(function (lambda () |
| 479 |
(skk-bayesian-save-history) |
| 480 |
(skk-bayesian-corpus-save) |
| 481 |
(skk-bayesian-kill-process)))) |
| 482 |
|
| 483 |
(skk-bayesian-init) |
| 484 |
|
| 485 |
|
| 486 |
;;; |
| 487 |
;;; functions for skk-bayesian-corpus |
| 488 |
;;; |
| 489 |
(defun skk-bayesian-corpus-init () |
| 490 |
(unless (buffer-live-p skk-bayesian-corpus-buffer) |
| 491 |
(setq skk-bayesian-corpus-buffer |
| 492 |
(get-buffer-create skk-bayesian-corpus-buffer-name)) )) |
| 493 |
|
| 494 |
(defun skk-bayesian-corpus-append (flag context midasi okurigana word) |
| 495 |
;; called by `skk-bayesian-add-to-history' |
| 496 |
;; if flag is non-nil then append data as positive data |
| 497 |
;; otherwise append data as negative data |
| 498 |
(when (and context midasi word) ;; okurigana can be nil |
| 499 |
(skk-bayesian-corpus-init) |
| 500 |
(with-current-buffer skk-bayesian-corpus-buffer |
| 501 |
(goto-char (point-max)) |
| 502 |
(insert (concat (cond ((eq flag 'positive) "+") |
| 503 |
((eq flag 'modified) "m") |
| 504 |
((eq flag 'bad-inference) "-") |
| 505 |
(t (error (concat "Error; invalid flag=" |
| 506 |
(prin1-to-string flag))))) |
| 507 |
midasi " [" |
| 508 |
okurigana "/" |
| 509 |
word "/]" |
| 510 |
context "\n"))))) |
| 511 |
|
| 512 |
(defun skk-bayesian-corpus-save () |
| 513 |
"Save corpus to `skk-bayesian-corpus-file'." |
| 514 |
(interactive) |
| 515 |
(if (let ((attrs (file-attributes skk-bayesian-corpus-file))) |
| 516 |
(or (not attrs) ;; $B%U%!%$%k$,B8:_$7$J$1$l$P!"(Battrs $B$O!"(Bnil |
| 517 |
(eq (nth 8 attrs) 0))) ;; $B%U%!%$%k%5%$%:$,(B 0 |
| 518 |
(with-temp-buffer |
| 519 |
(insert ";; + means positive |
| 520 |
;; m means modified after henkan |
| 521 |
;; - menas wrong inference |
| 522 |
;; m and - are usually negative. |
| 523 |
;; However, in some cases, m data would be correct henkan |
| 524 |
;; because we might delete correct henkan.") |
| 525 |
(write-file skk-bayesian-corpus-file))) |
| 526 |
(when skk-bayesian-corpus-buffer |
| 527 |
(with-current-buffer skk-bayesian-corpus-buffer |
| 528 |
(write-region (point-min) (point-max) skk-bayesian-corpus-file 'append) |
| 529 |
(delete-region (point-min) (point-max))))) |
| 530 |
|
| 531 |
;; Local Variables: |
| 532 |
;; indent-tabs-mode: nil |
| 533 |
;; End: |
| 534 |
|
| 535 |
;;; skk-bayesian.el ends here |