(char-component-variants): Fixed.
[chise/ids.git] / ids-find.el
1 ;;; ids-find.el --- search utility based on Ideographic-structures
2
3 ;; Copyright (C) 2002,2003,2005,2006 MORIOKA Tomohiko
4
5 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
6 ;; Keywords: Kanji, Ideographs, search, IDS, CHISE, UCS, Unicode
7
8 ;; This file is a part of CHISE IDS.
9
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.
14
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.
19
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.
24
25 ;;; Code:
26
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
31                           (cons product ret))
32       (when (setq ret (char-feature component 'ideographic-structure))
33         (ids-index-store-structure product ret)))
34     ))
35
36 (defun ids-index-store-structure (product structure)
37   (let (ret)
38     (dolist (cell (cdr structure))
39       (if (char-ref-p cell)
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))
47             ))))
48
49 ;;;###autoload
50 (defun ids-update-index ()
51   (interactive)
52   (map-char-attribute
53    (lambda (c v)
54      (ids-index-store-structure c v)
55      nil)
56    'ideographic-structure)
57   (save-char-attribute-table 'ideographic-products))
58
59
60 (mount-char-attribute-table 'ideographic-products)
61
62 ;;;###autoload
63 (defun ids-find-all-products (char)
64   (let (dest)
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))))
69     dest))
70
71 (defun of-component-features ()
72   (let (dest)
73     (dolist (feature (char-attribute-list))
74       (when (string-match "^<-.*[@/]component\\(/[^*/]+\\)*$"
75                           (symbol-name feature))
76         (push feature dest)))
77     (cons '<-ideographic-component-forms
78           dest)))
79
80 (defun to-component-features ()
81   (let (dest)
82     (dolist (feature (char-attribute-list))
83       (when (string-match "^->.*[@/]component\\(/[^*/]+\\)*$"
84                           (symbol-name feature))
85         (push feature dest)))
86     (cons '->ideographic-component-forms
87           dest)))
88
89 ;;;###autoload
90 (defun char-component-variants (char)
91   (let ((dest (list char))
92         ret uchr)
93     (dolist (feature (to-component-features))
94       (if (setq ret (get-char-attribute char feature))
95           (dolist (c ret)
96             (setq dest (union dest (char-component-variants c))))))
97     (cond
98      ;; ((setq ret (some (lambda (feature)
99      ;;                    (get-char-attribute char feature))
100      ;;                  (to-component-features)))
101      ;;  (dolist (c ret)
102      ;;    (setq dest (union dest (char-component-variants c))))
103      ;;  )
104      ((setq ret (get-char-attribute char '->ucs-unified))
105       (setq dest (cons char ret))
106       (dolist (c dest)
107         (setq dest (union dest
108                           (some (lambda (feature)
109                                   (get-char-attribute char feature))
110                                 (of-component-features))
111                           )))
112       )
113      ((and (setq ret (get-char-attribute char '=>ucs))
114            (setq uchr (decode-char '=ucs ret)))
115       (setq dest (cons uchr (char-variants uchr)))
116       (dolist (c dest)
117         (setq dest (union dest
118                           (some (lambda (feature)
119                                   (get-char-attribute char feature))
120                                 (of-component-features))
121                           )))
122       )
123      (t
124       (map-char-family
125        (lambda (c)
126          (unless (memq c dest)
127            (setq dest (cons c dest)))
128          (setq dest
129                (union dest
130                       (some (lambda (feature)
131                               (char-feature c feature))
132                             (of-component-features))
133                       ))
134          nil)
135        char)
136       ))
137     dest))
138
139 ;;;###autoload
140 (defun ideographic-products-find (&rest components)
141   (if (stringp (car components))
142       (setq components (string-to-char-list (car components))))
143   (let (dest products)
144     (dolist (variant (char-component-variants (car components)))
145       (setq products
146             (union products
147                    (get-char-attribute variant 'ideographic-products))))
148     (setq dest products)
149     (while (and dest
150                 (setq components (cdr components)))
151       (setq products nil)
152       (dolist (variant (char-component-variants (car components)))
153       (setq products
154             (union products
155                    (get-char-attribute variant 'ideographic-products))))
156       (setq dest (intersection dest products)))
157     dest))
158 ;; (defun ideographic-products-find (&rest components)
159 ;;   (if (stringp (car components))
160 ;;       (setq components (car components)))
161 ;;   (let ((len (length components))
162 ;;         (i 1)
163 ;;         dest products)
164 ;;     (dolist (variant (char-component-variants (elt components 0)))
165 ;;       (setq products
166 ;;             (union products
167 ;;                    (get-char-attribute variant 'ideographic-products))))
168 ;;     (setq dest products)
169 ;;     (while (and
170 ;;             (< i len)
171 ;;             (progn
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)))
180 ;;       (setq i (1+ i)))
181 ;;     products))
182
183
184 (defun ideographic-structure-char= (c1 c2)
185   (or (eq c1 c2)
186       (and c1 c2
187            (let ((m1 (char-ucs c1))
188                  (m2 (char-ucs c2)))
189              (or (and m1 m2
190                       (eq m1 m2))
191                  (some (lambda (feature)
192                          (some (lambda (b2)
193                                  (unless (characterp b2)
194                                    (setq b2 (find-char b2)))
195                                  (and b2
196                                       (ideographic-structure-char= c1 b2)))
197                                (char-feature c2 feature)
198                                ;; (get-char-attribute
199                                ;;  c2 '<-ideographic-component-forms)
200                                ))
201                        (of-component-features))
202                  (progn
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)))
209                    (when (or m1 m2)
210                      (ideographic-structure-char= m1 m2))))))))
211
212 (defun ideographic-structure-member-compare-components (component s-component)
213   (let (ret)
214     (cond ((char-ref= component s-component #'ideographic-structure-char=))
215           ((listp s-component)
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)))))
220
221 ;;;###autoload
222 (defun ideographic-structure-member (component structure)
223   "Return non-nil if COMPONENT is included in STRUCTURE."
224   (or (memq component structure)
225       (progn
226         (setq structure (cdr structure))
227         (ideographic-structure-member-compare-components
228          component (car structure)))
229       (progn
230         (setq structure (cdr structure))
231         (ideographic-structure-member-compare-components
232          component (car structure)))
233       (progn
234         (setq structure (cdr structure))
235         (and (car structure)
236              (ideographic-structure-member-compare-components
237               component (car structure))))))
238
239
240 ;;;###autoload
241 (defun ideographic-structure-repertoire-p (structure components)
242   "Return non-nil if STRUCTURE can be constructed by a subset of COMPONENTS."
243   (and structure
244        (let (ret s-component)
245          (catch 'tag
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)))
251              (unless (cond
252                       ((listp s-component)
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=))
258                       ((setq ret
259                              (get-char-attribute s-component
260                                                  'ideographic-structure))
261                        (ideographic-structure-repertoire-p ret components)))
262                (throw 'tag nil)))
263            t))))
264
265
266 (defvar ids-find-result-buffer "*ids-chars*")
267
268 (defun ids-find-format-line (c v)
269   (format "%c\t%s\t%s\n"
270           c
271           (or (let ((ucs (or (char-ucs c)
272                              (encode-char c 'ucs))))
273                 (if ucs
274                     (cond ((<= ucs #xFFFF)
275                            (format "    U+%04X" ucs))
276                           ((<= ucs #x10FFFF)
277                            (format "U-%08X" ucs)))))
278               "          ")
279           (or (ideographic-structure-to-ids v)
280               v)))
281
282 (defun ids-insert-chars-including-components (components
283                                               &optional level ignored-chars)
284   (unless level
285     (setq level 0))
286   (let (is dis i as bs)
287     (dolist (c (sort (copy-tree (ideographic-products-find components))
288                      (lambda (a b)
289                        (if (setq as (char-total-strokes a))
290                            (if (setq bs (char-total-strokes b))
291                                (if (= as bs)
292                                    (ideograph-char< a b)
293                                  (< as bs))
294                              t)
295                          (ideograph-char< a b)))))
296       (unless (memq c ignored-chars)
297         (setq is (char-feature c 'ideographic-structure))
298         (setq i 0)
299         (while (< i level)
300           (insert "\t")
301           (setq i (1+ i)))
302         (insert (ids-find-format-line c is))
303         (setq ignored-chars
304               (ids-insert-chars-including-components
305                (char-to-string c) (1+ level)
306                (cons c ignored-chars))))
307       ))
308   ignored-chars)
309 ;; (defun ids-insert-chars-including-components (components level)
310 ;;   (let (is dis i)
311 ;;     (dolist (c (ideographic-products-find components))
312 ;;       (setq is (char-feature c 'ideographic-structure))
313 ;;       (setq i 0)
314 ;;       (while (< i level)
315 ;;         (insert "\t")
316 ;;         (setq i (1+ i)))
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))
321 ;;       )))
322
323 ;;;###autoload
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)
329     (erase-buffer)
330     (ids-insert-chars-including-components components 0)
331     ;; (let (is dis)
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))
337     ;;     ;;              components)
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))
343     ;;     ;;                  components)
344     ;;       (insert "\t")
345     ;;       (insert (ids-find-format-line dc dis))
346     ;;       (forward-line -1)
347     ;;     ;;       )
348     ;;       )
349     ;;     (insert (ids-find-format-line c is))
350     ;;     (forward-line -1)
351     ;;     ;;   )
352     ;;     )
353     ;;   )
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)
361 ;;     (erase-buffer)
362 ;;     (map-char-attribute
363 ;;      (lambda (c v)
364 ;;        (when (every (lambda (p)
365 ;;                       (ideographic-structure-member p v))
366 ;;                     components)
367 ;;          (insert (ids-find-format-line c v)))
368 ;;        nil)
369 ;;      'ideographic-structure)
370 ;;     (goto-char (point-min)))
371 ;;   (view-buffer ids-find-result-buffer))
372
373 ;;;###autoload
374 (define-obsolete-function-alias 'ideographic-structure-search-chars
375   'ids-find-chars-including-components)
376
377 ;;;###autoload
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)
385     (erase-buffer)
386     (let (ucs jis)
387       (map-char-attribute
388        (lambda (c v)
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))
394
395
396 ;;; @ End.
397 ;;;
398
399 (provide 'ids-find)
400
401 ;;; ids-find.el ends here