1 ;;; ja-dic-cnv.el --- convert a Japanese dictionary (SKK-JISYO.L) to Emacs Lisp
3 ;; Copyright (C) 1995, 2000 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
6 ;; Keywords: mule, multilingual, Japanese
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
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 ;; To make a generated ja-dic.el smaller.
52 "Like `iso-2022-7bit' but no ASCII designation before SPC."
53 '(ascii nil nil nil t t nil t
)
54 '((safe-charsets . t
)))
56 (defun skkdic-convert-okuri-ari (skkbuf buf
)
57 (message "Processing OKURI-ARI entries ...")
58 (goto-char (point-min))
61 (insert ";; Setting okuri-ari entries.\n"
62 "(skkdic-set-okuri-ari\n"))
71 (insert-buffer-substring skkbuf from to
)
75 (delete-char 1) ; delete the first '/'
78 (delete-char -
1) ; delete the last '/'
79 (subst-char-in-region p
(point) ?
/ ?
'noundo
))
87 (defconst skkdic-postfix-list
'(skkdic-postfix-list))
89 (defconst skkdic-postfix-data
90 '(("\e$B$$$-\e(B" "\e$B9T\e(B")
91 ("\e$B$,$+$j\e(B" "\e$B78\e(B")
92 ("\e$B$,$/\e(B" "\e$B3X\e(B")
93 ("\e$B$,$o\e(B" "\e$B@n\e(B")
94 ("\e$B$7$c\e(B" "\e$B<R\e(B")
95 ("\e$B$7$e$&\e(B" "\e$B=8\e(B")
96 ("\e$B$7$g$&\e(B" "\e$B>^\e(B" "\e$B>k\e(B")
97 ("\e$B$8$g$&\e(B" "\e$B>k\e(B")
98 ("\e$B$;$s\e(B" "\e$B@~\e(B")
99 ("\e$B$@$1\e(B" "\e$B3Y\e(B")
100 ("\e$B$A$c$/\e(B" "\e$BCe\e(B")
101 ("\e$B$F$s\e(B" "\e$BE9\e(B")
102 ("\e$B$H$&$2\e(B" "\e$BF=\e(B")
103 ("\e$B$I$*$j\e(B" "\e$BDL$j\e(B")
104 ("\e$B$d$^\e(B" "\e$B;3\e(B")
105 ("\e$B$P$7\e(B" "\e$B66\e(B")
106 ("\e$B$O$D\e(B" "\e$BH/\e(B")
107 ("\e$B$b$/\e(B" "\e$BL\\e(B")
108 ("\e$B$f$-\e(B" "\e$B9T\e(B")))
110 (defun skkdic-convert-postfix (skkbuf buf
)
111 (message "Processing POSTFIX entries ...")
112 (goto-char (point-min))
115 (insert ";; Setting postfix entries.\n"
116 "(skkdic-set-postfix\n"))
118 ;; Initialize SKKDIC-POSTFIX-LIST by predefined data
119 ;; SKKDIC-POSTFIX-DATA.
122 (let ((l skkdic-postfix-data
)
123 kana candidates entry
)
125 (setq kana
(car (car l
)) candidates
(cdr (car l
)))
128 (insert " " (car candidates
))
129 (setq entry
(lookup-nested-alist (car candidates
)
130 skkdic-postfix-list nil nil t
))
131 (if (consp (car entry
))
132 (setcar entry
(cons kana
(car entry
)))
133 (set-nested-alist (car candidates
) (list kana
)
134 skkdic-postfix-list
))
135 (setq candidates
(cdr candidates
)))
139 ;; Search postfix entries.
140 (while (re-search-forward "^[#<>?]\\(\\(\\cH\\|\e$B!<\e(B\\)+\\) " nil t
)
141 (let ((kana (match-string 1))
143 (while (looking-at "/[#0-9 ]*\\([^/\n]*\\)/")
144 (setq str
(match-string 1))
145 (if (not (member str candidates
))
146 (setq candidates
(cons str candidates
)))
147 (goto-char (match-end 1)))
152 (insert " " (car candidates
))
153 (let ((entry (lookup-nested-alist (car candidates
)
154 skkdic-postfix-list nil nil t
)))
155 (if (consp (car entry
))
156 (if (not (member kana
(car entry
)))
157 (setcar entry
(cons kana
(car entry
))))
158 (set-nested-alist (car candidates
) (list kana
)
159 skkdic-postfix-list
)))
160 (setq candidates
(cdr candidates
)))
166 (defconst skkdic-prefix-list
'(skkdic-prefix-list))
168 (defun skkdic-convert-prefix (skkbuf buf
)
169 (message "Processing PREFIX entries ...")
170 (goto-char (point-min))
173 (insert ";; Setting prefix entries.\n"
174 "(skkdic-set-prefix\n"))
176 (while (re-search-forward "^\\(\\(\\cH\\|\e$B!<\e(B\\)+\\)[<>?] " nil t
)
177 (let ((kana (match-string 1))
179 (while (looking-at "/\\([^/\n]+\\)/")
180 (setq str
(match-string 1))
181 (if (not (member str candidates
))
182 (setq candidates
(cons str candidates
)))
183 (goto-char (match-end 1)))
188 (insert " " (car candidates
))
189 (set-nested-alist (car candidates
) kana skkdic-prefix-list
)
190 (setq candidates
(cdr candidates
)))
196 ;; FROM and TO point the head and tail of "/J../J../.../".
197 (defun skkdic-get-candidate-list (from to
)
200 (while (re-search-forward "/[^/ \n]+" to t
)
201 (setq candidates
(cons (buffer-substring (1+ (match-beginning 0))
206 ;; Return entry for STR from nested alist ALIST.
207 (defsubst skkdic-get-entry
(str alist
)
208 (car (lookup-nested-alist str alist nil nil t
)))
211 (defconst skkdic-word-list
'(skkdic-word-list))
213 ;; Return t if substring of STR (between FROM and TO) can be broken up
214 ;; to chunks all of which can be derived from another entry in SKK
215 ;; dictionary. SKKBUF is the buffer where the original SKK dictionary
216 ;; is visited, KANA is the current entry for STR. FIRST is t iff this
217 ;; is called at top level.
219 (defun skkdic-breakup-string (skkbuf kana str from to
&optional first
)
220 (let ((len (- to from
)))
222 (let ((min-idx (+ from
2))
223 (idx (if first
(1- to
) to
))
225 (while (and (not found
) (>= idx min-idx
))
226 (let ((kana2-list (skkdic-get-entry
227 (substring str from idx
)
229 (if (or (and (consp kana2-list
)
230 (let ((kana-len (length kana
))
234 (setq kana2
(car kana2-list
))
235 (if (string-match kana2 kana
)
236 (throw 'skkdic-tag t
))
237 (setq kana2-list
(cdr kana2-list
)))))
239 (skkdic-breakup-string skkbuf kana str
241 (and (stringp kana2-list
)
242 (string-match kana2-list kana
)))
244 (setq idx
(1- idx
)))))
248 (let ((kana2 (skkdic-get-entry
249 (substring str from
(1+ from
))
250 skkdic-prefix-list
)))
252 (eq (string-match kana2 kana
) 0)))
253 (skkdic-breakup-string skkbuf kana str
(1+ from
) to
))
256 (let ((kana2-list (skkdic-get-entry
257 (substring str from to
)
258 skkdic-postfix-list
)))
259 (and (consp kana2-list
)
263 (setq kana2
(car kana2-list
))
265 (substring kana
(- (length kana2
))))
266 (throw 'skkdic-tag t
))
267 (setq kana2-list
(cdr kana2-list
)))))))))))
269 ;; Return list of candidates which excludes some from CANDIDATES.
270 ;; Excluded candidates can be derived from another entry.
272 (defun skkdic-reduced-candidates (skkbuf kana candidates
)
275 (setq elt
(car candidates
))
276 (if (or (= (length elt
) 1)
277 (and (string-match "^\\cj" elt
)
278 (not (skkdic-breakup-string skkbuf kana elt
0 (length elt
)
280 (setq l
(cons elt l
)))
281 (setq candidates
(cdr candidates
)))
284 (defconst skkdic-okuri-nasi-entries
(list nil
))
285 (defconst skkdic-okuri-nasi-entries-count
0)
287 (defun skkdic-collect-okuri-nasi ()
288 (message "Collecting OKURI-NASI entries ...")
292 (while (re-search-forward "^\\(\\(\\cH\\|\e$B!<\e(B\\)+\\) \\(/\\cj.*\\)/$"
294 (let ((kana (match-string 1))
295 (candidates (skkdic-get-candidate-list (match-beginning 3)
297 (setq skkdic-okuri-nasi-entries
298 (cons (cons kana candidates
) skkdic-okuri-nasi-entries
)
299 skkdic-okuri-nasi-entries-count
300 (1+ skkdic-okuri-nasi-entries-count
))
301 (setq ratio
(floor (/ (* (point) 100.0) (point-max))))
302 (if (/= ratio prev-ratio
)
304 (message "collected %2d%% %s ..." ratio kana
)
305 (setq prev-ratio ratio
)))
307 (let ((entry (lookup-nested-alist (car candidates
)
308 skkdic-word-list nil nil t
)))
309 (if (consp (car entry
))
310 (setcar entry
(cons kana
(car entry
)))
311 (set-nested-alist (car candidates
) (list kana
)
313 (setq candidates
(cdr candidates
))))))))
315 (defun skkdic-convert-okuri-nasi (skkbuf buf
)
316 (message "Processing OKURI-NASI entries ...")
319 (insert ";; Setting okuri-nasi entries.\n"
320 "(skkdic-set-okuri-nasi\n")
321 (let ((l (nreverse skkdic-okuri-nasi-entries
))
326 (let ((kana (car (car l
)))
327 (candidates (cdr (car l
))))
328 (setq ratio
(/ (* count
1000) skkdic-okuri-nasi-entries-count
)
330 (if (/= prev-ratio
(/ ratio
10))
332 (message "processed %2d%% %s ..." (/ ratio
10) kana
)
333 (setq prev-ratio
(/ ratio
10))))
335 (skkdic-reduced-candidates skkbuf kana candidates
))
339 (insert " " (car candidates
))
340 (setq candidates
(cdr candidates
)))
345 (defun skkdic-convert (filename &optional dirname
)
346 "Generate Emacs lisp file form Japanese dictionary file FILENAME.
347 The format of the dictionary file should be the same as SKK dictionaries.
348 Optional argument DIRNAME if specified is the directory name under which
349 the generated Emacs Lisp is saved.
350 The name of generated file is specified by the variable `ja-dic-filename'."
351 (interactive "FSKK dictionary file: ")
352 (message "Reading file \"%s\" ..." filename
)
353 (let* ((coding-system-for-read 'euc-japan
)
354 (skkbuf(find-file-noselect (expand-file-name filename
)))
355 (buf (get-buffer-create "*skkdic-work*")))
357 ;; Setup and generate the header part of working buffer.
360 (buffer-disable-undo)
361 (insert ";;; ja-dic.el --- dictionary for Japanese input method"
362 " -*-coding: iso-2022-jp;-*-\n"
363 ";;\tGenerated by the command `skkdic-convert'\n"
364 ";;\tDate: " (current-time-string) "\n"
365 ";;\tOriginal SKK dictionary file: "
366 (file-name-nondirectory filename
)
368 ";; This file is part of GNU Emacs.\n\n"
369 ";;; Commentary:\n\n"
370 ";; Do byte-compile this file again after any modification.\n\n"
371 ";;; Start of the header of the original SKK dictionary.\n\n")
376 (search-forward ";; okuri-ari")
380 (insert-buffer-substring skkbuf
1 pos
))
382 ";;; Code:\n\n(eval-when-compile (require 'ja-dic-cnv))\n\n")
384 ;; Generate the body part of working buffer.
388 ;; Convert okuri-ari entries.
389 (search-forward ";; okuri-nasi")
392 (narrow-to-region from to
)
393 (skkdic-convert-okuri-ari skkbuf buf
)
396 ;; Convert okuri-nasi postfix entries.
400 (re-search-forward "^\\cH")
401 (setq to
(match-beginning 0))
402 (narrow-to-region from to
)
403 (skkdic-convert-postfix skkbuf buf
)
406 ;; Convert okuri-nasi prefix entries.
408 (skkdic-convert-prefix skkbuf buf
)
411 (skkdic-collect-okuri-nasi)
413 ;; Convert okuri-nasi general entries.
414 (skkdic-convert-okuri-nasi skkbuf buf
)
419 (goto-char (point-max))
420 (insert ";;\n(provide 'ja-dic)\n\n;;; ja-dic.el ends here\n")))
422 ;; Save the working buffer.
424 (set-visited-file-name (expand-file-name ja-dic-filename dirname
) t
)
425 (set-buffer-file-coding-system 'iso-2022-7bit-short
)
428 (switch-to-buffer buf
)))
430 (defun batch-skkdic-convert ()
431 "Run `skkdic-convert' on the files remaining on the command line.
432 Use this from the command line, with `-batch';
433 it won't work in an interactive Emacs.
435 % emacs -batch -l ja-dic-cnv -f batch-skkdic-convert SKK-JISYO.L
436 to generate \"ja-dic.el\" from SKK dictionary file \"SKK-JISYO.L\".
437 To get complete usage, invoke:
438 % emacs -batch -l ja-dic-cnv -f batch-skkdic-convert -h"
439 (defvar command-line-args-left
) ; Avoid compiler warning.
440 (if (not noninteractive
)
441 (error "`batch-skkdic-convert' should be used only with -batch"))
442 (if (string= (car command-line-args-left
) "-h")
444 (message "To convert SKK-JISYO.L into skkdic.el:")
445 (message " %% emacs -batch -l ja-dic-cnv -f batch-skkdic-convert SKK-JISYO.L")
446 (message "To convert SKK-JISYO.L into DIR/ja-dic.el:")
447 (message " %% emacs -batch -l ja-dic-cnv -f batch-skkdic-convert -dir DIR SKK-JISYO.L"))
448 (let (targetdir filename
)
449 (if (string= (car command-line-args-left
) "-dir")
451 (setq command-line-args-left
(cdr command-line-args-left
))
452 (setq targetdir
(expand-file-name (car command-line-args-left
)))
453 (setq command-line-args-left
(cdr command-line-args-left
))))
454 (setq filename
(expand-file-name (car command-line-args-left
)))
455 (message "Converting %s to %s ..." filename ja-dic-filename
)
456 (message "It takes around 10 minutes even on Sun SS20.")
457 (skkdic-convert filename targetdir
)
458 (message "Do byte-compile the created file by:")
459 (message " %% emacs -batch -f batch-byte-compile %s" ja-dic-filename
)
464 ;; The following macros are expanded at byte-compiling time so that
465 ;; compiled code can be loaded quickly.
467 (defun skkdic-get-kana-compact-codes (kana)
468 (let* ((len (length kana
))
469 (vec (make-vector len
0))
473 (setq ch
(aref kana i
))
475 (if (< ch
128) ; CH is an ASCII letter for OKURIGANA,
476 (- ch
) ; represented by a negative code.
477 (if (= ch ?
\e$B
!<\e(B) ; `\e$B!<\e(B' is represented by 0.
479 (- (nth 2 (split-char ch
)) 32))))
483 (defun skkdic-extract-conversion-data (entry)
484 (string-match "^\\cj+[a-z]* " entry
)
485 (let ((kana (substring entry
(match-beginning 0) (1- (match-end 0))))
488 (while (string-match "[^ ]+" entry i
)
489 (setq candidates
(cons (match-string 0 entry
) candidates
))
490 (setq i
(match-end 0)))
491 (cons (skkdic-get-kana-compact-codes kana
) candidates
)))
493 (defmacro skkdic-set-okuri-ari
(&rest entries
)
494 `(defconst skkdic-okuri-ari
496 (map '(skkdic-okuri-ari))
499 (setq entry
(skkdic-extract-conversion-data (car l
)))
500 (set-nested-alist (car entry
) (cdr entry
) map
)
504 (defmacro skkdic-set-postfix
(&rest entries
)
505 `(defconst skkdic-postfix
511 (setq entry
(skkdic-extract-conversion-data (car l
)))
512 (setq len
(length (car entry
)))
515 (let ((entry2 (lookup-nested-alist (car entry
) map nil 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
)))
527 (defmacro skkdic-set-prefix
(&rest entries
)
528 `(defconst skkdic-prefix
534 (setq entry
(skkdic-extract-conversion-data (car l
)))
535 (setq len
(length (car entry
)))
538 (let ((entry2 (lookup-nested-alist (car entry
) map len nil t
)))
539 (if (consp (car entry2
))
540 (let ((conversions (cdr entry
)))
542 (if (not (member (car conversions
) (car entry2
)))
543 (setcar entry2
(cons (car conversions
) (car entry2
))))
544 (setq conversions
(cdr conversions
))))
545 (set-nested-alist (car entry
) (cdr entry
) map len
)))
550 (defmacro skkdic-set-okuri-nasi
(&rest entries
)
551 `(defconst skkdic-okuri-nasi
553 (map '(skdic-okuri-nasi))
557 (setq count
(1+ count
))
558 (if (= (% count
10000) 0)
559 (message (format "%d entries" count
)))
560 (setq entry
(skkdic-extract-conversion-data (car l
)))
561 (set-nested-alist (car entry
) (cdr entry
) map
)
565 (provide 'ja-dic-cnv
)
568 ;; coding: iso-2022-7bit
571 ;;; ja-dic-cnv.el ends here