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 (/= (/ prev-ratio
10) (/ ratio
10))
284 (message "collected %2d%% ..." ratio
)
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
100) skkdic-okuri-nasi-entries-count
)
309 (if (/= (/ prev-ratio
10) (/ ratio
10))
311 (message "processed %2d%% ..." ratio
)
312 (setq prev-ratio ratio
)))
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 (get-buffer-create " *skkdic-unannotated*"))
334 (buf (get-buffer-create "*skkdic-work*")))
335 ;; Set skkbuf to an unannotated copy of the dictionary.
336 (with-current-buffer skkbuf
337 (insert-file-contents (expand-file-name filename
))
338 (re-search-forward "^[^;]")
339 (while (re-search-forward ";[^\n/]*/" nil t
)
340 (replace-match "/")))
341 ;; Setup and generate the header part of working buffer.
342 (with-current-buffer buf
344 (buffer-disable-undo)
345 (insert ";;; ja-dic.el --- dictionary for Japanese input method"
346 " -*-coding: utf-8; -*-\n"
347 ";;\tGenerated by the command `skkdic-convert'\n"
348 ";;\tOriginal SKK dictionary file: "
349 (file-relative-name (expand-file-name filename
) dirname
)
351 ";; This file is part of GNU Emacs.\n\n"
352 ";;; Commentary:\n\n"
353 ";; Do byte-compile this file again after any modification.\n\n"
354 ";;; Start of the header of the original SKK dictionary.\n\n")
358 (search-forward ";; okuri-ari")
362 (insert-buffer-substring skkbuf
1 pos
))
364 ";;; Code:\n\n(eval-when-compile (require 'ja-dic-cnv))\n\n")
366 ;; Generate the body part of working buffer.
370 ;; Convert okuri-ari entries.
371 (search-forward ";; okuri-nasi")
374 (narrow-to-region from to
)
375 (skkdic-convert-okuri-ari skkbuf buf
)
378 ;; Convert okuri-nasi postfix entries.
382 (re-search-forward "^\\cH")
383 (setq to
(match-beginning 0))
384 (narrow-to-region from to
)
385 (skkdic-convert-postfix skkbuf buf
)
388 ;; Convert okuri-nasi prefix entries.
390 (skkdic-convert-prefix skkbuf buf
)
393 (skkdic-collect-okuri-nasi)
395 ;; Convert okuri-nasi general entries.
396 (skkdic-convert-okuri-nasi skkbuf buf
)
399 (with-current-buffer buf
400 (goto-char (point-max))
401 (insert ";;\n(provide 'ja-dic)\n\n;;; ja-dic.el ends here\n")))
403 ;; Save the working buffer.
405 (set-visited-file-name (expand-file-name ja-dic-filename dirname
) t
)
406 (set-buffer-file-coding-system 'utf-8
)
409 (switch-to-buffer buf
)))
411 (defun batch-skkdic-convert ()
412 "Run `skkdic-convert' on the files remaining on the command line.
413 Use this from the command line, with `-batch';
414 it won't work in an interactive Emacs.
416 % emacs -batch -l ja-dic-cnv -f batch-skkdic-convert SKK-JISYO.L
417 to generate \"ja-dic.el\" from SKK dictionary file \"SKK-JISYO.L\".
418 To get complete usage, invoke:
419 % emacs -batch -l ja-dic-cnv -f batch-skkdic-convert -h"
420 (defvar command-line-args-left
) ; Avoid compiler warning.
421 (if (not noninteractive
)
422 (error "`batch-skkdic-convert' should be used only with -batch"))
423 (if (string= (car command-line-args-left
) "-h")
425 (message "To convert SKK-JISYO.L into skkdic.el:")
426 (message " %% emacs -batch -l ja-dic-cnv -f batch-skkdic-convert SKK-JISYO.L")
427 (message "To convert SKK-JISYO.L into DIR/ja-dic.el:")
428 (message " %% emacs -batch -l ja-dic-cnv -f batch-skkdic-convert -dir DIR SKK-JISYO.L"))
429 (let (targetdir filename
)
430 (if (string= (car command-line-args-left
) "-dir")
432 (setq command-line-args-left
(cdr command-line-args-left
))
433 (setq targetdir
(expand-file-name (car command-line-args-left
)))
434 (setq command-line-args-left
(cdr command-line-args-left
))))
435 (setq filename
(expand-file-name (car command-line-args-left
)))
436 (skkdic-convert filename targetdir
)))
440 ;; The following macros are expanded at byte-compiling time so that
441 ;; compiled code can be loaded quickly.
443 (defun skkdic-get-kana-compact-codes (kana)
444 (let* ((len (length kana
))
445 (vec (make-vector len
0))
449 (setq ch
(aref kana i
))
451 (if (< ch
128) ; CH is an ASCII letter for OKURIGANA,
452 (- ch
) ; represented by a negative code.
453 (if (= ch ?
\e$B
!<\e(B) ; `\e$B!<\e(B' is represented by 0.
455 (- (logand (encode-char ch
'japanese-jisx0208
) #xFF
) 32))))
459 (defun skkdic-extract-conversion-data (entry)
460 (string-match "^\\cj+[a-z]* " entry
)
461 (let ((kana (substring entry
(match-beginning 0) (1- (match-end 0))))
464 (while (string-match "[^ ]+" entry i
)
465 (setq candidates
(cons (match-string 0 entry
) candidates
))
466 (setq i
(match-end 0)))
467 (cons (skkdic-get-kana-compact-codes kana
) candidates
)))
469 (defmacro skkdic-set-okuri-ari
(&rest entries
)
470 `(defconst skkdic-okuri-ari
472 (map '(skkdic-okuri-ari))
475 (setq entry
(skkdic-extract-conversion-data (car l
)))
476 (set-nested-alist (car entry
) (cdr entry
) map
)
480 (defmacro skkdic-set-postfix
(&rest entries
)
481 `(defconst skkdic-postfix
487 (setq entry
(skkdic-extract-conversion-data (car l
)))
488 (setq len
(length (car entry
)))
491 (let ((entry2 (lookup-nested-alist (car entry
) map nil nil t
)))
492 (if (consp (car entry2
))
493 (let ((conversions (cdr entry
)))
495 (if (not (member (car conversions
) (car entry2
)))
496 (setcar entry2
(cons (car conversions
) (car entry2
))))
497 (setq conversions
(cdr conversions
))))
498 (set-nested-alist (car entry
) (cdr entry
) map
)))
503 (defmacro skkdic-set-prefix
(&rest entries
)
504 `(defconst skkdic-prefix
510 (setq entry
(skkdic-extract-conversion-data (car l
)))
511 (setq len
(length (car entry
)))
514 (let ((entry2 (lookup-nested-alist (car entry
) map len nil t
)))
515 (if (consp (car entry2
))
516 (let ((conversions (cdr entry
)))
518 (if (not (member (car conversions
) (car entry2
)))
519 (setcar entry2
(cons (car conversions
) (car entry2
))))
520 (setq conversions
(cdr conversions
))))
521 (set-nested-alist (car entry
) (cdr entry
) map len
)))
526 (defmacro skkdic-set-okuri-nasi
(&rest entries
)
527 `(defconst skkdic-okuri-nasi
529 (map '(skdic-okuri-nasi))
533 (setq count
(1+ count
))
534 (if (= (% count
10000) 0)
535 (message "%d entries" count
))
536 (setq entry
(skkdic-extract-conversion-data (car l
)))
537 (set-nested-alist (car entry
) (cdr entry
) map
)
541 (provide 'ja-dic-cnv
)
544 ;; coding: iso-2022-7bit
547 ;;; ja-dic-cnv.el ends here