(titdic-convert): Set
[bpt/emacs.git] / lisp / international / skkdic-utl.el
CommitLineData
3fdc9c8f 1;;; skkdic-utl.el --- Utility functions for handling SKK dictionary
4ed46869 2
4ed46869 3;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
fa526c4a 4;; Licensed to the Free Software Foundation.
4ed46869
KH
5
6;; Keywords: mule, multilingual, Japanese, SKK
7
8;; This file is part of GNU Emacs.
9
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)
13;; any later version.
14
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.
19
20;; You should have received a copy of the GNU General Public License
369314dc
KH
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.
4ed46869
KH
24
25;;; Commentary:
26
27;; SKK is a free Japanese input method running on Mule created by
c7c9a4a1
KH
28;; Masahiko Sato <masahiko@sato.riec.tohoku.ac.jp>. The Emacs Lisp
29;; library kkc.el provides a facility to convert a Japanese kana
30;; string to a kanji-kana-mixed string by using a SKK dictionary.
31;;
32;; This file provides a generic function to look up a SKK dictionary.
33;;
34;; The original SKK dictionary SKK-JISYO.L is converted to skkdic.el.
35;; We get entries of the dictionary in four variables (listed below)
36;; by loadig this file (or byte-compiled version skkdic.elc).
4ed46869
KH
37
38;;; Code:
39
c7c9a4a1 40;; The following four variables are set by loading skkdic.el[c].
4ed46869 41(defvar skkdic-okuri-ari nil
c7c9a4a1
KH
42 "Nested alist for OKURI-ARI entries of SKK dictionary.")
43
4ed46869 44(defvar skkdic-postfix nil
c7c9a4a1
KH
45 "Nested alist for SETSUBIJI (postfix) entries of SKK dictionary.")
46
4ed46869 47(defvar skkdic-prefix nil
c7c9a4a1
KH
48 "Nested alist SETTOUJI (prefix) entries of SKK dictionary.")
49
4ed46869 50(defvar skkdic-okuri-nasi nil
c7c9a4a1 51 "Nested alist for OKURI-NASI entries of SKK dictionary.")
4ed46869 52
4ed46869
KH
53(defconst skkdic-okurigana-table
54 '((?\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)
55 (?\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)
56 (?\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)
57 (?\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)
58 (?\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)
59 (?\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)
60 (?\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)
61 (?\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)
62 (?\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)
63 (?\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)
64 (?\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)
65 (?\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)
66 (?\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)
67 (?\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)
68 (?\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)
69 (?\e$B$o\e(B . ?w) (?\e$B$p\e(B . ?w) (?\e$B$q\e(B . ?w) (?\e$B$r\e(B . ?w)
70 (?\e$B$s\e(B . ?n)
c7c9a4a1
KH
71 )
72 "Alist of Okuriganas vs trailing ASCII letters in OKURI-ARI entry.")
4ed46869 73
4ed46869 74(defun skkdic-merge-head-and-tail (heads tails postfix)
2791eeaa 75 (let ((min-len 2)
4ed46869
KH
76 l)
77 (while heads
78 (if (or (not postfix)
79 (>= (length (car heads)) min-len))
80 (let ((tail tails))
81 (while tail
82 (if (or postfix
83 (>= (length (car tail)) min-len))
84 (setq l (cons (concat (car heads) (car tail)) l)))
85 (setq tail (cdr tail)))))
86 (setq heads (cdr heads)))
87 l))
88
89(defconst skkdic-jisx0208-hiragana-block (nth 1 (split-char ?\e$B$"\e(B)))
90
c7c9a4a1 91(defun skkdic-lookup-key (seq len &optional postfix prefer-noun)
4ed46869
KH
92 "Return a list of conversion string for sequence SEQ of length LEN.
93
94SEQ is a vector of Kana characters to be converted by SKK dictionary.
95If LEN is shorter than the length of KEYSEQ, the first LEN keys in SEQ
96are took into account.
97
c7c9a4a1
KH
98Optional 3rd arg POSTFIX non-nil means SETSUBIJI (postfix) are also
99considered to find conversion strings.
100
101Optional 4th arg PREFER-NOUN non-nil means that the conversions
102without okurigana are placed at the head of the returned list."
4ed46869
KH
103 (or skkdic-okuri-nasi
104 (condition-case err
105 (load-library "skk/skkdic")
106 (error (ding)
107 (with-output-to-temp-buffer "*Help*"
108 (princ "The library `skkdic' can't be loaded.
109
110The most common case is that you have not yet installed the library
111included in LEIM (Libraries of Emacs Input Method) which is
112distributed separately from Emacs.
113
114LEIM is available from the same ftp directory as Emacs."))
115 (signal (car err) (cdr err)))))
116
117 (let ((vec (make-vector len 0))
118 (i 0)
119 entry)
120 ;; At first, generate vector VEC from SEQ for looking up SKK
121 ;; alists. Nth element in VEC corresponds to Nth element in SEQ.
122 ;; The values are decided as follows.
123 ;; If SEQ[N] is `\e$B!<\e(B', VEC[N] is 0,
c7c9a4a1
KH
124 ;; else if SEQ[N] is a Hiragana character, VEC[N] is:
125 ;; ((The 2nd position code of SEQ[N]) - 32),
126 ;; else VEC[N] is 128.
4ed46869
KH
127 (while (< i len)
128 (let ((ch (aref seq i))
129 elts)
130 (if (= ch ?\e$B!<\e(B)
131 (aset vec i 0)
132 (setq elts (split-char ch))
133 (if (and (eq (car elts) 'japanese-jisx0208)
134 (= (nth 1 elts) skkdic-jisx0208-hiragana-block))
135 (aset vec i (- (nth 2 elts) 32))
136 (aset vec i 128))))
137 (setq i (1+ i)))
138
139 ;; Search OKURI-NASI entries.
140 (setq entry (lookup-nested-alist vec skkdic-okuri-nasi len 0 t))
141 (if (consp (car entry))
142 (setq entry (copy-sequence (car entry)))
143 (setq entry nil))
144
145 (if postfix
146 ;; Search OKURI-NASI entries with postfixes.
147 (let ((break (max (- len (car skkdic-postfix)) 1))
148 entry-head entry-postfix entry2)
149 (while (< break len)
150 (if (and (setq entry-head
151 (lookup-nested-alist vec skkdic-okuri-nasi
152 break 0 t))
153 (consp (car entry-head))
154 (setq entry-postfix
155 (lookup-nested-alist vec skkdic-postfix
156 len break t))
157 (consp (car entry-postfix))
158 (setq entry2 (skkdic-merge-head-and-tail
159 (car entry-head) (car entry-postfix) t)))
160 (if entry
161 (nconc entry entry2)
162 (setq entry entry2)))
163 (setq break (1+ break)))))
164
165 ;; Search OKURI-NASI entries with prefixes.
166 (let ((break (min (car skkdic-prefix) (- len 2)))
167 entry-prefix entry-tail entry2)
168 (while (> break 0)
169 (if (and (setq entry-prefix
170 (lookup-nested-alist vec skkdic-prefix break 0 t))
171 (consp (car entry-prefix))
172 (setq entry-tail
173 (lookup-nested-alist vec skkdic-okuri-nasi len break t))
174 (consp (car entry-tail))
175 (setq entry2 (skkdic-merge-head-and-tail
176 (car entry-prefix) (car entry-tail) nil)))
c7c9a4a1
KH
177 (progn
178 (if entry
179 (nconc entry entry2)
180 (setq entry entry2))))
4ed46869
KH
181 (setq break (1- break))))
182
183 ;; Search OKURI-ARI entries.
184 (let ((okurigana (assq (aref seq (1- len)) skkdic-okurigana-table))
185 orig-element entry2)
186 (if okurigana
187 (progn
188 (setq orig-element (aref vec (1- len)))
189 (aset vec (1- len) (- (cdr okurigana)))
190 (if (and (setq entry2 (lookup-nested-alist vec skkdic-okuri-ari
191 len 0 t))
192 (consp (car entry2)))
193 (progn
194 (setq entry2 (copy-sequence (car entry2)))
195 (let ((l entry2)
196 (okuri (char-to-string (aref seq (1- len)))))
197 (while l
198 (setcar l (concat (car l) okuri))
199 (setq l (cdr l)))
200 (if entry
c7c9a4a1
KH
201 (if prefer-noun
202 (nconc entry entry2)
203 (setq entry2 (nreverse entry2))
204 (nconc entry2 entry)
205 (setq entry entry2))
206 (setq entry (nreverse entry2))))))
4ed46869
KH
207 (aset vec (1- len) orig-element))))
208
209 entry))
210
211;;
212(provide 'skkdic-utl)
213
214;; skkdic-utl.el ends here