(whitespace): Set :version tag to 23.1.
[emacs.git] / lisp / international / ja-dic-utl.el
blob3fe1a8d7dd5bb48b209373ed35fcd6b8bfe3a4ea
1 ;;; ja-dic-utl.el --- utilities for handling Japanese dictionary (SKK-JISYO.L)
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008
5 ;; National Institute of Advanced Industrial Science and Technology (AIST)
6 ;; Registration Number H14PRO021
8 ;; Keywords: 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, or (at your option)
15 ;; 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; This file provides a generic function to look up a Japanese
30 ;; dictionary of SKK format.
32 ;; SKK is a free Japanese input method running on Mule created by
33 ;; Masahiko Sato <masahiko@sato.riec.tohoku.ac.jp>. The Emacs Lisp
34 ;; library kkc.el provides a facility to convert a Japanese kana
35 ;; string to a kanji-kana-mixed string by using SKK's dictionary.
37 ;; The original SKK dictionary SKK-JISYO.L is converted to ja-dic.el
38 ;; by ja-dic-cnv.el. We get entries of the dictionary in four
39 ;; variables (listed below) by loading that file (or byte-compiled
40 ;; version ja-dic.elc).
42 ;;; Code:
44 ;; The following four variables are set by loading ja-dic.el[c].
45 (defvar skkdic-okuri-ari nil
46 "Nested alist for OKURI-ARI entries of SKK dictionary.")
48 (defvar skkdic-postfix nil
49 "Nested alist for SETSUBIJI (postfix) entries of SKK dictionary.")
51 (defvar skkdic-prefix nil
52 "Nested alist SETTOUJI (prefix) entries of SKK dictionary.")
54 (defvar skkdic-okuri-nasi nil
55 "Nested alist for OKURI-NASI entries of SKK dictionary.")
57 (defconst skkdic-okurigana-table
58 '((?\e$B$!\e(B . ?a) (?\e$B$"\e(B . ?a) (?\e$B$#\e(B . ?i) (?\e$B$$\e(B . ?i) (?\e$B$%\e(B . ?u)
59 (?\e$B$&\e(B . ?u) (?\e$B$'\e(B . ?e) (?\e$B$(\e(B . ?e) (?\e$B$)\e(B . ?o) (?\e$B$*\e(B . ?o)
60 (?\e$B$+\e(B . ?k) (?\e$B$,\e(B . ?g) (?\e$B$-\e(B . ?k) (?\e$B$.\e(B . ?g) (?\e$B$/\e(B . ?k)
61 (?\e$B$0\e(B . ?g) (?\e$B$1\e(B . ?k) (?\e$B$2\e(B . ?g) (?\e$B$3\e(B . ?k) (?\e$B$4\e(B . ?g)
62 (?\e$B$5\e(B . ?s) (?\e$B$6\e(B . ?z) (?\e$B$7\e(B . ?s) (?\e$B$8\e(B . ?j) (?\e$B$9\e(B . ?s)
63 (?\e$B$:\e(B . ?z) (?\e$B$;\e(B . ?s) (?\e$B$<\e(B . ?z) (?\e$B$=\e(B . ?s) (?\e$B$>\e(B . ?z)
64 (?\e$B$?\e(B . ?t) (?\e$B$@\e(B . ?d) (?\e$B$A\e(B . ?t) (?\e$B$B\e(B . ?d) (?\e$B$C\e(B . ?t)
65 (?\e$B$D\e(B . ?t) (?\e$B$E\e(B . ?d) (?\e$B$F\e(B . ?t) (?\e$B$G\e(B . ?d) (?\e$B$H\e(B . ?t) (?\e$B$I\e(B . ?d)
66 (?\e$B$J\e(B . ?n) (?\e$B$K\e(B . ?n) (?\e$B$L\e(B . ?n) (?\e$B$M\e(B . ?n) (?\e$B$N\e(B . ?n)
67 (?\e$B$O\e(B . ?h) (?\e$B$P\e(B . ?b) (?\e$B$Q\e(B . ?p) (?\e$B$R\e(B . ?h) (?\e$B$S\e(B . ?b)
68 (?\e$B$T\e(B . ?p) (?\e$B$U\e(B . ?h) (?\e$B$V\e(B . ?b) (?\e$B$W\e(B . ?p) (?\e$B$X\e(B . ?h)
69 (?\e$B$Y\e(B . ?b) (?\e$B$Z\e(B . ?p) (?\e$B$[\e(B . ?h) (?\e$B$\\e(B . ?b) (?\e$B$]\e(B . ?p)
70 (?\e$B$^\e(B . ?m) (?\e$B$_\e(B . ?m) (?\e$B$`\e(B . ?m) (?\e$B$a\e(B . ?m) (?\e$B$b\e(B . ?m)
71 (?\e$B$c\e(B . ?y) (?\e$B$d\e(B . ?y) (?\e$B$e\e(B . ?y) (?\e$B$f\e(B . ?y) (?\e$B$g\e(B . ?y) (?\e$B$h\e(B . ?y)
72 (?\e$B$i\e(B . ?r) (?\e$B$j\e(B . ?r) (?\e$B$k\e(B . ?r) (?\e$B$l\e(B . ?r) (?\e$B$m\e(B . ?r)
73 (?\e$B$o\e(B . ?w) (?\e$B$p\e(B . ?w) (?\e$B$q\e(B . ?w) (?\e$B$r\e(B . ?w)
74 (?\e$B$s\e(B . ?n)
76 "Alist of Okuriganas vs trailing ASCII letters in OKURI-ARI entry.")
78 (defun skkdic-merge-head-and-tail (heads tails postfix)
79 (let ((min-len 2)
81 (while heads
82 (if (or (not postfix)
83 (>= (length (car heads)) min-len))
84 (let ((tail tails))
85 (while tail
86 (if (or postfix
87 (>= (length (car tail)) min-len))
88 (setq l (cons (concat (car heads) (car tail)) l)))
89 (setq tail (cdr tail)))))
90 (setq heads (cdr heads)))
91 l))
93 (defconst skkdic-jisx0208-hiragana-block (nth 1 (split-char ?\e$B$"\e(B)))
95 (defun skkdic-lookup-key (seq len &optional postfix prefer-noun)
96 "Return a list of conversion string for sequence SEQ of length LEN.
98 SEQ is a vector of Kana characters to be converted by SKK dictionary.
99 If LEN is shorter than the length of KEYSEQ, the first LEN keys in SEQ
100 are took into account.
102 Optional 3rd arg POSTFIX non-nil means SETSUBIJI (postfix) are also
103 considered to find conversion strings.
105 Optional 4th arg PREFER-NOUN non-nil means that the conversions
106 without okurigana are placed at the head of the returned list."
107 (or skkdic-okuri-nasi
108 (condition-case err
109 (load-library "ja-dic/ja-dic")
110 (error (ding)
111 (with-output-to-temp-buffer "*Help*"
112 (princ "The library `ja-dic' can't be loaded.
114 The most common case is that you have not yet installed the library
115 included in LEIM (Libraries of Emacs Input Method) which is
116 distributed separately from Emacs.
118 LEIM is available from the same ftp directory as Emacs."))
119 (signal (car err) (cdr err)))))
121 (let ((vec (make-vector len 0))
122 (i 0)
123 entry)
124 ;; At first, generate vector VEC from SEQ for looking up SKK
125 ;; alists. Nth element in VEC corresponds to Nth element in SEQ.
126 ;; The values are decided as follows.
127 ;; If SEQ[N] is `\e$B!<\e(B', VEC[N] is 0,
128 ;; else if SEQ[N] is a Hiragana character, VEC[N] is:
129 ;; ((The 2nd position code of SEQ[N]) - 32),
130 ;; else VEC[N] is 128.
131 (while (< i len)
132 (let ((ch (aref seq i))
133 elts)
134 (if (= ch ?\e$B!<\e(B)
135 (aset vec i 0)
136 (setq elts (split-char ch))
137 (if (and (eq (car elts) 'japanese-jisx0208)
138 (= (nth 1 elts) skkdic-jisx0208-hiragana-block))
139 (aset vec i (- (nth 2 elts) 32))
140 (aset vec i 128))))
141 (setq i (1+ i)))
143 ;; Search OKURI-NASI entries.
144 (setq entry (lookup-nested-alist vec skkdic-okuri-nasi len 0 t))
145 (if (consp (car entry))
146 (setq entry (copy-sequence (car entry)))
147 (setq entry nil))
149 (if postfix
150 ;; Search OKURI-NASI entries with postfixes.
151 (let ((break (max (- len (car skkdic-postfix)) 1))
152 entry-head entry-postfix entry2)
153 (while (< break len)
154 (if (and (setq entry-head
155 (lookup-nested-alist vec skkdic-okuri-nasi
156 break 0 t))
157 (consp (car entry-head))
158 (setq entry-postfix
159 (lookup-nested-alist vec skkdic-postfix
160 len break t))
161 (consp (car entry-postfix))
162 (setq entry2 (skkdic-merge-head-and-tail
163 (car entry-head) (car entry-postfix) t)))
164 (if entry
165 (nconc entry entry2)
166 (setq entry entry2)))
167 (setq break (1+ break)))))
169 ;; Search OKURI-NASI entries with prefixes.
170 (let ((break (min (car skkdic-prefix) (- len 2)))
171 entry-prefix entry-tail entry2)
172 (while (> break 0)
173 (if (and (setq entry-prefix
174 (lookup-nested-alist vec skkdic-prefix break 0 t))
175 (consp (car entry-prefix))
176 (setq entry-tail
177 (lookup-nested-alist vec skkdic-okuri-nasi len break t))
178 (consp (car entry-tail))
179 (setq entry2 (skkdic-merge-head-and-tail
180 (car entry-prefix) (car entry-tail) nil)))
181 (progn
182 (if entry
183 (nconc entry entry2)
184 (setq entry entry2))))
185 (setq break (1- break))))
187 ;; Search OKURI-ARI entries.
188 (let ((okurigana (assq (aref seq (1- len)) skkdic-okurigana-table))
189 orig-element entry2)
190 (if okurigana
191 (progn
192 (setq orig-element (aref vec (1- len)))
193 (aset vec (1- len) (- (cdr okurigana)))
194 (if (and (setq entry2 (lookup-nested-alist vec skkdic-okuri-ari
195 len 0 t))
196 (consp (car entry2)))
197 (progn
198 (setq entry2 (copy-sequence (car entry2)))
199 (let ((l entry2)
200 (okuri (char-to-string (aref seq (1- len)))))
201 (while l
202 (setcar l (concat (car l) okuri))
203 (setq l (cdr l)))
204 (if entry
205 (if prefer-noun
206 (nconc entry entry2)
207 (setq entry2 (nreverse entry2))
208 (nconc entry2 entry)
209 (setq entry entry2))
210 (setq entry (nreverse entry2))))))
211 (aset vec (1- len) orig-element))))
213 entry))
216 (provide 'ja-dic-utl)
218 ;; Local Variables:
219 ;; coding: iso-2022-7bit
220 ;; End:
222 ;;; arch-tag: df2218fa-469c-40f6-bace-7f89a053f9c0
223 ;;; ja-dic-utl.el ends here