1 ;;; ja-dic-cnv.el --- convert a Japanese dictionary (SKK-JISYO.L) to Emacs Lisp
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 ;; National Institute of Advanced Industrial Science and Technology (AIST)
6 ;; Registration Number H14PRO021
8 ;; Keywords: i18n, mule, multilingual, Japanese
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; SKK is a Japanese input method running on Mule created by Masahiko
28 ;; Sato <masahiko@sato.riec.tohoku.ac.jp>. Here we provide utilities
29 ;; to handle a dictionary distributed with SKK so that a different
30 ;; input method (e.g. quail-japanese) can utilize the dictionary.
32 ;; The format of SKK dictionary is quite simple. Each line has the
33 ;; form "KANASTRING /CONV1/CONV2/.../" which means KANASTRING (\e$B2>L>J8\e(B
34 ;; \e$B;zNs\e(B) can be converted to one of CONVi. CONVi is a Kanji (\e$B4A;z\e(B)
35 ;; and Kana (\e$B2>L>\e(B) mixed string.
37 ;; KANASTRING may have a trailing ASCII letter for Okurigana (\e$BAw$j2>L>\e(B)
38 ;; information. For instance, the trailing letter `k' means that one
39 ;; of the following Okurigana is allowed: \e$B$+$-$/$1$3\e(B. So, in that
40 ;; case, the string "KANASTRING\e$B$/\e(B" can be converted to one of "CONV1\e$B$/\e(B",
41 ;; CONV2\e$B$/\e(B, ...
45 ;; Name of a file to generate from SKK dictionary.
46 (defvar ja-dic-filename
"ja-dic.el")
48 (defun skkdic-convert-okuri-ari (skkbuf buf
)
49 (message "Processing OKURI-ARI entries ...")
50 (goto-char (point-min))
51 (with-current-buffer buf
52 (insert ";; Setting okuri-ari entries.\n"
53 "(skkdic-set-okuri-ari\n"))
55 (if (/= (following-char) ?
>)
57 (to (line-end-position)))
58 (with-current-buffer buf
59 (insert-buffer-substring skkbuf from to
)
63 (delete-char 1) ; delete the first '/'
66 (delete-char -
1) ; delete the last '/'
67 (subst-char-in-region p
(point) ?
/ ?
'noundo
))
71 (with-current-buffer buf
74 (defconst skkdic-postfix-list
'(skkdic-postfix-list))
76 (defconst skkdic-postfix-data
77 '(("\e$B$$$-\e(B" "\e$B9T\e(B")
78 ("\e$B$,$+$j\e(B" "\e$B78\e(B")
79 ("\e$B$,$/\e(B" "\e$B3X\e(B")
80 ("\e$B$,$o\e(B" "\e$B@n\e(B")
81 ("\e$B$7$c\e(B" "\e$B<R\e(B")
82 ("\e$B$7$e$&\e(B" "\e$B=8\e(B")
83 ("\e$B$7$g$&\e(B" "\e$B>^\e(B" "\e$B>k\e(B")
84 ("\e$B$8$g$&\e(B" "\e$B>k\e(B")
85 ("\e$B$;$s\e(B" "\e$B@~\e(B")
86 ("\e$B$@$1\e(B" "\e$B3Y\e(B")
87 ("\e$B$A$c$/\e(B" "\e$BCe\e(B")
88 ("\e$B$F$s\e(B" "\e$BE9\e(B")
89 ("\e$B$H$&$2\e(B" "\e$BF=\e(B")
90 ("\e$B$I$*$j\e(B" "\e$BDL$j\e(B")
91 ("\e$B$d$^\e(B" "\e$B;3\e(B")
92 ("\e$B$P$7\e(B" "\e$B66\e(B")
93 ("\e$B$O$D\e(B" "\e$BH/\e(B")
94 ("\e$B$b$/\e(B" "\e$BL\\e(B")
95 ("\e$B$f$-\e(B" "\e$B9T\e(B")))
97 (defun skkdic-convert-postfix (skkbuf buf
)
98 (message "Processing POSTFIX entries ...")
99 (goto-char (point-min))
100 (with-current-buffer buf
101 (insert ";; Setting postfix entries.\n"
102 "(skkdic-set-postfix\n"))
104 ;; Initialize SKKDIC-POSTFIX-LIST by predefined data
105 ;; SKKDIC-POSTFIX-DATA.
106 (with-current-buffer buf
107 (let ((l skkdic-postfix-data
)
108 kana candidates entry
)
110 (setq kana
(car (car l
)) candidates
(cdr (car l
)))
113 (insert " " (car candidates
))
114 (setq entry
(lookup-nested-alist (car candidates
)
115 skkdic-postfix-list nil nil t
))
116 (if (consp (car entry
))
117 (setcar entry
(cons kana
(car entry
)))
118 (set-nested-alist (car candidates
) (list kana
)
119 skkdic-postfix-list
))
120 (setq candidates
(cdr candidates
)))
124 ;; Search postfix entries.
125 (while (re-search-forward "^[#<>?]\\(\\(\\cH\\|\e$B!<\e(B\\)+\\) " nil t
)
126 (let ((kana (match-string 1))
128 (while (looking-at "/[#0-9 ]*\\([^/\n]*\\)/")
129 (setq str
(match-string 1))
130 (if (not (member str candidates
))
131 (setq candidates
(cons str candidates
)))
132 (goto-char (match-end 1)))
133 (with-current-buffer buf
136 (insert " " (car candidates
))
137 (let ((entry (lookup-nested-alist (car candidates
)
138 skkdic-postfix-list nil nil t
)))
139 (if (consp (car entry
))
140 (if (not (member kana
(car entry
)))
141 (setcar entry
(cons kana
(car entry
))))
142 (set-nested-alist (car candidates
) (list kana
)
143 skkdic-postfix-list
)))
144 (setq candidates
(cdr candidates
)))
146 (with-current-buffer buf
149 (defconst skkdic-prefix-list
'(skkdic-prefix-list))
151 (defun skkdic-convert-prefix (skkbuf buf
)
152 (message "Processing PREFIX entries ...")
153 (goto-char (point-min))
154 (with-current-buffer buf
155 (insert ";; Setting prefix entries.\n"
156 "(skkdic-set-prefix\n"))
158 (while (re-search-forward "^\\(\\(\\cH\\|\e$B!<\e(B\\)+\\)[<>?] " nil t
)
159 (let ((kana (match-string 1))
161 (while (looking-at "/\\([^/\n]+\\)/")
162 (setq str
(match-string 1))
163 (if (not (member str candidates
))
164 (setq candidates
(cons str candidates
)))
165 (goto-char (match-end 1)))
166 (with-current-buffer buf
169 (insert " " (car candidates
))
170 (set-nested-alist (car candidates
) kana skkdic-prefix-list
)
171 (setq candidates
(cdr candidates
)))
173 (with-current-buffer buf
176 ;; FROM and TO point the head and tail of "/J../J../.../".
177 (defun skkdic-get-candidate-list (from to
)
180 (while (re-search-forward "/[^/ \n]+" to t
)
181 (setq candidates
(cons (buffer-substring (1+ (match-beginning 0))
186 ;; Return entry for STR from nested alist ALIST.
187 (defsubst skkdic-get-entry
(str alist
)
188 (car (lookup-nested-alist str alist nil nil t
)))
191 (defconst skkdic-word-list
'(skkdic-word-list))
193 ;; Return t if substring of STR (between FROM and TO) can be broken up
194 ;; to chunks all of which can be derived from another entry in SKK
195 ;; dictionary. SKKBUF is the buffer where the original SKK dictionary
196 ;; is visited, KANA is the current entry for STR. FIRST is t only if
197 ;; this is called at top level.
199 (defun skkdic-breakup-string (skkbuf kana str from to
&optional first
)
200 (let ((len (- to from
)))
202 (let ((min-idx (+ from
2))
203 (idx (if first
(1- to
) to
))
205 (while (and (not found
) (>= idx min-idx
))
206 (let ((kana2-list (skkdic-get-entry
207 (substring str from idx
)
209 (if (or (and (consp kana2-list
)
210 (let ((kana-len (length kana
))
214 (setq kana2
(car kana2-list
))
215 (if (string-match kana2 kana
)
216 (throw 'skkdic-tag t
))
217 (setq kana2-list
(cdr kana2-list
)))))
219 (skkdic-breakup-string skkbuf kana str
221 (and (stringp kana2-list
)
222 (string-match kana2-list kana
)))
224 (setq idx
(1- idx
)))))
228 (let ((kana2 (skkdic-get-entry
229 (substring str from
(1+ from
))
230 skkdic-prefix-list
)))
232 (eq (string-match kana2 kana
) 0)))
233 (skkdic-breakup-string skkbuf kana str
(1+ from
) to
))
236 (let ((kana2-list (skkdic-get-entry
237 (substring str from to
)
238 skkdic-postfix-list
)))
239 (and (consp kana2-list
)
243 (setq kana2
(car kana2-list
))
245 (substring kana
(- (length kana2
))))
246 (throw 'skkdic-tag t
))
247 (setq kana2-list
(cdr kana2-list
)))))))))))
249 ;; Return list of candidates which excludes some from CANDIDATES.
250 ;; Excluded candidates can be derived from another entry.
252 (defun skkdic-reduced-candidates (skkbuf kana candidates
)
255 (setq elt
(car candidates
))
256 (if (or (= (length elt
) 1)
257 (and (string-match "^\\cj" elt
)
258 (not (skkdic-breakup-string skkbuf kana elt
0 (length elt
)
260 (setq l
(cons elt l
)))
261 (setq candidates
(cdr candidates
)))
264 (defvar skkdic-okuri-nasi-entries
(list nil
))
265 (defvar skkdic-okuri-nasi-entries-count
0)
267 (defun skkdic-collect-okuri-nasi ()
268 (message "Collecting OKURI-NASI entries ...")
272 (while (re-search-forward "^\\(\\(\\cH\\|\e$B!<\e(B\\)+\\) \\(/\\cj.*\\)/$"
274 (let ((kana (match-string 1))
275 (candidates (skkdic-get-candidate-list (match-beginning 3)
277 (setq skkdic-okuri-nasi-entries
278 (cons (cons kana candidates
) skkdic-okuri-nasi-entries
)
279 skkdic-okuri-nasi-entries-count
280 (1+ skkdic-okuri-nasi-entries-count
))
281 (setq ratio
(floor (/ (* (point) 100.0) (point-max))))
282 (if (/= ratio prev-ratio
)
284 (message "collected %2d%% %s ..." ratio kana
)
285 (setq prev-ratio ratio
)))
287 (let ((entry (lookup-nested-alist (car candidates
)
288 skkdic-word-list nil nil t
)))
289 (if (consp (car entry
))
290 (setcar entry
(cons kana
(car entry
)))
291 (set-nested-alist (car candidates
) (list kana
)
293 (setq candidates
(cdr candidates
))))))))
295 (defun skkdic-convert-okuri-nasi (skkbuf buf
)
296 (message "Processing OKURI-NASI entries ...")
297 (with-current-buffer buf
298 (insert ";; Setting okuri-nasi entries.\n"
299 "(skkdic-set-okuri-nasi\n")
300 (let ((l (nreverse skkdic-okuri-nasi-entries
))
305 (let ((kana (car (car l
)))
306 (candidates (cdr (car l
))))
307 (setq ratio
(/ (* count
1000) skkdic-okuri-nasi-entries-count
)
309 (if (/= prev-ratio
(/ ratio
10))
311 (message "processed %2d%% %s ..." (/ ratio
10) kana
)
312 (setq prev-ratio
(/ ratio
10))))
314 (skkdic-reduced-candidates skkbuf kana candidates
))
318 (insert " " (car candidates
))
319 (setq candidates
(cdr candidates
)))
324 (defun skkdic-convert (filename &optional dirname
)
325 "Generate Emacs Lisp file form Japanese dictionary file FILENAME.
326 The format of the dictionary file should be the same as SKK dictionaries.
327 Optional argument DIRNAME if specified is the directory name under which
328 the generated Emacs Lisp is saved.
329 The name of generated file is specified by the variable `ja-dic-filename'."
330 (interactive "FSKK dictionary file: ")
331 (message "Reading file \"%s\" ..." filename
)
332 (let* ((coding-system-for-read 'euc-japan
)
333 (skkbuf(find-file-noselect (expand-file-name filename
)))
334 (buf (get-buffer-create "*skkdic-work*")))
335 ;; Setup and generate the header part of working buffer.
336 (with-current-buffer buf
338 (buffer-disable-undo)
339 (insert ";;; ja-dic.el --- dictionary for Japanese input method"
340 " -*-coding: euc-japan; -*-\n"
341 ";;\tGenerated by the command `skkdic-convert'\n"
342 ";;\tDate: " (current-time-string) "\n"
343 ";;\tOriginal SKK dictionary file: "
344 (file-relative-name (expand-file-name filename
) dirname
)
346 ";; This file is part of GNU Emacs.\n\n"
347 ";;; Commentary:\n\n"
348 ";; Do byte-compile this file again after any modification.\n\n"
349 ";;; Start of the header of the original SKK dictionary.\n\n")
354 (search-forward ";; okuri-ari")
358 (insert-buffer-substring skkbuf
1 pos
))
360 ";;; Code:\n\n(eval-when-compile (require 'ja-dic-cnv))\n\n")
362 ;; Generate the body part of working buffer.
366 ;; Convert okuri-ari entries.
367 (search-forward ";; okuri-nasi")
370 (narrow-to-region from to
)
371 (skkdic-convert-okuri-ari skkbuf buf
)
374 ;; Convert okuri-nasi postfix entries.
378 (re-search-forward "^\\cH")
379 (setq to
(match-beginning 0))
380 (narrow-to-region from to
)
381 (skkdic-convert-postfix skkbuf buf
)
384 ;; Convert okuri-nasi prefix entries.
386 (skkdic-convert-prefix skkbuf buf
)
389 (skkdic-collect-okuri-nasi)
391 ;; Convert okuri-nasi general entries.
392 (skkdic-convert-okuri-nasi skkbuf buf
)
395 (with-current-buffer buf
396 (goto-char (point-max))
397 (insert ";;\n(provide 'ja-dic)\n\n;;; ja-dic.el ends here\n")))
399 ;; Save the working buffer.
401 (set-visited-file-name (expand-file-name ja-dic-filename dirname
) t
)
402 (set-buffer-file-coding-system 'euc-japan
)
405 (switch-to-buffer buf
)))
407 (defun batch-skkdic-convert ()
408 "Run `skkdic-convert' on the files remaining on the command line.
409 Use this from the command line, with `-batch';
410 it won't work in an interactive Emacs.
412 % emacs -batch -l ja-dic-cnv -f batch-skkdic-convert SKK-JISYO.L
413 to generate \"ja-dic.el\" from SKK dictionary file \"SKK-JISYO.L\".
414 To get complete usage, invoke:
415 % emacs -batch -l ja-dic-cnv -f batch-skkdic-convert -h"
416 (defvar command-line-args-left
) ; Avoid compiler warning.
417 (if (not noninteractive
)
418 (error "`batch-skkdic-convert' should be used only with -batch"))
419 (if (string= (car command-line-args-left
) "-h")
421 (message "To convert SKK-JISYO.L into skkdic.el:")
422 (message " %% emacs -batch -l ja-dic-cnv -f batch-skkdic-convert SKK-JISYO.L")
423 (message "To convert SKK-JISYO.L into DIR/ja-dic.el:")
424 (message " %% emacs -batch -l ja-dic-cnv -f batch-skkdic-convert -dir DIR SKK-JISYO.L"))
425 (let (targetdir filename
)
426 (if (string= (car command-line-args-left
) "-dir")
428 (setq command-line-args-left
(cdr command-line-args-left
))
429 (setq targetdir
(expand-file-name (car command-line-args-left
)))
430 (setq command-line-args-left
(cdr command-line-args-left
))))
431 (setq filename
(expand-file-name (car command-line-args-left
)))
432 (message "Converting %s to %s ..." filename ja-dic-filename
)
433 (message "It takes around 10 minutes even on Sun SS20.")
434 (skkdic-convert filename targetdir
)
435 (message "Do byte-compile the created file by:")
436 (message " %% emacs -batch -f batch-byte-compile %s" ja-dic-filename
)
441 ;; The following macros are expanded at byte-compiling time so that
442 ;; compiled code can be loaded quickly.
444 (defun skkdic-get-kana-compact-codes (kana)
445 (let* ((len (length kana
))
446 (vec (make-vector len
0))
450 (setq ch
(aref kana i
))
452 (if (< ch
128) ; CH is an ASCII letter for OKURIGANA,
453 (- ch
) ; represented by a negative code.
454 (if (= ch ?
\e$B
!<\e(B) ; `\e$B!<\e(B' is represented by 0.
456 (- (logand (encode-char ch
'japanese-jisx0208
) #xFF
) 32))))
460 (defun skkdic-extract-conversion-data (entry)
461 (string-match "^\\cj+[a-z]* " entry
)
462 (let ((kana (substring entry
(match-beginning 0) (1- (match-end 0))))
465 (while (string-match "[^ ]+" entry i
)
466 (setq candidates
(cons (match-string 0 entry
) candidates
))
467 (setq i
(match-end 0)))
468 (cons (skkdic-get-kana-compact-codes kana
) candidates
)))
470 (defmacro skkdic-set-okuri-ari
(&rest entries
)
471 `(defconst skkdic-okuri-ari
473 (map '(skkdic-okuri-ari))
476 (setq entry
(skkdic-extract-conversion-data (car l
)))
477 (set-nested-alist (car entry
) (cdr entry
) map
)
481 (defmacro skkdic-set-postfix
(&rest entries
)
482 `(defconst skkdic-postfix
488 (setq entry
(skkdic-extract-conversion-data (car l
)))
489 (setq len
(length (car entry
)))
492 (let ((entry2 (lookup-nested-alist (car entry
) map nil nil t
)))
493 (if (consp (car entry2
))
494 (let ((conversions (cdr entry
)))
496 (if (not (member (car conversions
) (car entry2
)))
497 (setcar entry2
(cons (car conversions
) (car entry2
))))
498 (setq conversions
(cdr conversions
))))
499 (set-nested-alist (car entry
) (cdr entry
) map
)))
504 (defmacro skkdic-set-prefix
(&rest entries
)
505 `(defconst skkdic-prefix
511 (setq entry
(skkdic-extract-conversion-data (car l
)))
512 (setq len
(length (car entry
)))
515 (let ((entry2 (lookup-nested-alist (car entry
) map len nil t
)))
516 (if (consp (car entry2
))
517 (let ((conversions (cdr entry
)))
519 (if (not (member (car conversions
) (car entry2
)))
520 (setcar entry2
(cons (car conversions
) (car entry2
))))
521 (setq conversions
(cdr conversions
))))
522 (set-nested-alist (car entry
) (cdr entry
) map len
)))
527 (defmacro skkdic-set-okuri-nasi
(&rest entries
)
528 `(defconst skkdic-okuri-nasi
530 (map '(skdic-okuri-nasi))
534 (setq count
(1+ count
))
535 (if (= (% count
10000) 0)
536 (message "%d entries" count
))
537 (setq entry
(skkdic-extract-conversion-data (car l
)))
538 (set-nested-alist (car entry
) (cdr entry
) map
)
542 (provide 'ja-dic-cnv
)
545 ;; coding: iso-2022-7bit
548 ;;; ja-dic-cnv.el ends here