1 ;;; ids-find.el --- search utility based on Ideographic-structures
3 ;; Copyright (C) 2002,2003,2005 MORIOKA Tomohiko
5 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
6 ;; Keywords: Kanji, Ideographs, search, IDS, CHISE, UCS, Unicode
8 ;; This file is a part of Tomoyo-Tools.
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 (defun ids-index-store-char (product component)
28 (let ((ret (char-feature
29 component 'ideographic-products)))
30 (unless (memq product ret)
31 (put-char-attribute component 'ideographic-products
33 (when ret (setq ret (char-feature
34 component 'ideographic-structure))
35 (ids-index-store-structure product ret))
38 (defun ids-index-store-structure (product structure)
40 (dolist (cell (cdr structure))
42 (setq cell (plist-get cell :char)))
43 (cond ((characterp cell)
44 (ids-index-store-char product cell))
45 ((setq ret (assq 'ideographic-structure cell))
46 (ids-index-store-structure product (cdr ret)))
47 ;; ((setq ret (find-char cell))
48 ;; (ids-index-store-char product ret))
52 (defun ids-update-index ()
56 (ids-index-store-structure c v)
58 'ideographic-structure)
59 (save-char-attribute-table 'ideographic-products))
62 (mount-char-attribute-table 'ideographic-products)
65 (defun ids-find-all-products (char)
67 (dolist (cell (char-feature char 'ideographic-products))
68 (unless (memq cell dest)
69 (setq dest (cons cell dest)))
70 (setq dest (union dest (ids-find-all-products cell))))
73 (defun of-component-features ()
75 (dolist (feature (char-attribute-list))
76 (when (string-match "^<-.*[@/]component\\(/[^*/]+\\)*$"
77 (symbol-name feature))
79 (cons '<-ideographic-component-forms
82 (defun to-component-features ()
84 (dolist (feature (char-attribute-list))
85 (when (string-match "^->.*[@/]component\\(/[^*/]+\\)*$"
86 (symbol-name feature))
88 (cons '->ideographic-component-forms
92 (defun char-component-variants (char)
93 (let ((dest (list char))
96 ((setq ret (some (lambda (feature)
97 (get-char-attribute char feature))
98 (to-component-features)))
100 (setq dest (union dest (char-component-variants c))))
102 ((setq ret (get-char-attribute char '->ucs-unified))
103 (setq dest (cons char ret))
105 (setq dest (union dest
106 (some (lambda (feature)
107 (get-char-attribute char feature))
108 (of-component-features))
111 ((and (setq ret (get-char-attribute char '=>ucs))
112 (setq uchr (decode-char '=ucs ret)))
113 (setq dest (cons uchr (char-variants uchr)))
115 (setq dest (union dest
116 (some (lambda (feature)
117 (get-char-attribute char feature))
118 (of-component-features))
124 (unless (memq c dest)
125 (setq dest (cons c dest)))
128 (some (lambda (feature)
129 (char-feature c feature))
130 (of-component-features))
138 (defun ideographic-products-find (&rest components)
139 (if (stringp (car components))
140 (setq components (string-to-char-list (car components))))
142 (dolist (variant (char-component-variants (car components)))
145 (get-char-attribute variant 'ideographic-products))))
148 (setq components (cdr components)))
150 (dolist (variant (char-component-variants (car components)))
153 (get-char-attribute variant 'ideographic-products))))
154 (setq dest (intersection dest products)))
156 ;; (defun ideographic-products-find (&rest components)
157 ;; (if (stringp (car components))
158 ;; (setq components (car components)))
159 ;; (let ((len (length components))
162 ;; (dolist (variant (char-component-variants (elt components 0)))
165 ;; (get-char-attribute variant 'ideographic-products))))
166 ;; (setq dest products)
170 ;; (setq products nil)
171 ;; (dolist (variant (char-component-variants (elt components i)))
172 ;; (dolist (product (get-char-attribute
173 ;; variant 'ideographic-products))
174 ;; (unless (memq product products)
175 ;; (when (memq product dest)
176 ;; (setq products (cons product products))))))
177 ;; (setq dest products)))
182 (defun ideographic-structure-char= (c1 c2)
185 (let ((m1 (char-ucs c1))
189 (some (lambda (feature)
191 (unless (characterp b2)
192 (setq b2 (find-char b2)))
194 (ideographic-structure-char= c1 b2)))
195 (char-feature c2 feature)
196 ;; (get-char-attribute
197 ;; c2 '<-ideographic-component-forms)
199 (of-component-features))
201 (setq m1 (car (get-char-attribute c1 '<-radical))
202 m2 (car (get-char-attribute c2 '<-radical)))
203 (unless (characterp m1)
204 (setq m1 (find-char m1)))
205 (unless (characterp m2)
206 (setq m2 (find-char m2)))
208 (ideographic-structure-char= m1 m2))))))))
210 (defun ideographic-structure-member-compare-components (component s-component)
212 (cond ((char-ref= component s-component #'ideographic-structure-char=))
214 (if (setq ret (assq 'ideographic-structure s-component))
215 (ideographic-structure-member component (cdr ret))))
216 ((setq ret (get-char-attribute s-component 'ideographic-structure))
217 (ideographic-structure-member component ret)))))
220 (defun ideographic-structure-member (component structure)
221 "Return non-nil if COMPONENT is included in STRUCTURE."
222 (or (memq component structure)
224 (setq structure (cdr structure))
225 (ideographic-structure-member-compare-components
226 component (car structure)))
228 (setq structure (cdr structure))
229 (ideographic-structure-member-compare-components
230 component (car structure)))
232 (setq structure (cdr structure))
234 (ideographic-structure-member-compare-components
235 component (car structure))))))
239 (defun ideographic-structure-repertoire-p (structure components)
240 "Return non-nil if STRUCTURE can be constructed by a subset of COMPONENTS."
242 (let (ret s-component)
244 (while (setq structure (cdr structure))
245 (setq s-component (car structure))
246 (unless (characterp s-component)
247 (if (setq ret (find-char s-component))
248 (setq s-component ret)))
251 (if (setq ret (assq 'ideographic-structure s-component))
252 (ideographic-structure-repertoire-p
253 (cdr ret) components)))
254 ((member* s-component components
255 :test #'ideographic-structure-char=))
257 (get-char-attribute s-component
258 'ideographic-structure))
259 (ideographic-structure-repertoire-p ret components)))
264 (defvar ids-find-result-buffer "*ids-chars*")
266 (defun ids-find-format-line (c v)
267 (format "%c\t%s\t%s\n"
269 (or (let ((ucs (or (char-ucs c)
270 (encode-char c 'ucs))))
272 (cond ((<= ucs #xFFFF)
273 (format " U+%04X" ucs))
275 (format "U-%08X" ucs)))))
277 (or (ideographic-structure-to-ids v)
280 (defun ids-insert-chars-including-components (components
281 &optional level ignored-chars)
284 (let (is dis i as bs)
285 (dolist (c (sort (ideographic-products-find components)
287 (if (setq as (char-total-strokes a))
288 (if (setq bs (char-total-strokes b))
290 (ideograph-char< a b)
293 (ideograph-char< a b)))))
294 (unless (memq c ignored-chars)
295 (setq is (char-feature c 'ideographic-structure))
300 (insert (ids-find-format-line c is))
302 (ids-insert-chars-including-components
303 (char-to-string c) (1+ level)
304 (cons c ignored-chars))))
307 ;; (defun ids-insert-chars-including-components (components level)
309 ;; (dolist (c (ideographic-products-find components))
310 ;; (setq is (char-feature c 'ideographic-structure))
312 ;; (while (< i level)
315 ;; (insert (ids-find-format-line c is))
316 ;; ;;(forward-line -1)
317 ;; (ids-insert-chars-including-components
318 ;; (char-to-string c) (1+ level))
322 (defun ids-find-chars-including-components (components)
323 "Search Ideographs whose structures have COMPONENTS."
324 (interactive "sComponents : ")
325 (with-current-buffer (get-buffer-create ids-find-result-buffer)
326 (setq buffer-read-only nil)
328 (ids-insert-chars-including-components components 0)
330 ;; (dolist (c (ideographic-products-find components))
331 ;; (setq is (char-feature c 'ideographic-structure))
332 ;; ;; to avoid problems caused by wrong indexes
333 ;; ;; (when (every (lambda (cc)
334 ;; ;; (ideographic-structure-member cc is))
336 ;; (dolist (dc (ideographic-products-find (char-to-string c)))
337 ;; (setq dis (char-feature dc 'ideographic-structure))
338 ;; ;; ;; to avoid problems caused by wrong indexes
339 ;; ;; (when (every (lambda (dcc)
340 ;; ;; (ideographic-structure-member dcc is))
343 ;; (insert (ids-find-format-line dc dis))
347 ;; (insert (ids-find-format-line c is))
352 (goto-char (point-min)))
353 (view-buffer ids-find-result-buffer))
354 ;; (defun ids-find-chars-including-components (components)
355 ;; "Search Ideographs whose structures have COMPONENTS."
356 ;; (interactive "sComponents : ")
357 ;; (with-current-buffer (get-buffer-create ids-find-result-buffer)
358 ;; (setq buffer-read-only nil)
360 ;; (map-char-attribute
362 ;; (when (every (lambda (p)
363 ;; (ideographic-structure-member p v))
365 ;; (insert (ids-find-format-line c v)))
367 ;; 'ideographic-structure)
368 ;; (goto-char (point-min)))
369 ;; (view-buffer ids-find-result-buffer))
372 (define-obsolete-function-alias 'ideographic-structure-search-chars
373 'ids-find-chars-including-components)
376 (defun ids-find-chars-covered-by-components (components)
377 "Search Ideographs which structures are consisted by subsets of COMPONENTS."
378 (interactive "sComponents: ")
379 (if (stringp components)
380 (setq components (string-to-char-list components)))
381 (with-current-buffer (get-buffer-create ids-find-result-buffer)
382 (setq buffer-read-only nil)
387 (when (ideographic-structure-repertoire-p v components)
388 (insert (ids-find-format-line c v))))
389 'ideographic-structure))
390 (goto-char (point-min)))
391 (view-buffer ids-find-result-buffer))
399 ;;; ids-find.el ends here