1 ;;; ja-dic-cnv.el --- convert a Japanese dictionary (SKK-JISYO.L) to Emacs Lisp
3 ;; Copyright (C) 1995, 2000
4 ;; National Institute of Advanced Industrial Science and Technology (AIST)
5 ;; Registration Number H14PRO021
7 ;; Keywords: mule, multilingual, Japanese
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; SKK is a Japanese input method running on Mule created by Masahiko
29 ;; Sato <masahiko@sato.riec.tohoku.ac.jp>. Here we provide utilities
30 ;; to handle a dictionary distributed with SKK so that a different
31 ;; input method (e.g. quail-japanese) can utilize the dictionary.
33 ;; The format of SKK dictionary is quite simple. Each line has the
34 ;; form "KANASTRING /CONV1/CONV2/.../" which means KANASTRING (\e$B2>L>J8\e(B
35 ;; \e$B;zNs\e(B) can be converted to one of CONVi. CONVi is a Kanji (\e$B4A;z\e(B)
36 ;; and Kana (\e$B2>L>\e(B) mixed string.
38 ;; KANASTRING may have a trailing ASCII letter for Okurigana (\e$BAw$j2>L>\e(B)
39 ;; information. For instance, the trailing letter `k' means that one
40 ;; of the following Okurigana is allowed: \e$B$+$-$/$1$3\e(B. So, in that
41 ;; case, the string "KANASTRING\e$B$/\e(B" can be converted to one of "CONV1\e$B$/\e(B",
42 ;; CONV2\e$B$/\e(B, ...
46 ;; Name of a file to generate from SKK dictionary.
47 (defvar ja-dic-filename
"ja-dic.el")
49 ;; To make a generated ja-dic.el smaller.
53 "Like `iso-2022-7bit' but no ASCII designation before SPC."
54 '(ascii nil nil nil t t nil t
)
55 '((safe-charsets . t
)))
57 (defun skkdic-convert-okuri-ari (skkbuf buf
)
58 (message "Processing OKURI-ARI entries ...")
59 (goto-char (point-min))
62 (insert ";; Setting okuri-ari entries.\n"
63 "(skkdic-set-okuri-ari\n"))
72 (insert-buffer-substring skkbuf from to
)
76 (delete-char 1) ; delete the first '/'
79 (delete-char -
1) ; delete the last '/'
80 (subst-char-in-region p
(point) ?
/ ?
'noundo
))
88 (defconst skkdic-postfix-list
'(skkdic-postfix-list))
90 (defconst skkdic-postfix-data
91 '(("\e$B$$$-\e(B" "\e$B9T\e(B")
92 ("\e$B$,$+$j\e(B" "\e$B78\e(B")
93 ("\e$B$,$/\e(B" "\e$B3X\e(B")
94 ("\e$B$,$o\e(B" "\e$B@n\e(B")
95 ("\e$B$7$c\e(B" "\e$B<R\e(B")
96 ("\e$B$7$e$&\e(B" "\e$B=8\e(B")
97 ("\e$B$7$g$&\e(B" "\e$B>^\e(B" "\e$B>k\e(B")
98 ("\e$B$8$g$&\e(B" "\e$B>k\e(B")
99 ("\e$B$;$s\e(B" "\e$B@~\e(B")
100 ("\e$B$@$1\e(B" "\e$B3Y\e(B")
101 ("\e$B$A$c$/\e(B" "\e$BCe\e(B")
102 ("\e$B$F$s\e(B" "\e$BE9\e(B")
103 ("\e$B$H$&$2\e(B" "\e$BF=\e(B")
104 ("\e$B$I$*$j\e(B" "\e$BDL$j\e(B")
105 ("\e$B$d$^\e(B" "\e$B;3\e(B")
106 ("\e$B$P$7\e(B" "\e$B66\e(B")
107 ("\e$B$O$D\e(B" "\e$BH/\e(B")
108 ("\e$B$b$/\e(B" "\e$BL\\e(B")
109 ("\e$B$f$-\e(B" "\e$B9T\e(B")))
111 (defun skkdic-convert-postfix (skkbuf buf
)
112 (message "Processing POSTFIX entries ...")
113 (goto-char (point-min))
116 (insert ";; Setting postfix entries.\n"
117 "(skkdic-set-postfix\n"))
119 ;; Initialize SKKDIC-POSTFIX-LIST by predefined data
120 ;; SKKDIC-POSTFIX-DATA.
123 (let ((l skkdic-postfix-data
)
124 kana candidates entry
)
126 (setq kana
(car (car l
)) candidates
(cdr (car l
)))
129 (insert " " (car candidates
))
130 (setq entry
(lookup-nested-alist (car candidates
)
131 skkdic-postfix-list nil nil t
))
132 (if (consp (car entry
))
133 (setcar entry
(cons kana
(car entry
)))
134 (set-nested-alist (car candidates
) (list kana
)
135 skkdic-postfix-list
))
136 (setq candidates
(cdr candidates
)))
140 ;; Search postfix entries.
141 (while (re-search-forward "^[#<>?]\\(\\(\\cH\\|\e$B!<\e(B\\)+\\) " nil t
)
142 (let ((kana (match-string 1))
144 (while (looking-at "/[#0-9 ]*\\([^/\n]*\\)/")
145 (setq str
(match-string 1))
146 (if (not (member str candidates
))
147 (setq candidates
(cons str candidates
)))
148 (goto-char (match-end 1)))
153 (insert " " (car candidates
))
154 (let ((entry (lookup-nested-alist (car candidates
)
155 skkdic-postfix-list nil nil t
)))
156 (if (consp (car entry
))
157 (if (not (member kana
(car entry
)))
158 (setcar entry
(cons kana
(car entry
))))
159 (set-nested-alist (car candidates
) (list kana
)
160 skkdic-postfix-list
)))
161 (setq candidates
(cdr candidates
)))
167 (defconst skkdic-prefix-list
'(skkdic-prefix-list))
169 (defun skkdic-convert-prefix (skkbuf buf
)
170 (message "Processing PREFIX entries ...")
171 (goto-char (point-min))
174 (insert ";; Setting prefix entries.\n"
175 "(skkdic-set-prefix\n"))
177 (while (re-search-forward "^\\(\\(\\cH\\|\e$B!<\e(B\\)+\\)[<>?] " nil t
)
178 (let ((kana (match-string 1))
180 (while (looking-at "/\\([^/\n]+\\)/")
181 (setq str
(match-string 1))
182 (if (not (member str candidates
))
183 (setq candidates
(cons str candidates
)))
184 (goto-char (match-end 1)))
189 (insert " " (car candidates
))
190 (set-nested-alist (car candidates
) kana skkdic-prefix-list
)
191 (setq candidates
(cdr candidates
)))
197 ;; FROM and TO point the head and tail of "/J../J../.../".
198 (defun skkdic-get-candidate-list (from to
)
201 (while (re-search-forward "/[^/ \n]+" to t
)
202 (setq candidates
(cons (buffer-substring (1+ (match-beginning 0))
207 ;; Return entry for STR from nested alist ALIST.
208 (defsubst skkdic-get-entry
(str alist
)
209 (car (lookup-nested-alist str alist nil nil t
)))
212 (defconst skkdic-word-list
'(skkdic-word-list))
214 ;; Return t if substring of STR (between FROM and TO) can be broken up
215 ;; to chunks all of which can be derived from another entry in SKK
216 ;; dictionary. SKKBUF is the buffer where the original SKK dictionary
217 ;; is visited, KANA is the current entry for STR. FIRST is t iff this
218 ;; is called at top level.
220 (defun skkdic-breakup-string (skkbuf kana str from to
&optional first
)
221 (let ((len (- to from
)))
223 (let ((min-idx (+ from
2))
224 (idx (if first
(1- to
) to
))
226 (while (and (not found
) (>= idx min-idx
))
227 (let ((kana2-list (skkdic-get-entry
228 (substring str from idx
)
230 (if (or (and (consp kana2-list
)
231 (let ((kana-len (length kana
))
235 (setq kana2
(car kana2-list
))
236 (if (string-match kana2 kana
)
237 (throw 'skkdic-tag t
))
238 (setq kana2-list
(cdr kana2-list
)))))
240 (skkdic-breakup-string skkbuf kana str
242 (and (stringp kana2-list
)
243 (string-match kana2-list kana
)))
245 (setq idx
(1- idx
)))))
249 (let ((kana2 (skkdic-get-entry
250 (substring str from
(1+ from
))
251 skkdic-prefix-list
)))
253 (eq (string-match kana2 kana
) 0)))
254 (skkdic-breakup-string skkbuf kana str
(1+ from
) to
))
257 (let ((kana2-list (skkdic-get-entry
258 (substring str from to
)
259 skkdic-postfix-list
)))
260 (and (consp kana2-list
)
264 (setq kana2
(car kana2-list
))
266 (substring kana
(- (length kana2
))))
267 (throw 'skkdic-tag t
))
268 (setq kana2-list
(cdr kana2-list
)))))))))))
270 ;; Return list of candidates which excludes some from CANDIDATES.
271 ;; Excluded candidates can be derived from another entry.
273 (defun skkdic-reduced-candidates (skkbuf kana candidates
)
276 (setq elt
(car candidates
))
277 (if (or (= (length elt
) 1)
278 (and (string-match "^\\cj" elt
)
279 (not (skkdic-breakup-string skkbuf kana elt
0 (length elt
)
281 (setq l
(cons elt l
)))
282 (setq candidates
(cdr candidates
)))
285 (defvar skkdic-okuri-nasi-entries
(list nil
))
286 (defvar skkdic-okuri-nasi-entries-count
0)
288 (defun skkdic-collect-okuri-nasi ()
289 (message "Collecting OKURI-NASI entries ...")
293 (while (re-search-forward "^\\(\\(\\cH\\|\e$B!<\e(B\\)+\\) \\(/\\cj.*\\)/$"
295 (let ((kana (match-string 1))
296 (candidates (skkdic-get-candidate-list (match-beginning 3)
298 (setq skkdic-okuri-nasi-entries
299 (cons (cons kana candidates
) skkdic-okuri-nasi-entries
)
300 skkdic-okuri-nasi-entries-count
301 (1+ skkdic-okuri-nasi-entries-count
))
302 (setq ratio
(floor (/ (* (point) 100.0) (point-max))))
303 (if (/= ratio prev-ratio
)
305 (message "collected %2d%% %s ..." ratio kana
)
306 (setq prev-ratio ratio
)))
308 (let ((entry (lookup-nested-alist (car candidates
)
309 skkdic-word-list nil nil t
)))
310 (if (consp (car entry
))
311 (setcar entry
(cons kana
(car entry
)))
312 (set-nested-alist (car candidates
) (list kana
)
314 (setq candidates
(cdr candidates
))))))))
316 (defun skkdic-convert-okuri-nasi (skkbuf buf
)
317 (message "Processing OKURI-NASI entries ...")
320 (insert ";; Setting okuri-nasi entries.\n"
321 "(skkdic-set-okuri-nasi\n")
322 (let ((l (nreverse skkdic-okuri-nasi-entries
))
327 (let ((kana (car (car l
)))
328 (candidates (cdr (car l
))))
329 (setq ratio
(/ (* count
1000) skkdic-okuri-nasi-entries-count
)
331 (if (/= prev-ratio
(/ ratio
10))
333 (message "processed %2d%% %s ..." (/ ratio
10) kana
)
334 (setq prev-ratio
(/ ratio
10))))
336 (skkdic-reduced-candidates skkbuf kana candidates
))
340 (insert " " (car candidates
))
341 (setq candidates
(cdr candidates
)))
346 (defun skkdic-convert (filename &optional dirname
)
347 "Generate Emacs Lisp file form Japanese dictionary file FILENAME.
348 The format of the dictionary file should be the same as SKK dictionaries.
349 Optional argument DIRNAME if specified is the directory name under which
350 the generated Emacs Lisp is saved.
351 The name of generated file is specified by the variable `ja-dic-filename'."
352 (interactive "FSKK dictionary file: ")
353 (message "Reading file \"%s\" ..." filename
)
354 (let* ((coding-system-for-read 'euc-japan
)
355 (skkbuf(find-file-noselect (expand-file-name filename
)))
356 (buf (get-buffer-create "*skkdic-work*")))
358 ;; Setup and generate the header part of working buffer.
361 (buffer-disable-undo)
362 (insert ";;; ja-dic.el --- dictionary for Japanese input method"
363 " -*-coding: iso-2022-jp;-*-\n"
364 ";;\tGenerated by the command `skkdic-convert'\n"
365 ";;\tDate: " (current-time-string) "\n"
366 ";;\tOriginal SKK dictionary file: "
367 (file-name-nondirectory filename
)
369 ";; This file is part of GNU Emacs.\n\n"
370 ";;; Commentary:\n\n"
371 ";; Do byte-compile this file again after any modification.\n\n"
372 ";;; Start of the header of the original SKK dictionary.\n\n")
377 (search-forward ";; okuri-ari")
381 (insert-buffer-substring skkbuf
1 pos
))
383 ";;; Code:\n\n(eval-when-compile (require 'ja-dic-cnv))\n\n")
385 ;; Generate the body part of working buffer.
389 ;; Convert okuri-ari entries.
390 (search-forward ";; okuri-nasi")
393 (narrow-to-region from to
)
394 (skkdic-convert-okuri-ari skkbuf buf
)
397 ;; Convert okuri-nasi postfix entries.
401 (re-search-forward "^\\cH")
402 (setq to
(match-beginning 0))
403 (narrow-to-region from to
)
404 (skkdic-convert-postfix skkbuf buf
)
407 ;; Convert okuri-nasi prefix entries.
409 (skkdic-convert-prefix skkbuf buf
)
412 (skkdic-collect-okuri-nasi)
414 ;; Convert okuri-nasi general entries.
415 (skkdic-convert-okuri-nasi skkbuf buf
)
420 (goto-char (point-max))
421 (insert ";;\n(provide 'ja-dic)\n\n;;; ja-dic.el ends here\n")))
423 ;; Save the working buffer.
425 (set-visited-file-name (expand-file-name ja-dic-filename dirname
) t
)
426 (set-buffer-file-coding-system 'iso-2022-7bit-short
)
429 (switch-to-buffer buf
)))
431 (defun batch-skkdic-convert ()
432 "Run `skkdic-convert' on the files remaining on the command line.
433 Use this from the command line, with `-batch';
434 it won't work in an interactive Emacs.
436 % emacs -batch -l ja-dic-cnv -f batch-skkdic-convert SKK-JISYO.L
437 to generate \"ja-dic.el\" from SKK dictionary file \"SKK-JISYO.L\".
438 To get complete usage, invoke:
439 % emacs -batch -l ja-dic-cnv -f batch-skkdic-convert -h"
440 (defvar command-line-args-left
) ; Avoid compiler warning.
441 (if (not noninteractive
)
442 (error "`batch-skkdic-convert' should be used only with -batch"))
443 (if (string= (car command-line-args-left
) "-h")
445 (message "To convert SKK-JISYO.L into skkdic.el:")
446 (message " %% emacs -batch -l ja-dic-cnv -f batch-skkdic-convert SKK-JISYO.L")
447 (message "To convert SKK-JISYO.L into DIR/ja-dic.el:")
448 (message " %% emacs -batch -l ja-dic-cnv -f batch-skkdic-convert -dir DIR SKK-JISYO.L"))
449 (let (targetdir filename
)
450 (if (string= (car command-line-args-left
) "-dir")
452 (setq command-line-args-left
(cdr command-line-args-left
))
453 (setq targetdir
(expand-file-name (car command-line-args-left
)))
454 (setq command-line-args-left
(cdr command-line-args-left
))))
455 (setq filename
(expand-file-name (car command-line-args-left
)))
456 (message "Converting %s to %s ..." filename ja-dic-filename
)
457 (message "It takes around 10 minutes even on Sun SS20.")
458 (skkdic-convert filename targetdir
)
459 (message "Do byte-compile the created file by:")
460 (message " %% emacs -batch -f batch-byte-compile %s" ja-dic-filename
)
465 ;; The following macros are expanded at byte-compiling time so that
466 ;; compiled code can be loaded quickly.
468 (defun skkdic-get-kana-compact-codes (kana)
469 (let* ((len (length kana
))
470 (vec (make-vector len
0))
474 (setq ch
(aref kana i
))
476 (if (< ch
128) ; CH is an ASCII letter for OKURIGANA,
477 (- ch
) ; represented by a negative code.
478 (if (= ch ?
\e$B
!<\e(B) ; `\e$B!<\e(B' is represented by 0.
480 (- (nth 2 (split-char ch
)) 32))))
484 (defun skkdic-extract-conversion-data (entry)
485 (string-match "^\\cj+[a-z]* " entry
)
486 (let ((kana (substring entry
(match-beginning 0) (1- (match-end 0))))
489 (while (string-match "[^ ]+" entry i
)
490 (setq candidates
(cons (match-string 0 entry
) candidates
))
491 (setq i
(match-end 0)))
492 (cons (skkdic-get-kana-compact-codes kana
) candidates
)))
494 (defmacro skkdic-set-okuri-ari
(&rest entries
)
495 `(defconst skkdic-okuri-ari
497 (map '(skkdic-okuri-ari))
500 (setq entry
(skkdic-extract-conversion-data (car l
)))
501 (set-nested-alist (car entry
) (cdr entry
) map
)
505 (defmacro skkdic-set-postfix
(&rest entries
)
506 `(defconst skkdic-postfix
512 (setq entry
(skkdic-extract-conversion-data (car l
)))
513 (setq len
(length (car entry
)))
516 (let ((entry2 (lookup-nested-alist (car entry
) map nil nil t
)))
517 (if (consp (car entry2
))
518 (let ((conversions (cdr entry
)))
520 (if (not (member (car conversions
) (car entry2
)))
521 (setcar entry2
(cons (car conversions
) (car entry2
))))
522 (setq conversions
(cdr conversions
))))
523 (set-nested-alist (car entry
) (cdr entry
) map
)))
528 (defmacro skkdic-set-prefix
(&rest entries
)
529 `(defconst skkdic-prefix
535 (setq entry
(skkdic-extract-conversion-data (car l
)))
536 (setq len
(length (car entry
)))
539 (let ((entry2 (lookup-nested-alist (car entry
) map len nil t
)))
540 (if (consp (car entry2
))
541 (let ((conversions (cdr entry
)))
543 (if (not (member (car conversions
) (car entry2
)))
544 (setcar entry2
(cons (car conversions
) (car entry2
))))
545 (setq conversions
(cdr conversions
))))
546 (set-nested-alist (car entry
) (cdr entry
) map len
)))
551 (defmacro skkdic-set-okuri-nasi
(&rest entries
)
552 `(defconst skkdic-okuri-nasi
554 (map '(skdic-okuri-nasi))
558 (setq count
(1+ count
))
559 (if (= (% count
10000) 0)
560 (message (format "%d entries" count
)))
561 (setq entry
(skkdic-extract-conversion-data (car l
)))
562 (set-nested-alist (car entry
) (cdr entry
) map
)
566 (provide 'ja-dic-cnv
)
569 ;; coding: iso-2022-7bit
572 ;;; arch-tag: dec06fb0-8118-45b1-80d7-dc360b6fd3b2
573 ;;; ja-dic-cnv.el ends here