1 ;;; ids-find.el --- search utility based on Ideographic-structures
3 ;; Copyright (C) 2002,2003,2005,2006 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 CHISE IDS.
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))
93 (dolist (feature (to-component-features))
94 (if (setq ret (get-char-attribute char feature))
96 (setq dest (union dest (char-component-variants c))))))
98 ;; ((setq ret (some (lambda (feature)
99 ;; (get-char-attribute char feature))
100 ;; (to-component-features)))
102 ;; (setq dest (union dest (char-component-variants c))))
104 ((setq ret (get-char-attribute char '->ucs-unified))
105 (setq dest (cons char ret))
107 (setq dest (union dest
108 (some (lambda (feature)
109 (get-char-attribute char feature))
110 (of-component-features))
113 ((and (setq ret (get-char-attribute char '=>ucs))
114 (setq uchr (decode-char '=ucs ret)))
115 (setq dest (cons uchr (char-variants uchr)))
117 (setq dest (union dest
118 (some (lambda (feature)
119 (get-char-attribute char feature))
120 (of-component-features))
126 (unless (memq c dest)
127 (setq dest (cons c dest)))
130 (some (lambda (feature)
131 (char-feature c feature))
132 (of-component-features))
140 (defun ideographic-products-find (&rest components)
141 (if (stringp (car components))
142 (setq components (string-to-char-list (car components))))
144 (dolist (variant (char-component-variants (car components)))
147 (get-char-attribute variant 'ideographic-products))))
150 (setq components (cdr components)))
152 (dolist (variant (char-component-variants (car components)))
155 (get-char-attribute variant 'ideographic-products))))
156 (setq dest (intersection dest products)))
158 ;; (defun ideographic-products-find (&rest components)
159 ;; (if (stringp (car components))
160 ;; (setq components (car components)))
161 ;; (let ((len (length components))
164 ;; (dolist (variant (char-component-variants (elt components 0)))
167 ;; (get-char-attribute variant 'ideographic-products))))
168 ;; (setq dest products)
172 ;; (setq products nil)
173 ;; (dolist (variant (char-component-variants (elt components i)))
174 ;; (dolist (product (get-char-attribute
175 ;; variant 'ideographic-products))
176 ;; (unless (memq product products)
177 ;; (when (memq product dest)
178 ;; (setq products (cons product products))))))
179 ;; (setq dest products)))
184 (defun ideographic-structure-char= (c1 c2)
187 (let ((m1 (char-ucs c1))
191 (some (lambda (feature)
193 (unless (characterp b2)
194 (setq b2 (find-char b2)))
196 (ideographic-structure-char= c1 b2)))
197 (char-feature c2 feature)
198 ;; (get-char-attribute
199 ;; c2 '<-ideographic-component-forms)
201 (of-component-features))
203 (setq m1 (car (get-char-attribute c1 '<-radical))
204 m2 (car (get-char-attribute c2 '<-radical)))
205 (unless (characterp m1)
206 (setq m1 (find-char m1)))
207 (unless (characterp m2)
208 (setq m2 (find-char m2)))
210 (ideographic-structure-char= m1 m2))))))))
212 (defun ideographic-structure-member-compare-components (component s-component)
214 (cond ((char-ref= component s-component #'ideographic-structure-char=))
216 (if (setq ret (assq 'ideographic-structure s-component))
217 (ideographic-structure-member component (cdr ret))))
218 ((setq ret (get-char-attribute s-component 'ideographic-structure))
219 (ideographic-structure-member component ret)))))
222 (defun ideographic-structure-member (component structure)
223 "Return non-nil if COMPONENT is included in STRUCTURE."
224 (or (memq component structure)
226 (setq structure (cdr structure))
227 (ideographic-structure-member-compare-components
228 component (car structure)))
230 (setq structure (cdr structure))
231 (ideographic-structure-member-compare-components
232 component (car structure)))
234 (setq structure (cdr structure))
236 (ideographic-structure-member-compare-components
237 component (car structure))))))
241 (defun ideographic-structure-repertoire-p (structure components)
242 "Return non-nil if STRUCTURE can be constructed by a subset of COMPONENTS."
244 (let (ret s-component)
246 (while (setq structure (cdr structure))
247 (setq s-component (car structure))
248 (unless (characterp s-component)
249 (if (setq ret (find-char s-component))
250 (setq s-component ret)))
253 (if (setq ret (assq 'ideographic-structure s-component))
254 (ideographic-structure-repertoire-p
255 (cdr ret) components)))
256 ((member* s-component components
257 :test #'ideographic-structure-char=))
259 (get-char-attribute s-component
260 'ideographic-structure))
261 (ideographic-structure-repertoire-p ret components)))
266 (defvar ids-find-result-buffer "*ids-chars*")
268 (defun ids-find-format-line (c v)
269 (format "%c\t%s\t%s\n"
271 (or (let ((ucs (or (char-ucs c)
272 (encode-char c 'ucs))))
274 (cond ((<= ucs #xFFFF)
275 (format " U+%04X" ucs))
277 (format "U-%08X" ucs)))))
279 (or (ideographic-structure-to-ids v)
282 (defun ids-insert-chars-including-components (components
283 &optional level ignored-chars)
286 (let (is dis i as bs)
287 (dolist (c (sort (copy-tree (ideographic-products-find components))
289 (if (setq as (char-total-strokes a))
290 (if (setq bs (char-total-strokes b))
292 (ideograph-char< a b)
295 (ideograph-char< a b)))))
296 (unless (memq c ignored-chars)
297 (setq is (char-feature c 'ideographic-structure))
302 (insert (ids-find-format-line c is))
304 (ids-insert-chars-including-components
305 (char-to-string c) (1+ level)
306 (cons c ignored-chars))))
309 ;; (defun ids-insert-chars-including-components (components level)
311 ;; (dolist (c (ideographic-products-find components))
312 ;; (setq is (char-feature c 'ideographic-structure))
314 ;; (while (< i level)
317 ;; (insert (ids-find-format-line c is))
318 ;; ;;(forward-line -1)
319 ;; (ids-insert-chars-including-components
320 ;; (char-to-string c) (1+ level))
324 (defun ids-find-chars-including-components (components)
325 "Search Ideographs whose structures have COMPONENTS."
326 (interactive "sComponents : ")
327 (with-current-buffer (get-buffer-create ids-find-result-buffer)
328 (setq buffer-read-only nil)
330 (ids-insert-chars-including-components components 0)
332 ;; (dolist (c (ideographic-products-find components))
333 ;; (setq is (char-feature c 'ideographic-structure))
334 ;; ;; to avoid problems caused by wrong indexes
335 ;; ;; (when (every (lambda (cc)
336 ;; ;; (ideographic-structure-member cc is))
338 ;; (dolist (dc (ideographic-products-find (char-to-string c)))
339 ;; (setq dis (char-feature dc 'ideographic-structure))
340 ;; ;; ;; to avoid problems caused by wrong indexes
341 ;; ;; (when (every (lambda (dcc)
342 ;; ;; (ideographic-structure-member dcc is))
345 ;; (insert (ids-find-format-line dc dis))
349 ;; (insert (ids-find-format-line c is))
354 (goto-char (point-min)))
355 (view-buffer ids-find-result-buffer))
356 ;; (defun ids-find-chars-including-components (components)
357 ;; "Search Ideographs whose structures have COMPONENTS."
358 ;; (interactive "sComponents : ")
359 ;; (with-current-buffer (get-buffer-create ids-find-result-buffer)
360 ;; (setq buffer-read-only nil)
362 ;; (map-char-attribute
364 ;; (when (every (lambda (p)
365 ;; (ideographic-structure-member p v))
367 ;; (insert (ids-find-format-line c v)))
369 ;; 'ideographic-structure)
370 ;; (goto-char (point-min)))
371 ;; (view-buffer ids-find-result-buffer))
374 (define-obsolete-function-alias 'ideographic-structure-search-chars
375 'ids-find-chars-including-components)
378 (defun ids-find-chars-covered-by-components (components)
379 "Search Ideographs which structures are consisted by subsets of COMPONENTS."
380 (interactive "sComponents: ")
381 (if (stringp components)
382 (setq components (string-to-char-list components)))
383 (with-current-buffer (get-buffer-create ids-find-result-buffer)
384 (setq buffer-read-only nil)
389 (when (ideographic-structure-repertoire-p v components)
390 (insert (ids-find-format-line c v))))
391 'ideographic-structure))
392 (goto-char (point-min)))
393 (view-buffer ids-find-result-buffer))
401 ;;; ids-find.el ends here