1 ;;; ids-find.el --- search utility based on Ideographic-structures
3 ;; Copyright (C) 2002,2003,2005,2006,2007,2017 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 c 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 c 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)))
159 (defun ideograph-find-products-with-variants (components &optional ignored-chars)
160 (if (stringp components)
161 (setq components (string-to-char-list components)))
163 (dolist (variant (char-component-variants (car components)))
167 (get-char-attribute variant 'ideographic-products)
171 (setq components (cdr components)))
173 (dolist (variant (char-component-variants (car components)))
177 (get-char-attribute variant 'ideographic-products)
179 (setq dest (intersection dest products)))
182 (defun ideograph-find-products (components &optional ignored-chars)
183 (if (stringp components)
184 (setq components (string-to-char-list components)))
186 ;; (dolist (variant (char-component-variants (car components)))
189 ;; (get-char-attribute variant 'ideographic-products))))
190 ;; (setq dest products)
191 (setq dest (get-char-attribute (car components) 'ideographic-products))
193 (setq components (cdr components)))
194 ;; (setq products nil)
195 ;; (dolist (variant (char-component-variants (car components)))
198 ;; (get-char-attribute variant 'ideographic-products))))
199 (setq products (get-char-attribute (car components) 'ideographic-products))
200 (setq dest (intersection dest products)))
204 (defun ideographic-structure-char= (c1 c2)
207 (let ((m1 (char-ucs c1))
211 (memq c1 (char-component-variants c2)))))))
213 (defun ideographic-structure-member-compare-components (component s-component)
215 (cond ((char-ref= component s-component #'ideographic-structure-char=))
217 (if (setq ret (assq 'ideographic-structure s-component))
218 (ideographic-structure-member component (cdr ret))))
219 ((setq ret (get-char-attribute s-component 'ideographic-structure))
220 (ideographic-structure-member component ret)))))
223 (defun ideographic-structure-member (component structure)
224 "Return non-nil if COMPONENT is included in STRUCTURE."
225 (or (memq component structure)
227 (setq structure (cdr structure))
228 (ideographic-structure-member-compare-components
229 component (car structure)))
231 (setq structure (cdr structure))
232 (ideographic-structure-member-compare-components
233 component (car structure)))
235 (setq structure (cdr structure))
237 (ideographic-structure-member-compare-components
238 component (car structure))))))
242 (defun ideographic-structure-repertoire-p (structure components)
243 "Return non-nil if STRUCTURE can be constructed by a subset of COMPONENTS."
245 (let (ret s-component)
247 (while (setq structure (cdr structure))
248 (setq s-component (car structure))
249 (unless (characterp s-component)
250 (if (setq ret (find-char s-component))
251 (setq s-component ret)))
254 (if (setq ret (assq 'ideographic-structure s-component))
255 (ideographic-structure-repertoire-p
256 (cdr ret) components)))
257 ((member* s-component components
258 :test #'ideographic-structure-char=))
260 (get-char-attribute s-component
261 'ideographic-structure))
262 (ideographic-structure-repertoire-p ret components)))
267 (defvar ids-find-result-buffer "*ids-chars*")
269 (defun ids-find-format-line (c v)
270 (format "%c\t%s\t%s\n"
272 (or (let ((ucs (or (char-ucs c)
273 (encode-char c 'ucs))))
275 (cond ((<= ucs #xFFFF)
276 (format " U+%04X" ucs))
278 (format "U-%08X" ucs)))))
280 (or (ideographic-structure-to-ids v)
283 (defun ids-insert-chars-including-components* (components
284 &optional level ignored-chars)
288 (dolist (c (sort (copy-tree (ideograph-find-products components
291 (if (setq as (char-total-strokes a))
292 (if (setq bs (char-total-strokes b))
294 (ideograph-char< a b)
297 (ideograph-char< a b)))))
298 (unless (memq c ignored-chars)
299 (setq is (char-feature c 'ideographic-structure))
304 (insert (ids-find-format-line c is))
306 (ids-insert-chars-including-components*
307 (char-to-string c) (1+ level)
308 (cons c ignored-chars))))
313 (defun ids-insert-chars-including-components (components
314 &optional level ignored-chars)
319 (ids-insert-chars-including-components* components
320 level ignored-chars)))
322 (dolist (c ignored-chars)
323 (dolist (vc (char-component-variants c))
324 (unless (memq vc ignored-chars)
325 (when (setq is (get-char-attribute vc 'ideographic-structure))
330 (insert (ids-find-format-line vc is))
332 (ids-insert-chars-including-components*
333 (char-to-string vc) (1+ level)
334 (cons vc ignored-chars)))))))
335 (dolist (c (sort (copy-tree (ideograph-find-products-with-variants
336 components ignored-chars))
338 (if (setq as (char-total-strokes a))
339 (if (setq bs (char-total-strokes b))
341 (ideograph-char< a b)
344 (ideograph-char< a b)))))
345 (unless (memq c ignored-chars)
346 (setq is (get-char-attribute c 'ideographic-structure))
351 (insert (ids-find-format-line c is))
353 (ids-insert-chars-including-components*
354 (char-to-string c) (1+ level)
355 (cons c ignored-chars))))
361 (defun ids-find-chars-including-components (components)
362 "Search Ideographs whose structures have COMPONENTS."
363 (interactive "sComponents : ")
364 (with-current-buffer (get-buffer-create ids-find-result-buffer)
365 (setq buffer-read-only nil)
367 (ids-insert-chars-including-components components 0 nil)
368 ;; (let ((ignored-chars
370 ;; (ids-insert-chars-including-components components 0 nil
371 ;; #'ideograph-find-products)))
373 ;; (setq rest ignored-chars)
374 ;; ;; (dolist (c rest)
375 ;; ;; (setq ignored-chars
376 ;; ;; (union ignored-chars
377 ;; ;; (ids-insert-chars-including-components
378 ;; ;; (list c) 0 ignored-chars
379 ;; ;; #'ideograph-find-products-with-variants))))
380 ;; (ids-insert-chars-including-components components 0 ignored-chars
381 ;; #'ideograph-find-products-with-variants))
382 (goto-char (point-min)))
383 (view-buffer ids-find-result-buffer))
386 (define-obsolete-function-alias 'ideographic-structure-search-chars
387 'ids-find-chars-including-components)
390 (defun ids-find-chars-covered-by-components (components)
391 "Search Ideographs which structures are consisted by subsets of COMPONENTS."
392 (interactive "sComponents: ")
393 (if (stringp components)
394 (setq components (string-to-char-list components)))
395 (with-current-buffer (get-buffer-create ids-find-result-buffer)
396 (setq buffer-read-only nil)
401 (when (ideographic-structure-repertoire-p v components)
402 (insert (ids-find-format-line c v))))
403 'ideographic-structure))
404 (goto-char (point-min)))
405 (view-buffer ids-find-result-buffer))
413 ;;; ids-find.el ends here