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
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 ;; To make a generated ja-dic.el smaller.
49 (define-coding-system 'iso-2022-7bit-short
50 "Like `iso-2022-7bit' but no ASCII designation before SPC."
51 :coding-type
'iso-2022
53 :charset-list
'iso-2022
54 :designation
[(ascii t
) nil nil nil
]
55 :flags
'(short 7-bit designation
))
57 (defun skkdic-convert-okuri-ari (skkbuf buf
)
58 (message "Processing OKURI-ARI entries ...")
59 (goto-char (point-min))
60 (with-current-buffer buf
61 (insert ";; Setting okuri-ari entries.\n"
62 "(skkdic-set-okuri-ari\n"))
69 (with-current-buffer buf
70 (insert-buffer-substring skkbuf from to
)
74 (delete-char 1) ; delete the first '/'
77 (delete-char -
1) ; delete the last '/'
78 (subst-char-in-region p
(point) ?
/ ?
'noundo
))
82 (with-current-buffer buf
85 (defconst skkdic-postfix-list
'(skkdic-postfix-list))
87 (defconst skkdic-postfix-data
88 '(("\e$B$$$-\e(B" "\e$B9T\e(B")
89 ("\e$B$,$+$j\e(B" "\e$B78\e(B")
90 ("\e$B$,$/\e(B" "\e$B3X\e(B")
91 ("\e$B$,$o\e(B" "\e$B@n\e(B")
92 ("\e$B$7$c\e(B" "\e$B<R\e(B")
93 ("\e$B$7$e$&\e(B" "\e$B=8\e(B")
94 ("\e$B$7$g$&\e(B" "\e$B>^\e(B" "\e$B>k\e(B")
95 ("\e$B$8$g$&\e(B" "\e$B>k\e(B")
96 ("\e$B$;$s\e(B" "\e$B@~\e(B")
97 ("\e$B$@$1\e(B" "\e$B3Y\e(B")
98 ("\e$B$A$c$/\e(B" "\e$BCe\e(B")
99 ("\e$B$F$s\e(B" "\e$BE9\e(B")
100 ("\e$B$H$&$2\e(B" "\e$BF=\e(B")
101 ("\e$B$I$*$j\e(B" "\e$BDL$j\e(B")
102 ("\e$B$d$^\e(B" "\e$B;3\e(B")
103 ("\e$B$P$7\e(B" "\e$B66\e(B")
104 ("\e$B$O$D\e(B" "\e$BH/\e(B")
105 ("\e$B$b$/\e(B" "\e$BL\\e(B")
106 ("\e$B$f$-\e(B" "\e$B9T\e(B")))
108 (defun skkdic-convert-postfix (skkbuf buf
)
109 (message "Processing POSTFIX entries ...")
110 (goto-char (point-min))
111 (with-current-buffer buf
112 (insert ";; Setting postfix entries.\n"
113 "(skkdic-set-postfix\n"))
115 ;; Initialize SKKDIC-POSTFIX-LIST by predefined data
116 ;; SKKDIC-POSTFIX-DATA.
117 (with-current-buffer buf
118 (let ((l skkdic-postfix-data
)
119 kana candidates entry
)
121 (setq kana
(car (car l
)) candidates
(cdr (car l
)))
124 (insert " " (car candidates
))
125 (setq entry
(lookup-nested-alist (car candidates
)
126 skkdic-postfix-list nil nil t
))
127 (if (consp (car entry
))
128 (setcar entry
(cons kana
(car entry
)))
129 (set-nested-alist (car candidates
) (list kana
)
130 skkdic-postfix-list
))
131 (setq candidates
(cdr candidates
)))
135 ;; Search postfix entries.
136 (while (re-search-forward "^[#<>?]\\(\\(\\cH\\|\e$B!<\e(B\\)+\\) " nil t
)
137 (let ((kana (match-string 1))
139 (while (looking-at "/[#0-9 ]*\\([^/\n]*\\)/")
140 (setq str
(match-string 1))
141 (if (not (member str candidates
))
142 (setq candidates
(cons str candidates
)))
143 (goto-char (match-end 1)))
144 (with-current-buffer buf
147 (insert " " (car candidates
))
148 (let ((entry (lookup-nested-alist (car candidates
)
149 skkdic-postfix-list nil nil t
)))
150 (if (consp (car entry
))
151 (if (not (member kana
(car entry
)))
152 (setcar entry
(cons kana
(car entry
))))
153 (set-nested-alist (car candidates
) (list kana
)
154 skkdic-postfix-list
)))
155 (setq candidates
(cdr candidates
)))
157 (with-current-buffer buf
160 (defconst skkdic-prefix-list
'(skkdic-prefix-list))
162 (defun skkdic-convert-prefix (skkbuf buf
)
163 (message "Processing PREFIX entries ...")
164 (goto-char (point-min))
165 (with-current-buffer buf
166 (insert ";; Setting prefix entries.\n"
167 "(skkdic-set-prefix\n"))
169 (while (re-search-forward "^\\(\\(\\cH\\|\e$B!<\e(B\\)+\\)[<>?] " nil t
)
170 (let ((kana (match-string 1))
172 (while (looking-at "/\\([^/\n]+\\)/")
173 (setq str
(match-string 1))
174 (if (not (member str candidates
))
175 (setq candidates
(cons str candidates
)))
176 (goto-char (match-end 1)))
177 (with-current-buffer buf
180 (insert " " (car candidates
))
181 (set-nested-alist (car candidates
) kana skkdic-prefix-list
)
182 (setq candidates
(cdr candidates
)))
184 (with-current-buffer buf
187 ;; FROM and TO point the head and tail of "/J../J../.../".
188 (defun skkdic-get-candidate-list (from to
)
191 (while (re-search-forward "/[^/ \n]+" to t
)
192 (setq candidates
(cons (buffer-substring (1+ (match-beginning 0))
197 ;; Return entry for STR from nested alist ALIST.
198 (defsubst skkdic-get-entry
(str alist
)
199 (car (lookup-nested-alist str alist nil nil t
)))
202 (defconst skkdic-word-list
'(skkdic-word-list))
204 ;; Return t if substring of STR (between FROM and TO) can be broken up
205 ;; to chunks all of which can be derived from another entry in SKK
206 ;; dictionary. SKKBUF is the buffer where the original SKK dictionary
207 ;; is visited, KANA is the current entry for STR. FIRST is t only if
208 ;; this is called at top level.
210 (defun skkdic-breakup-string (skkbuf kana str from to
&optional first
)
211 (let ((len (- to from
)))
213 (let ((min-idx (+ from
2))
214 (idx (if first
(1- to
) to
))
216 (while (and (not found
) (>= idx min-idx
))
217 (let ((kana2-list (skkdic-get-entry
218 (substring str from idx
)
220 (if (or (and (consp kana2-list
)
221 (let ((kana-len (length kana
))
225 (setq kana2
(car kana2-list
))
226 (if (string-match kana2 kana
)
227 (throw 'skkdic-tag t
))
228 (setq kana2-list
(cdr kana2-list
)))))
230 (skkdic-breakup-string skkbuf kana str
232 (and (stringp kana2-list
)
233 (string-match kana2-list kana
)))
235 (setq idx
(1- idx
)))))
239 (let ((kana2 (skkdic-get-entry
240 (substring str from
(1+ from
))
241 skkdic-prefix-list
)))
243 (eq (string-match kana2 kana
) 0)))
244 (skkdic-breakup-string skkbuf kana str
(1+ from
) to
))
247 (let ((kana2-list (skkdic-get-entry
248 (substring str from to
)
249 skkdic-postfix-list
)))
250 (and (consp kana2-list
)
254 (setq kana2
(car kana2-list
))
256 (substring kana
(- (length kana2
))))
257 (throw 'skkdic-tag t
))
258 (setq kana2-list
(cdr kana2-list
)))))))))))
260 ;; Return list of candidates which excludes some from CANDIDATES.
261 ;; Excluded candidates can be derived from another entry.
263 (defun skkdic-reduced-candidates (skkbuf kana candidates
)
266 (setq elt
(car candidates
))
267 (if (or (= (length elt
) 1)
268 (and (string-match "^\\cj" elt
)
269 (not (skkdic-breakup-string skkbuf kana elt
0 (length elt
)
271 (setq l
(cons elt l
)))
272 (setq candidates
(cdr candidates
)))
275 (defvar skkdic-okuri-nasi-entries
(list nil
))
276 (defvar skkdic-okuri-nasi-entries-count
0)
278 (defun skkdic-collect-okuri-nasi ()
279 (message "Collecting OKURI-NASI entries ...")
283 (while (re-search-forward "^\\(\\(\\cH\\|\e$B!<\e(B\\)+\\) \\(/\\cj.*\\)/$"
285 (let ((kana (match-string 1))
286 (candidates (skkdic-get-candidate-list (match-beginning 3)
288 (setq skkdic-okuri-nasi-entries
289 (cons (cons kana candidates
) skkdic-okuri-nasi-entries
)
290 skkdic-okuri-nasi-entries-count
291 (1+ skkdic-okuri-nasi-entries-count
))
292 (setq ratio
(floor (/ (* (point) 100.0) (point-max))))
293 (if (/= ratio prev-ratio
)
295 (message "collected %2d%% %s ..." ratio kana
)
296 (setq prev-ratio ratio
)))
298 (let ((entry (lookup-nested-alist (car candidates
)
299 skkdic-word-list nil nil t
)))
300 (if (consp (car entry
))
301 (setcar entry
(cons kana
(car entry
)))
302 (set-nested-alist (car candidates
) (list kana
)
304 (setq candidates
(cdr candidates
))))))))
306 (defun skkdic-convert-okuri-nasi (skkbuf buf
)
307 (message "Processing OKURI-NASI entries ...")
308 (with-current-buffer buf
309 (insert ";; Setting okuri-nasi entries.\n"
310 "(skkdic-set-okuri-nasi\n")
311 (let ((l (nreverse skkdic-okuri-nasi-entries
))
316 (let ((kana (car (car l
)))
317 (candidates (cdr (car l
))))
318 (setq ratio
(/ (* count
1000) skkdic-okuri-nasi-entries-count
)
320 (if (/= prev-ratio
(/ ratio
10))
322 (message "processed %2d%% %s ..." (/ ratio
10) kana
)
323 (setq prev-ratio
(/ ratio
10))))
325 (skkdic-reduced-candidates skkbuf kana candidates
))
329 (insert " " (car candidates
))
330 (setq candidates
(cdr candidates
)))
335 (defun skkdic-convert (filename &optional dirname
)
336 "Generate Emacs Lisp file form Japanese dictionary file FILENAME.
337 The format of the dictionary file should be the same as SKK dictionaries.
338 Optional argument DIRNAME if specified is the directory name under which
339 the generated Emacs Lisp is saved.
340 The name of generated file is specified by the variable `ja-dic-filename'."
341 (interactive "FSKK dictionary file: ")
342 (message "Reading file \"%s\" ..." filename
)
343 (let* ((coding-system-for-read 'euc-japan
)
344 (skkbuf(find-file-noselect (expand-file-name filename
)))
345 (buf (get-buffer-create "*skkdic-work*")))
346 ;; Setup and generate the header part of working buffer.
347 (with-current-buffer buf
349 (buffer-disable-undo)
350 (insert ";;; ja-dic.el --- dictionary for Japanese input method"
351 " -*-coding: iso-2022-jp; byte-compile-disable-print-circle:t; -*-\n"
352 ";;\tGenerated by the command `skkdic-convert'\n"
353 ";;\tDate: " (current-time-string) "\n"
354 ";;\tOriginal SKK dictionary file: "
355 (file-relative-name (expand-file-name filename
) dirname
)
357 ";; This file is part of GNU Emacs.\n\n"
358 ";;; Commentary:\n\n"
359 ";; Do byte-compile this file again after any modification.\n\n"
360 ";;; Start of the header of the original SKK dictionary.\n\n")
365 (search-forward ";; okuri-ari")
369 (insert-buffer-substring skkbuf
1 pos
))
371 ";;; Code:\n\n(eval-when-compile (require 'ja-dic-cnv))\n\n")
373 ;; Generate the body part of working buffer.
377 ;; Convert okuri-ari entries.
378 (search-forward ";; okuri-nasi")
381 (narrow-to-region from to
)
382 (skkdic-convert-okuri-ari skkbuf buf
)
385 ;; Convert okuri-nasi postfix entries.
389 (re-search-forward "^\\cH")
390 (setq to
(match-beginning 0))
391 (narrow-to-region from to
)
392 (skkdic-convert-postfix skkbuf buf
)
395 ;; Convert okuri-nasi prefix entries.
397 (skkdic-convert-prefix skkbuf buf
)
400 (skkdic-collect-okuri-nasi)
402 ;; Convert okuri-nasi general entries.
403 (skkdic-convert-okuri-nasi skkbuf buf
)
406 (with-current-buffer buf
407 (goto-char (point-max))
408 (insert ";;\n(provide 'ja-dic)\n\n;;; ja-dic.el ends here\n")))
410 ;; Save the working buffer.
412 (set-visited-file-name (expand-file-name ja-dic-filename dirname
) t
)
413 (set-buffer-file-coding-system 'iso-2022-7bit-short
)
416 (switch-to-buffer buf
)))
418 (defun batch-skkdic-convert ()
419 "Run `skkdic-convert' on the files remaining on the command line.
420 Use this from the command line, with `-batch';
421 it won't work in an interactive Emacs.
423 % emacs -batch -l ja-dic-cnv -f batch-skkdic-convert SKK-JISYO.L
424 to generate \"ja-dic.el\" from SKK dictionary file \"SKK-JISYO.L\".
425 To get complete usage, invoke:
426 % emacs -batch -l ja-dic-cnv -f batch-skkdic-convert -h"
427 (defvar command-line-args-left
) ; Avoid compiler warning.
428 (if (not noninteractive
)
429 (error "`batch-skkdic-convert' should be used only with -batch"))
430 (if (string= (car command-line-args-left
) "-h")
432 (message "To convert SKK-JISYO.L into skkdic.el:")
433 (message " %% emacs -batch -l ja-dic-cnv -f batch-skkdic-convert SKK-JISYO.L")
434 (message "To convert SKK-JISYO.L into DIR/ja-dic.el:")
435 (message " %% emacs -batch -l ja-dic-cnv -f batch-skkdic-convert -dir DIR SKK-JISYO.L"))
436 (let (targetdir filename
)
437 (if (string= (car command-line-args-left
) "-dir")
439 (setq command-line-args-left
(cdr command-line-args-left
))
440 (setq targetdir
(expand-file-name (car command-line-args-left
)))
441 (setq command-line-args-left
(cdr command-line-args-left
))))
442 (setq filename
(expand-file-name (car command-line-args-left
)))
443 (message "Converting %s to %s ..." filename ja-dic-filename
)
444 (message "It takes around 10 minutes even on Sun SS20.")
445 (skkdic-convert filename targetdir
)
446 (message "Do byte-compile the created file by:")
447 (message " %% emacs -batch -f batch-byte-compile %s" ja-dic-filename
)
452 ;; The following macros are expanded at byte-compiling time so that
453 ;; compiled code can be loaded quickly.
455 (defun skkdic-get-kana-compact-codes (kana)
456 (let* ((len (length kana
))
457 (vec (make-vector len
0))
461 (setq ch
(aref kana i
))
463 (if (< ch
128) ; CH is an ASCII letter for OKURIGANA,
464 (- ch
) ; represented by a negative code.
465 (if (= ch ?
\e$B
!<\e(B) ; `\e$B!<\e(B' is represented by 0.
467 (- (logand (encode-char ch
'japanese-jisx0208
) #xFF
) 32))))
471 (defun skkdic-extract-conversion-data (entry)
472 (string-match "^\\cj+[a-z]* " entry
)
473 (let ((kana (substring entry
(match-beginning 0) (1- (match-end 0))))
476 (while (string-match "[^ ]+" entry i
)
477 (setq candidates
(cons (match-string 0 entry
) candidates
))
478 (setq i
(match-end 0)))
479 (cons (skkdic-get-kana-compact-codes kana
) candidates
)))
481 (defmacro skkdic-set-okuri-ari
(&rest entries
)
482 `(defconst skkdic-okuri-ari
484 (map '(skkdic-okuri-ari))
487 (setq entry
(skkdic-extract-conversion-data (car l
)))
488 (set-nested-alist (car entry
) (cdr entry
) map
)
492 (defmacro skkdic-set-postfix
(&rest entries
)
493 `(defconst skkdic-postfix
499 (setq entry
(skkdic-extract-conversion-data (car l
)))
500 (setq len
(length (car entry
)))
503 (let ((entry2 (lookup-nested-alist (car entry
) map nil nil t
)))
504 (if (consp (car entry2
))
505 (let ((conversions (cdr entry
)))
507 (if (not (member (car conversions
) (car entry2
)))
508 (setcar entry2
(cons (car conversions
) (car entry2
))))
509 (setq conversions
(cdr conversions
))))
510 (set-nested-alist (car entry
) (cdr entry
) map
)))
515 (defmacro skkdic-set-prefix
(&rest entries
)
516 `(defconst skkdic-prefix
522 (setq entry
(skkdic-extract-conversion-data (car l
)))
523 (setq len
(length (car entry
)))
526 (let ((entry2 (lookup-nested-alist (car entry
) map len nil t
)))
527 (if (consp (car entry2
))
528 (let ((conversions (cdr entry
)))
530 (if (not (member (car conversions
) (car entry2
)))
531 (setcar entry2
(cons (car conversions
) (car entry2
))))
532 (setq conversions
(cdr conversions
))))
533 (set-nested-alist (car entry
) (cdr entry
) map len
)))
538 (defmacro skkdic-set-okuri-nasi
(&rest entries
)
539 `(defconst skkdic-okuri-nasi
541 (map '(skdic-okuri-nasi))
545 (setq count
(1+ count
))
546 (if (= (% count
10000) 0)
547 (message "%d entries" count
))
548 (setq entry
(skkdic-extract-conversion-data (car l
)))
549 (set-nested-alist (car entry
) (cdr entry
) map
)
553 (provide 'ja-dic-cnv
)
556 ;; coding: iso-2022-7bit
559 ;; arch-tag: dec06fb0-8118-45b1-80d7-dc360b6fd3b2
560 ;;; ja-dic-cnv.el ends here