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 (get-char-attribute component 'ideographic-products)))
29 (unless (memq product ret)
30 (put-char-attribute component 'ideographic-products
32 (when (setq ret (char-feature component 'ideographic-structure))
33 (ids-index-store-structure product ret)))
36 (defun ids-index-store-structure (product structure)
38 (dolist (cell (cdr structure))
40 (setq cell (plist-get cell :char)))
41 (cond ((characterp cell)
42 (ids-index-store-char product cell))
43 ((setq ret (assq 'ideographic-structure cell))
44 (ids-index-store-structure product (cdr ret)))
45 ;; ((setq ret (find-char cell))
46 ;; (ids-index-store-char product ret))
50 (defun ids-update-index ()
54 (ids-index-store-structure c v)
56 'ideographic-structure)
57 (save-char-attribute-table 'ideographic-products))
60 (mount-char-attribute-table 'ideographic-products)
63 (defun ids-find-all-products (char)
65 (dolist (cell (char-feature char 'ideographic-products))
66 (unless (memq cell dest)
67 (setq dest (cons cell dest)))
68 (setq dest (union dest (ids-find-all-products cell))))
71 (defun of-component-features ()
73 (dolist (feature (char-attribute-list))
74 (when (string-match "^<-.*[@/]component\\(/[^*/]+\\)*$"
75 (symbol-name feature))
77 (cons '<-ideographic-component-forms
80 (defun to-component-features ()
82 (dolist (feature (char-attribute-list))
83 (when (string-match "^->.*[@/]component\\(/[^*/]+\\)*$"
84 (symbol-name feature))
86 (cons '->ideographic-component-forms
90 (defun char-component-variants (char)
91 (let ((dest (list char))
94 ((setq ret (some (lambda (feature)
95 (get-char-attribute char feature))
96 (to-component-features)))
98 (setq dest (union dest (char-component-variants c))))
100 ((setq ret (get-char-attribute char '->ucs-unified))
101 (setq dest (cons char ret))
103 (setq dest (union dest
104 (some (lambda (feature)
105 (get-char-attribute char feature))
106 (of-component-features))
109 ((and (setq ret (get-char-attribute char '=>ucs))
110 (setq uchr (decode-char '=ucs ret)))
111 (setq dest (cons uchr (char-variants uchr)))
113 (setq dest (union dest
114 (some (lambda (feature)
115 (get-char-attribute char feature))
116 (of-component-features))
122 (unless (memq c dest)
123 (setq dest (cons c dest)))
126 (some (lambda (feature)
127 (char-feature c feature))
128 (of-component-features))
136 (defun ideographic-products-find (&rest components)
137 (if (stringp (car components))
138 (setq components (string-to-char-list (car components))))
140 (dolist (variant (char-component-variants (car components)))
143 (get-char-attribute variant 'ideographic-products))))
146 (setq components (cdr components)))
148 (dolist (variant (char-component-variants (car components)))
151 (get-char-attribute variant 'ideographic-products))))
152 (setq dest (intersection dest products)))
154 ;; (defun ideographic-products-find (&rest components)
155 ;; (if (stringp (car components))
156 ;; (setq components (car components)))
157 ;; (let ((len (length components))
160 ;; (dolist (variant (char-component-variants (elt components 0)))
163 ;; (get-char-attribute variant 'ideographic-products))))
164 ;; (setq dest products)
168 ;; (setq products nil)
169 ;; (dolist (variant (char-component-variants (elt components i)))
170 ;; (dolist (product (get-char-attribute
171 ;; variant 'ideographic-products))
172 ;; (unless (memq product products)
173 ;; (when (memq product dest)
174 ;; (setq products (cons product products))))))
175 ;; (setq dest products)))
180 (defun ideographic-structure-char= (c1 c2)
183 (let ((m1 (char-ucs c1))
187 (some (lambda (feature)
189 (unless (characterp b2)
190 (setq b2 (find-char b2)))
192 (ideographic-structure-char= c1 b2)))
193 (char-feature c2 feature)
194 ;; (get-char-attribute
195 ;; c2 '<-ideographic-component-forms)
197 (of-component-features))
199 (setq m1 (car (get-char-attribute c1 '<-radical))
200 m2 (car (get-char-attribute c2 '<-radical)))
201 (unless (characterp m1)
202 (setq m1 (find-char m1)))
203 (unless (characterp m2)
204 (setq m2 (find-char m2)))
206 (ideographic-structure-char= m1 m2))))))))
208 (defun ideographic-structure-member-compare-components (component s-component)
210 (cond ((char-ref= component s-component #'ideographic-structure-char=))
212 (if (setq ret (assq 'ideographic-structure s-component))
213 (ideographic-structure-member component (cdr ret))))
214 ((setq ret (get-char-attribute s-component 'ideographic-structure))
215 (ideographic-structure-member component ret)))))
218 (defun ideographic-structure-member (component structure)
219 "Return non-nil if COMPONENT is included in STRUCTURE."
220 (or (memq component structure)
222 (setq structure (cdr structure))
223 (ideographic-structure-member-compare-components
224 component (car structure)))
226 (setq structure (cdr structure))
227 (ideographic-structure-member-compare-components
228 component (car structure)))
230 (setq structure (cdr structure))
232 (ideographic-structure-member-compare-components
233 component (car structure))))))
237 (defun ideographic-structure-repertoire-p (structure components)
238 "Return non-nil if STRUCTURE can be constructed by a subset of COMPONENTS."
240 (let (ret s-component)
242 (while (setq structure (cdr structure))
243 (setq s-component (car structure))
244 (unless (characterp s-component)
245 (if (setq ret (find-char s-component))
246 (setq s-component ret)))
249 (if (setq ret (assq 'ideographic-structure s-component))
250 (ideographic-structure-repertoire-p
251 (cdr ret) components)))
252 ((member* s-component components
253 :test #'ideographic-structure-char=))
255 (get-char-attribute s-component
256 'ideographic-structure))
257 (ideographic-structure-repertoire-p ret components)))
262 (defvar ids-find-result-buffer "*ids-chars*")
264 (defun ids-find-format-line (c v)
265 (format "%c\t%s\t%s\n"
267 (or (let ((ucs (or (char-ucs c)
268 (encode-char c 'ucs))))
270 (cond ((<= ucs #xFFFF)
271 (format " U+%04X" ucs))
273 (format "U-%08X" ucs)))))
275 (or (ideographic-structure-to-ids v)
278 (defun ids-insert-chars-including-components (components
279 &optional level ignored-chars)
282 (let (is dis i as bs)
283 (dolist (c (sort (ideographic-products-find components)
285 (if (setq as (char-total-strokes a))
286 (if (setq bs (char-total-strokes b))
288 (ideograph-char< a b)
291 (ideograph-char< a b)))))
292 (unless (memq c ignored-chars)
293 (setq is (char-feature c 'ideographic-structure))
298 (insert (ids-find-format-line c is))
300 (ids-insert-chars-including-components
301 (char-to-string c) (1+ level)
302 (cons c ignored-chars))))
305 ;; (defun ids-insert-chars-including-components (components level)
307 ;; (dolist (c (ideographic-products-find components))
308 ;; (setq is (char-feature c 'ideographic-structure))
310 ;; (while (< i level)
313 ;; (insert (ids-find-format-line c is))
314 ;; ;;(forward-line -1)
315 ;; (ids-insert-chars-including-components
316 ;; (char-to-string c) (1+ level))
320 (defun ids-find-chars-including-components (components)
321 "Search Ideographs whose structures have COMPONENTS."
322 (interactive "sComponents : ")
323 (with-current-buffer (get-buffer-create ids-find-result-buffer)
324 (setq buffer-read-only nil)
326 (ids-insert-chars-including-components components 0)
328 ;; (dolist (c (ideographic-products-find components))
329 ;; (setq is (char-feature c 'ideographic-structure))
330 ;; ;; to avoid problems caused by wrong indexes
331 ;; ;; (when (every (lambda (cc)
332 ;; ;; (ideographic-structure-member cc is))
334 ;; (dolist (dc (ideographic-products-find (char-to-string c)))
335 ;; (setq dis (char-feature dc 'ideographic-structure))
336 ;; ;; ;; to avoid problems caused by wrong indexes
337 ;; ;; (when (every (lambda (dcc)
338 ;; ;; (ideographic-structure-member dcc is))
341 ;; (insert (ids-find-format-line dc dis))
345 ;; (insert (ids-find-format-line c is))
350 (goto-char (point-min)))
351 (view-buffer ids-find-result-buffer))
352 ;; (defun ids-find-chars-including-components (components)
353 ;; "Search Ideographs whose structures have COMPONENTS."
354 ;; (interactive "sComponents : ")
355 ;; (with-current-buffer (get-buffer-create ids-find-result-buffer)
356 ;; (setq buffer-read-only nil)
358 ;; (map-char-attribute
360 ;; (when (every (lambda (p)
361 ;; (ideographic-structure-member p v))
363 ;; (insert (ids-find-format-line c v)))
365 ;; 'ideographic-structure)
366 ;; (goto-char (point-min)))
367 ;; (view-buffer ids-find-result-buffer))
370 (define-obsolete-function-alias 'ideographic-structure-search-chars
371 'ids-find-chars-including-components)
374 (defun ids-find-chars-covered-by-components (components)
375 "Search Ideographs which structures are consisted by subsets of COMPONENTS."
376 (interactive "sComponents: ")
377 (if (stringp components)
378 (setq components (string-to-char-list components)))
379 (with-current-buffer (get-buffer-create ids-find-result-buffer)
380 (setq buffer-read-only nil)
385 (when (ideographic-structure-repertoire-p v components)
386 (insert (ids-find-format-line c v))))
387 'ideographic-structure))
388 (goto-char (point-min)))
389 (view-buffer ids-find-result-buffer))
397 ;;; ids-find.el ends here