Reformatted.
[chise/chiset.git] / char-db-turtle.el
1 ;;; char-db-turtle.el --- Character Database utility -*- coding: utf-8-er; -*-
2
3 ;; Copyright (C) 2017 MORIOKA Tomohiko.
4
5 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
6 ;; Keywords: CHISE, Character Database, RDF, Turtle, ISO/IEC 10646, UCS, Unicode, MULE.
7
8 ;; This file is part of CHISET (CHISE/Turtle).
9
10 ;; XEmacs CHISE 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 ;; XEmacs CHISE is distributed in the hope that it will be useful,
16 ;; but 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 XEmacs CHISE; 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 (require 'char-db-util)
28 (require 'cwiki-common)
29 (require 'isd-turtle)
30 (require 'ideograph-util)
31
32 (setq est-coded-charset-priority-list
33   '(; =ucs
34     =mj
35     =adobe-japan1-0
36     =adobe-japan1-1
37     =adobe-japan1-2
38     =adobe-japan1-3
39     =adobe-japan1-4
40     =adobe-japan1-5
41     =adobe-japan1-6
42     =ucs@iso
43     =jis-x0208 =jis-x0208@1990
44     =jis-x0213-1
45     =jis-x0213-1@2000 =jis-x0213-1@2004
46     =jis-x0213-2
47     =jis-x0212
48     =gt
49     =hanyo-denshi/ks
50     =hanyo-denshi/tk
51     =ucs-itaiji-001
52     =ucs-itaiji-002
53     =ucs-itaiji-003
54     =ucs-itaiji-004
55     =ucs-itaiji-005
56     =ucs-itaiji-006
57     =ucs-itaiji-084
58     =ucs-var-001
59     =ucs-var-002
60     =ucs-var-004
61     =cns11643-1 =cns11643-2 =cns11643-3
62     =cns11643-4 =cns11643-5 =cns11643-6 =cns11643-7
63     =gb2312
64     =big5-cdp
65     =ks-x1001
66     =gt-k
67     =ucs@unicode
68     =ucs@JP/hanazono
69     =gb12345
70     =ucs@cns
71     =zinbun-oracle =>zinbun-oracle
72     =daikanwa
73     =ruimoku-v6
74     =cbeta =jef-china3
75     =+>ucs@iso =+>ucs@unicode
76     =+>ucs@jis
77     =+>ucs@cns
78     =+>ucs@ks
79     =>mj
80     =>jis-x0208 =>jis-x0213-1
81     =>jis-x0208@1997
82     =>ucs@iwds-1
83     =>ucs@component
84     =>iwds-1
85     =>ucs@iso
86     =>ucs@unicode
87     =>ucs@jis =>ucs@cns =>ucs@ks
88     =>gt
89     =>gt-k
90     =>>ucs@iso =>>ucs@unicode
91     =>>ucs@jis =>>ucs@cns =>>ucs@ks
92     =>>gt-k
93     ==mj
94     ==ucs@iso
95     ==ucs@unicode
96     ==adobe-japan1-0
97     ==adobe-japan1-1
98     ==adobe-japan1-2
99     ==adobe-japan1-3
100     ==adobe-japan1-4
101     ==adobe-japan1-5
102     ==adobe-japan1-6
103     ==ks-x1001
104     ==hanyo-denshi/ks
105     ==hanyo-denshi/tk
106     ==ucs@jis
107     ==gt
108     ==cns11643-1 ==cns11643-2 ==cns11643-3
109     ==cns11643-4 ==cns11643-5 ==cns11643-6 ==cns11643-7
110     ==jis-x0212
111     ==ucs@cns
112     ==koseki
113     ==daikanwa
114     ==gt-k
115     ==ucs@JP/hanazono
116     =>>jis-x0208 =>>jis-x0213-1 =>>jis-x0213-2
117     =+>jis-x0208 =+>jis-x0213-1 =+>jis-x0213-2
118     =+>hanyo-denshi/jt
119     =+>jis-x0208@1978
120     =>>gt
121     =+>adobe-japan1
122     =>>adobe-japan1
123     =jis-x0208@1983 =jis-x0208@1978
124     =>ucs-itaiji-001
125     =>ucs-itaiji-002
126     =>ucs-itaiji-005
127     ==>ucs@bucs
128     =big5
129     =>cbeta
130     ===mj
131     ===ucs@iso
132     ===ucs@unicode
133     ===hanyo-denshi/ks
134     ===ks-x1001
135     ===gt
136     ===gt-k
137     ===ucs@ks
138     ===ucs@gb
139     =shinjigen
140     =shinjigen@1ed
141     =shinjigen/+p@rev
142     ==shinjigen
143     ==shinjigen@rev
144     ==daikanwa/+p
145     ===daikanwa/+p
146     =>daikanwa/ho
147     ===daikanwa/ho
148     ))
149
150 (defvar chise-turtle-ccs-prefix-alist nil)
151
152 (defun charset-code-point-format-spec (ccs)
153   (cond ((memq ccs '(=ucs))
154          "0x%04X")
155         (t
156          (let ((ccs-name (symbol-name ccs)))
157            (cond
158             ((string-match
159               "\\(shinjigen\\|daikanwa/ho\\|=>iwds-1\\)"
160               ccs-name)
161              "%04d")
162             ((string-match
163               "\\(gt\\|daikanwa\\|adobe-japan1\\|cbeta\\|zinbun-oracle\\|hng\\)"
164               ccs-name)
165              "%05d")
166             ((string-match "\\(hanyo-denshi/ks\\|koseki\\|mj\\)" ccs-name)
167              "%06d")
168             ((string-match "hanyo-denshi/tk" ccs-name)
169              "%08d")
170             (t
171              "0x%X"))))))
172
173 (defun chise-turtle-uri-decode-feature-name (uri-feature)
174   (cond ((string= "a.ucs" uri-feature)
175          '=ucs)
176         ((string= "a.big5" uri-feature)
177          '=big5)
178         (t
179          (www-uri-decode-feature-name uri-feature))))
180
181 (defun chise-turtle-uri-encode-ccs-name (feature-name)
182   (cond
183    ((eq '=ucs feature-name)
184     "a.ucs")
185    ((eq '=big5 feature-name)
186     "a.big5")
187    ((eq '==>ucs@bucs feature-name)
188     "bucs")
189    (t
190     (mapconcat (lambda (c)
191                  (cond
192                   ((eq c ?@)
193                    "_")
194                   ((eq c ?+)
195                    "._.")
196                   ((eq c ?=)
197                    ".:.")
198                   (t
199                    (char-to-string c))))
200                (www-uri-encode-feature-name feature-name)
201                ""))))
202
203 (defun chise-turtle-uri-encode-feature-name (feature-name)
204   (cond
205    ((eq '->subsumptive feature-name)
206     ":subsume")
207    ((eq '<-denotational feature-name)
208     ":denotation-of")
209    ((eq '<-formed feature-name)
210     ":form-of")
211    ((eq 'hanyu-dazidian feature-name)
212     "ideo:hanyu-dazidian")
213    (t
214     (concat ":" (chise-turtle-uri-encode-ccs-name feature-name)))))
215
216 (defun chise-turtle-format-ccs-code-point (ccs code-point)
217   (let ((ccs-uri (chise-turtle-uri-encode-ccs-name ccs)))
218     (unless (assoc ccs-uri chise-turtle-ccs-prefix-alist)
219       (setq chise-turtle-ccs-prefix-alist
220             (cons (cons ccs-uri ccs)
221                   chise-turtle-ccs-prefix-alist)))
222     (format "%s:%s"
223             ccs-uri
224             (format (charset-code-point-format-spec ccs)
225                     code-point))))
226
227 (defun chise-turtle-encode-char (object)
228   (let ((ccs-list est-coded-charset-priority-list)
229         ccs ret)
230     (if (setq ret (encode-char object '=ucs))
231         (chise-turtle-format-ccs-code-point '=ucs ret)
232       (while (and ccs-list
233                   (setq ccs (pop ccs-list))
234                   (not (setq ret (encode-char object ccs 'defined-only)))))
235       (cond (ret
236              (chise-turtle-format-ccs-code-point ccs ret)
237              )
238             ((and (setq ccs (car (split-char object)))
239                   (setq ret (encode-char object ccs)))
240              (chise-turtle-format-ccs-code-point ccs ret)
241              )
242             (t
243              (format (if est-hide-cgi-mode
244                          "system-char-id=0x%X"
245                        "system-char-id:0x%X")
246                      (encode-char object 'system-char-id))
247              )))))
248
249 (defun chise-split-feature-name (feature-name)
250   (let (base domain number meta)
251     (setq feature-name (symbol-name feature-name))
252     (if (string-match ".\\*." feature-name)
253         (setq meta (intern
254                     (format ":%s" (substring feature-name (1- (match-end 0)))))
255               feature-name (substring feature-name 0 (1+ (match-beginning 0)))))
256     (if (string-match "\\$_\\([0-9]+\\)$" feature-name)
257         (setq number (car (read-from-string (match-string 1 feature-name)))
258               feature-name (substring feature-name 0 (match-beginning 0))))
259     (if (string-match "@" feature-name)
260         (setq domain (car (read-from-string (substring feature-name (match-end 0))))
261               base (intern (substring feature-name 0 (match-beginning 0))))
262       (setq base (intern feature-name)))
263     (list base domain number meta)))
264
265 (defun chise-compose-feature-name (base domain number meta)
266   (let ((name (if domain
267                   (format "%s@%s" base domain)
268                 (symbol-name base))))
269     (if number
270         (setq name (format "%s$_%d" name number)))
271     (if meta
272         (setq name (format "%s*%s" name
273                            (substring (symbol-name meta) 1))))
274     (intern name)))
275
276 (defvar chise-feature-name-base-metadata-alist nil)
277
278 (defun chise-update-feature-name-base-metadata-alist ()
279   (interactive)
280   (let (base domain number metadata
281              bcell dcell ncell ret)
282     (setq chise-feature-name-base-metadata-alist nil)
283     (dolist (fname (sort (char-attribute-list)
284                          #'char-attribute-name<))
285       (setq ret (chise-split-feature-name fname)
286             base (car ret)
287             domain (nth 1 ret)
288             number (nth 2 ret)
289             metadata (nth 3 ret))
290       (when metadata
291         (if (setq bcell (assq base chise-feature-name-base-metadata-alist))
292             (if (setq dcell (assq domain (cdr bcell)))
293                 (if (setq ncell (assq number (cdr dcell)))
294                     (unless (memq metadata (cdr ncell))
295                       (setcdr ncell (nconc (cdr ncell)
296                                            (list metadata))))
297                   (setcdr dcell (cons (list number metadata)
298                                       (cdr dcell))))
299               (setcdr bcell (cons (list domain (list number metadata))
300                                   (cdr bcell))))
301           (setq chise-feature-name-base-metadata-alist
302                 (cons (list base (list domain (list number metadata)))
303                       chise-feature-name-base-metadata-alist))
304           )))))
305
306 (chise-update-feature-name-base-metadata-alist)
307
308 (defun chise-get-char-attribute-with-metadata (character feature-name-base domain)
309   (let ((value (get-char-attribute
310                 character
311                 (chise-compose-feature-name feature-name-base domain nil nil)
312                 '*feature-value-is-empty*))
313         dcell
314         base-metadata metadata
315         ret m i rest dest)
316     (unless (eq value '*feature-value-is-empty*)
317       (cond
318        ((and (setq ret (assq feature-name-base
319                              chise-feature-name-base-metadata-alist))
320              (setq dcell (assq domain (cdr ret))))
321         (if (setq ret (assq nil (cdr dcell)))
322             (dolist (bmn (cdr ret))
323               (when (setq m (get-char-attribute
324                              character
325                              (chise-compose-feature-name
326                               feature-name-base domain nil bmn)))
327                 (setq base-metadata
328                       (list* bmn m base-metadata)))))
329         (setq i 1
330               rest value)
331         (while (consp rest)
332           (setq dest
333                 (cons (cond
334                        ((setq ret (assq i (cdr dcell)))
335                         (setq metadata nil)
336                         (dolist (mn (cdr ret))
337                           (when (setq m (get-char-attribute
338                                          character
339                                          (chise-compose-feature-name
340                                           feature-name-base domain i mn)))
341                             (setq metadata (list* mn m metadata))))
342                         (if metadata
343                             (list* :target (car rest) metadata)
344                           (car rest))
345                         )
346                        (t (car rest)))
347                       dest))
348           (setq i (1+ i)
349                 rest (cdr rest)))
350         (list (nconc (nreverse dest) rest)
351               base-metadata)
352         )
353        (t (list value nil)))
354       )))
355           
356 (defun chise-split-ccs-name (ccs)
357   (cond ((eq ccs '=ucs)
358          '(ucs abstract-character nil)
359          )
360         ((eq ccs '=big5)
361          '(big5 abstract-character nil)
362          )
363         (t
364          (setq ccs (symbol-name ccs))
365          (let (ret)
366            (if (string-match "^\\(=[=+>]*\\)\\([^=>@*]+\\)@?" ccs)
367                (list (intern (match-string 2 ccs))
368                      (chise-decode-ccs-prefix (match-string 1 ccs))
369                      (if (string= (setq ret (substring ccs (match-end 0))) "")
370                          nil
371                        (intern ret))))
372            ))))
373
374 (defun chise-decode-ccs-prefix (ccs)
375   (or (cdr (assoc ccs '(("==>" . super-abstract-character)
376                         ("=>"  . abstract-character)
377                         ("=+>" . unified-glyph)
378                         ("="   . abstract-glyph)
379                         ("=>>" . detailed-glyph)
380                         ("=="  . abstract-glyph-form)
381                         ("===" . glyph-image))))
382       'character))
383
384 (defun chise-turtle-uri-split-ccs (uri-ccs)
385   (cond
386    ((string-match "^a2\\." uri-ccs)
387     (cons ":super-abstract-character"
388           (substring uri-ccs (match-end 0)))
389     )
390    ((string-match "^a\\." uri-ccs)
391     (cons ":abstract-character"
392           (substring uri-ccs (match-end 0)))
393     )
394    ((string-match "^o\\." uri-ccs)
395     (cons ":unified-glyph"
396           (substring uri-ccs (match-end 0)))
397     )
398    ((string-match "^rep\\." uri-ccs)
399     (cons ":abstract-glyph"
400           (substring uri-ccs (match-end 0)))
401     )
402    ((string-match "^g\\." uri-ccs)
403     (cons ":detailed-glyph"
404           (substring uri-ccs (match-end 0)))
405     )
406    ((string-match "^g2\\." uri-ccs)
407     (cons ":abstract-glyph-form"
408           (substring uri-ccs (match-end 0)))
409     )
410    ((string-match "^gi\\." uri-ccs)
411     (cons ":abstract-glyph-form"
412           (substring uri-ccs (match-end 0)))
413     )
414    ((string-match "^repi\\." uri-ccs)
415     (cons ":glyph-image"
416           (substring uri-ccs (match-end 0)))
417     )
418    (t (cons ":character" uri-ccs))))
419
420 (defun char-db-turtle-insert-relation-feature (char name value line-breaking
421                                                     ccss readable)
422   (insert (format "    %s%s        "
423                   (chise-turtle-uri-encode-feature-name name)
424                   line-breaking))
425   (char-db-turtle-insert-relations value readable)
426   (insert " ;")
427   )
428
429 (defun char-db-turtle-insert-metadata (name value)
430   (let (col indent)
431     (insert (format "%-7s " name))
432     (cond
433      ((or (eq name :sources)
434           (eq name :denied))
435       (setq col (current-column))
436       (setq indent (make-string col ?\ ))
437       (insert (format "chisebib:%s"
438                       (chise-turtle-uri-encode-ccs-name (car value))))
439       (dolist (source (cdr value))
440         (insert (format " ,\n%schisebib:%s" indent
441                         (chise-turtle-uri-encode-ccs-name source))))
442       nil)
443      (t
444       (insert (format "%S" value))
445       nil))))
446
447 (defun char-db-turtle-insert-radical (radical-number)
448   (insert (format "        %3d ; # %c"
449                   radical-number
450                   (ideographic-radical radical-number)))
451   'with-separator)
452
453 (defun char-db-turtle-insert-list (value &optional readable)
454   (let (lbs separator rest cell al cal key ret)
455     (insert "( ")
456     (setq lbs (concat "\n" (make-string (current-column) ?\ ))
457           separator nil)
458     (while (consp value)
459       (setq cell (car value))
460       (if (and (consp cell)
461                (consp (car cell))
462                (setq ret (condition-case nil
463                              (find-char cell)
464                            (error nil))))
465           (progn
466             (setq rest cell
467                   al nil
468                   cal nil)
469             (while rest
470               (setq key (car (car rest)))
471               (if (find-charset key)
472                   (setq cal (cons key cal))
473                 (setq al (cons key al)))
474               (setq rest (cdr rest)))
475             (if separator
476                 (insert lbs))
477             (char-db-turtle-insert-char-features ret
478                                                  readable
479                                                  al
480                                                  nil 'for-sub-node)
481             (setq separator lbs))
482         (setq ret (prin1-to-string cell))
483         (if separator
484             (if (< (+ (current-column)
485                       (length ret)
486                       (length separator))
487                    76)
488                 (insert separator)
489               (insert lbs)))
490         (insert ret)
491         (setq separator " "))
492       (setq value (cdr value)))
493     (insert " ) ;")
494     'with-separator))
495
496 (defun char-db-turtle-insert-source-list (value &optional readable)
497   (let (lbs separator rest cell al cal key ret)
498     (setq lbs (concat " ,\n" (make-string (current-column) ?\ ))
499           separator nil)
500     (while (consp value)
501       (setq cell (car value))
502       (if (and (consp cell)
503                (consp (car cell))
504                (setq ret (condition-case nil
505                              (find-char cell)
506                            (error nil))))
507           (progn
508             (setq rest cell
509                   al nil
510                   cal nil)
511             (while rest
512               (setq key (car (car rest)))
513               (if (find-charset key)
514                   (setq cal (cons key cal))
515                 (setq al (cons key al)))
516               (setq rest (cdr rest)))
517             (if separator
518                 (insert lbs))
519             (char-db-turtle-insert-char-features ret
520                                                  readable
521                                                  al
522                                                  nil 'for-sub-node)
523             (setq separator lbs))
524         (setq ret (prin1-to-string cell))
525         (if separator
526             (if (< (+ (current-column)
527                       (length ret)
528                       (length separator))
529                    76)
530                 (insert separator)
531               (insert lbs)))
532         (if (string-match "=" ret)
533             (insert (format "%s:%s"
534                             (substring ret 0 (match-beginning 0))
535                             (substring ret (match-end 0))))
536           (insert (format "chisebib:%s" ret)))
537         (setq separator " , "))
538       (setq value (cdr value)))
539     (insert " ;")
540     'with-separator))
541
542 (defun char-db-turtle-insert-relations (value &optional readable)
543   (let ((lbs (concat "\n" (make-string (current-column) ?\ )))
544         separator cell)
545     (if (characterp value)
546         (setq value (list value)))
547     (while (consp value)
548       (setq cell (car value))
549       (if (integerp cell)
550           (setq cell (decode-char '=ucs cell)))
551       (if separator
552           (insert separator)
553         (setq separator (format " ,%s" lbs)))
554       (if (characterp cell)
555           (insert (format "%-20s" (chise-turtle-encode-char cell)))
556         (char-db-turtle-insert-char-ref cell 'ideographic-combination))
557       (setq value (cdr value)))
558     nil))
559
560 (defun char-db-turtle-insert-target-value (value feature-name-base &optional readable)
561   (cond ((eq feature-name-base 'ideographic-radical)
562          (char-db-turtle-insert-radical value)
563          )
564         ((or (eq feature-name-base 'ideographic-combination)
565              (eq feature-name-base '=decomposition)
566              (string-match "^\\(->\\|<-\\)[^*]*$" (symbol-name feature-name-base)))
567          (char-db-turtle-insert-relations value readable)
568          )
569         ((eq feature-name-base 'comment)
570          (insert (format "%S" value))
571          nil)
572         ((eq feature-name-base 'sources)
573          (char-db-turtle-insert-source-list value readable)
574          )
575         ((consp value)
576          (char-db-turtle-insert-list value readable)
577          )
578         (t
579          (insert (format " %-14s" value))
580          nil)))
581
582 (defun char-db-turtle-insert-feature-value (value metadata domain feature-name-base)
583   (let (indent0 indent rest mdname mdval lb)
584     (cond
585      ((or metadata domain)
586       (setq indent0 (make-string (current-column) ?\ ))
587       (insert "[ ")
588       (setq indent (make-string (current-column) ?\ ))
589       (when domain
590         (insert (format ":context domain:%-7s ;"
591                         (chise-turtle-uri-encode-ccs-name domain)))
592         (setq lb t))
593       (setq rest metadata)
594       (while rest
595         (setq mdname (pop rest)
596               mdval  (pop rest))
597         (if lb
598             (insert (format "\n%s" indent))
599           (setq lb t))
600         (unless (char-db-turtle-insert-metadata mdname mdval)
601           (insert " ;")))
602       (if lb
603           (insert (format "\n%s" indent)))
604       (insert ":target  ")
605       (if (char-db-turtle-insert-target-value value feature-name-base)
606           (insert (format "\n%s] ;" indent0))
607         (insert " ] ;"))
608       'with-separator)
609      (t
610       (char-db-turtle-insert-target-value value feature-name-base)
611       ))))
612
613 (defun char-db-turtle-insert-char-ref (char-ref feature-name-base)
614   (let (indent0 indent rest mdname mdval lb last-sep)
615     (setq indent0 (make-string (current-column) ?\ ))
616     (insert "[ ")
617     (setq indent (make-string (current-column) ?\ ))
618     (setq rest char-ref)
619     (while rest
620       (setq mdname (pop rest)
621             mdval  (pop rest))
622       (if lb
623           (insert (format "%s\n%s"
624                           (if last-sep
625                               ""
626                             " ;")
627                           indent))
628         (setq lb t))
629       (setq last-sep
630             (cond ((eq mdname :target)
631                    (insert ":target  ")
632                    (char-db-turtle-insert-target-value mdval feature-name-base)
633                      )
634                   (t
635                    (char-db-turtle-insert-metadata mdname mdval)))))
636     (if last-sep
637         (insert (format "\n%s]" indent0))
638       (insert " ]"))
639     nil))
640   
641 (defun char-db-turtle-insert-char-features (char
642                                             &optional readable attributes column
643                                             for-sub-node)
644   (unless column
645     (setq column (current-column)))
646   (let ((est-coded-charset-priority-list est-coded-charset-priority-list)
647         (est-view-url-prefix "http://chise.org/est/view")
648         id obj-id type domain
649         name value metadata
650         name-base name-domain
651         radical strokes
652         (line-breaking (concat "\n" (make-string column ?\ )))
653         line-separator
654         ret
655         skey
656         dest-ccss ; sources required-features
657         ccss eq-cpos-list
658         uri-ccs uri-cpos ccs-base children child-ccs-list col indent)
659     (let (atr-d)
660       (setq attributes
661             (sort (if attributes
662                       (if (consp attributes)
663                           (progn
664                             (dolist (name attributes)
665                               (unless (or (memq name char-db-ignored-attributes)
666                                           (string-match "\\*" (symbol-name name)))
667                                 (if (find-charset name)
668                                     (push name ccss))
669                                 (push name atr-d)))
670                             atr-d))
671                     (dolist (name (char-attribute-list))
672                       (unless (or (memq name char-db-ignored-attributes)
673                                   (string-match "\\*" (symbol-name name)))
674                         (if (find-charset name)
675                             (push name ccss))
676                         (push name atr-d)))
677                     atr-d)
678                   #'char-attribute-name<)))
679     (setq line-separator line-breaking)
680     (setq id (chise-turtle-encode-char char))
681     (setq obj-id (file-name-nondirectory id))
682     (string-match ":" obj-id)
683     (setq uri-ccs (substring obj-id 0 (match-beginning 0))
684           uri-cpos (substring obj-id (match-end 0)))
685     (insert (format "%s" obj-id))
686     (setq ret (assoc uri-ccs chise-turtle-ccs-prefix-alist))
687     (setq dest-ccss (list (cdr ret)))
688     (setq ret (chise-split-ccs-name (cdr ret)))
689     (setq ccs-base (car ret)
690           type (nth 1 ret)
691           domain (nth 2 ret))
692     (insert (format "%s    a chisegg:%s ;" line-separator type))
693     (insert (format "%s    :%s-of" line-breaking type))
694     (if (null domain)
695         (insert (format " %s:%s ;"
696                         (chise-turtle-uri-encode-ccs-name ccs-base) uri-cpos))
697       (insert " [ ")
698       (setq col (current-column))
699       (insert (format ":context domain:%-7s ;\n%s:target %7s:%-7s ] ;"
700                       (chise-turtle-uri-encode-ccs-name domain)
701                       (make-string col ?\ )
702                       (chise-turtle-uri-encode-ccs-name ccs-base) uri-cpos)))
703     (when (memq '<-subsumptive attributes)
704       (when (or readable (not for-sub-node))
705         (when (setq value (get-char-attribute char '<-subsumptive))
706           (insert line-separator)
707           (char-db-turtle-insert-relation-feature char '<-subsumptive value
708                                                   line-breaking
709                                                   ccss readable)
710           ))
711       (setq attributes (delq '<-subsumptive attributes))
712       )
713     (when (and (memq '<-denotational attributes)
714                (setq value (get-char-attribute char '<-denotational)))
715       (insert line-separator)
716       (char-db-turtle-insert-relation-feature char '<-denotational value
717                                             line-breaking
718                                             ccss readable)
719       (setq attributes (delq '<-denotational attributes)))
720     (when (and (memq '<-denotational@component attributes)
721                (setq value
722                      (get-char-attribute char '<-denotational@component)))
723       (insert line-separator)
724       (char-db-turtle-insert-relation-feature
725        char '<-denotational@component value
726        line-breaking
727        ccss readable)
728       (setq attributes (delq '<-denotational@component attributes)))
729     (when (and (memq 'name attributes)
730                (setq value (get-char-attribute char 'name)))
731       (insert (format "%s    " line-separator))
732       (insert (format
733                (if (> (+ (current-column) (length value)) 48)
734                    ":name %S ;"
735                  ":name                 %S ;")
736                value))
737       (setq attributes (delq 'name attributes))
738       )
739     (when (and (memq 'name* attributes)
740                (setq value (get-char-attribute char 'name*)))
741       (insert line-separator)
742       (insert (format
743                (if (> (+ (current-column) (length value)) 48)
744                    " \"name*\": %S"
745                  " \"name*\":                 %S")
746                value))
747       (setq attributes (delq 'name* attributes))
748       )
749     (when (and (memq 'script attributes)
750                (setq value (get-char-attribute char 'script)))
751       (insert (format "%s    :script\t\t  ( %s ) ;"
752                       line-separator
753                       (mapconcat (lambda (cell)
754                                    (format "script:%s" cell))
755                                  value " ")))
756       (setq attributes (delq 'script attributes))
757       )
758     ;; (dolist (name '(=>ucs =>ucs*))
759     ;;   (when (and (memq name attributes)
760     ;;              (setq value (get-char-attribute char name)))
761     ;;     (insert line-separator)
762     ;;     (insert (format " \"%-20s\":  #x%04X,\t\"_comment\": \"%c\""
763     ;;                     name value (decode-char '=ucs value)))
764     ;;     (setq attributes (delq name attributes))))
765     (when (and (memq '=>ucs attributes)
766                (setq value (get-char-attribute char '=>ucs)))
767       (insert (format "%s    :to.ucs\t\t  a.ucs:0x%04X ; # %c"
768                       line-separator value (decode-char '=ucs value)))
769       (setq attributes (delq '=>ucs attributes))
770       )
771     (when (setq value (get-char-attribute char '=>ucs*))
772       (insert (format "%s    :to.canonical-ucs\ta.ucs:0x%04X ; # %c"
773                       line-separator value (decode-char '=ucs value)))
774       (setq attributes (delq '=>ucs* attributes))
775       )
776     (dolist (name '(=>ucs@gb =>ucs@big5))
777       (when (and (memq name attributes)
778                  (setq value (get-char-attribute char name)))
779         (insert line-separator)
780         (insert (format " \"%-20s\":  #x%04X,\t\"_comment\": \"%c\"%s"
781                         name value
782                         (decode-char (intern
783                                       (concat "="
784                                               (substring
785                                                (symbol-name name) 2)))
786                                      value)
787                         line-breaking))
788         (setq attributes (delq name attributes))
789         ))
790     (when (and (memq 'general-category attributes)
791                (setq value (get-char-attribute char 'general-category)))
792       (insert (format "%s    :general-category     \"%s\" ; # %s"
793                       line-separator value
794                       (cond ((rassoc value unidata-normative-category-alist)
795                              "Normative Category")
796                             ((rassoc value unidata-informative-category-alist)
797                              "Informative Category")
798                             (t
799                              "Unknown Category"))))
800       (setq attributes (delq 'general-category attributes))
801       )
802     (when (and (memq 'bidi-category attributes)
803                (setq value (get-char-attribute char 'bidi-category)))
804       (insert (format "%s    :bidi-category        %S ;"
805                       line-separator
806                       value))
807       (setq attributes (delq 'bidi-category attributes))
808       )
809     (unless (or (not (memq 'mirrored attributes))
810                 (eq (setq value (get-char-attribute char 'mirrored 'empty))
811                     'empty))
812       (insert (format "%s    :mirrored             \"%s\" ;"
813                       line-separator
814                       value))
815       (setq attributes (delq 'mirrored attributes))
816       )
817     (cond
818      ((and (memq 'decimal-digit-value attributes)
819            (setq value (get-char-attribute char 'decimal-digit-value)))
820       (insert (format "%s    :decimal-digit-value  %2d ;"
821                       line-separator value))
822       (setq attributes (delq 'decimal-digit-value attributes))
823       (when (and (memq 'digit-value attributes)
824                  (setq value (get-char-attribute char 'digit-value)))
825         (insert (format "%s    :digit-value\t  %2d ;"
826                         line-separator value))
827         (setq attributes (delq 'digit-value attributes))
828         )
829       (when (and (memq 'numeric-value attributes)
830                  (setq value (get-char-attribute char 'numeric-value)))
831         (insert (format "%s    :numeric-value\t  %2d ;"
832                         line-separator value))
833         (setq attributes (delq 'numeric-value attributes))
834         )
835       )
836      (t
837       (when (and (memq 'digit-value attributes)
838                  (setq value (get-char-attribute char 'digit-value)))
839         (insert line-separator)
840         (insert (format "%s    :digit-value\t  %2d ;"
841                         line-separator value))
842         (setq attributes (delq 'digit-value attributes))
843         )
844       (when (and (memq 'numeric-value attributes)
845                  (setq value (get-char-attribute char 'numeric-value)))
846         (insert line-separator)
847         (insert (format "%s    :numeric-value\t  %2d ;"
848                         line-separator value))
849         (setq attributes (delq 'numeric-value attributes))
850         )))
851     (when (and (memq 'iso-10646-comment attributes)
852                (setq value (get-char-attribute char 'iso-10646-comment)))
853       (insert line-separator)
854       (insert (format "{\"iso-10646-comment\":\t %S}%s"
855                       value
856                       line-breaking))
857       (setq attributes (delq 'iso-10646-comment attributes))
858       )
859     (when (and (memq 'morohashi-daikanwa attributes)
860                (setq value (get-char-attribute char 'morohashi-daikanwa)))
861       (insert line-separator)
862       (insert (format "%s    :morohashi-daikanwa\t  %S ;"
863                       line-separator value))
864       (setq attributes (delq 'morohashi-daikanwa attributes))
865       )
866     (setq radical nil
867           strokes nil)
868     (when (and (memq 'ideographic-radical attributes)
869                (setq value (get-char-attribute char 'ideographic-radical)))
870       (setq radical value)
871       (insert (format "%s    ideo:radical         %3d ; # %c "
872                       line-separator
873                       radical
874                       (ideographic-radical radical)
875                       ))
876       (setq attributes (delq 'ideographic-radical attributes))
877       )
878     (when (and (memq 'shuowen-radical attributes)
879                (setq value (get-char-attribute char 'shuowen-radical)))
880       (insert line-separator)
881       (insert (format " \"shuowen-radical\":\t %S,\t\"_comment\": \"%c\""
882                       value
883                       (shuowen-radical value)))
884       (setq attributes (delq 'shuowen-radical attributes))
885       )
886     (let (key)
887       (dolist (domain
888                (append
889                 char-db-feature-domains
890                 (let (dest domain)
891                   (dolist (feature (char-attribute-list))
892                     (setq feature (symbol-name feature))
893                     (when (string-match
894                            "\\(radical\\|strokes\\)@\\([^@*]+\\)\\(\\*\\|$\\)"
895                            feature)
896                       (setq domain (intern (match-string 2 feature)))
897                      (unless (memq domain dest)
898                        (setq dest (cons domain dest)))))
899                   (sort dest #'string<))))
900         (setq key (intern (format "%s@%s" 'ideographic-radical domain)))
901         (when (and (memq key attributes)
902                    (setq value (get-char-attribute char key)))
903           (setq radical value)
904           (insert (format "%s    ideo:radical           "
905                           line-separator))
906           (char-db-turtle-insert-feature-value value nil domain 'ideographic-radical)
907           (setq attributes (delq key attributes))
908           )
909         (setq key (intern (format "%s@%s" 'ideographic-strokes domain)))
910         (when (and (memq key attributes)
911                    (setq value (get-char-attribute char key)))
912           (setq strokes value)
913           (insert (format "%s    ideo:strokes           [ "
914                           line-separator))
915           (setq col (current-column))
916           (setq indent (make-string col ?\ ))
917           (insert (format ":context domain:%-7s ;\n%s:target  %S"
918                           (chise-turtle-uri-encode-ccs-name domain)
919                           indent strokes))
920           (setq attributes (delq key attributes))
921           (setq skey (intern (format "%s*sources" key)))
922           (when (and (memq skey attributes)
923                      (setq value (get-char-attribute char skey)))
924             (insert (format " ;\n%s:sources (" indent))
925             (setq col (current-column))
926             (setq indent (make-string col ?\ ))
927             (insert (format " chisebib:%s" (car value)))
928             (dolist (cell (cdr value))
929               (insert (format "\n%s chisebib:%s" indent cell)))
930             (insert " )"))
931           (setq attributes (delq skey attributes))
932           (insert " ] ;")
933           )
934         (setq key (intern (format "%s@%s" 'total-strokes domain)))
935         (when (and (memq key attributes)
936                    (setq value (get-char-attribute char key)))
937           (insert (format "%s    ideo:total-strokes     [ "
938                           line-separator))
939           (setq col (current-column))
940           (insert (format ":context domain:%-7s ;\n%s:target  %S"
941                           domain
942                           (make-string col ?\ )
943                           value))
944           (setq attributes (delq key attributes))
945           (setq skey (intern (format "%s*sources" key)))
946           (insert " ] ;")
947           )
948         (dolist (feature '(ideographic-radical
949                            ideographic-strokes
950                            total-strokes))
951           (setq key (intern (format "%s@%s*sources" feature domain)))
952           (when (and (memq key attributes)
953                      (setq value (get-char-attribute char key)))
954             (insert line-separator)
955             (insert (format " \"%s\":%s" key line-breaking))
956             (dolist (cell value)
957               (insert (format " %s" cell)))
958             (setq attributes (delq key attributes))
959             ))
960         ))
961     (when (and (memq 'ideographic-strokes attributes)
962                (setq value (get-char-attribute char 'ideographic-strokes)))
963       (setq strokes value)
964       (insert (format "%s    ideo:strokes          %2d ;"
965                       line-separator strokes))
966       (setq attributes (delq 'ideographic-strokes attributes))
967       )
968     (when (and (memq 'kangxi-radical attributes)
969                (setq value (get-char-attribute char 'kangxi-radical)))
970       (unless (eq value radical)
971         (insert line-separator)
972         (insert (format "{\"kangxi-radical\":\t%S},\t\"_comment\": \"%c\"%s"
973                         value
974                         (ideographic-radical value)
975                         line-breaking))
976         (or radical
977             (setq radical value)))
978       (setq attributes (delq 'kangxi-radical attributes))
979       )
980     (when (and (memq 'kangxi-strokes attributes)
981                (setq value (get-char-attribute char 'kangxi-strokes)))
982       (unless (eq value strokes)
983         (insert line-separator)
984         (insert (format "{\"kangxi-strokes\":\t%S}%s"
985                         value
986                         line-breaking))
987         (or strokes
988             (setq strokes value)))
989       (setq attributes (delq 'kangxi-strokes attributes))
990       )
991     (when (and (memq 'japanese-strokes attributes)
992                (setq value (get-char-attribute char 'japanese-strokes)))
993       (unless (eq value strokes)
994         (insert line-separator)
995         (insert (format "{\"japanese-strokes\":\t%S}%s"
996                         value
997                         line-breaking))
998         (or strokes
999             (setq strokes value)))
1000       (setq attributes (delq 'japanese-strokes attributes))
1001       )
1002     (when (and (memq 'cns-radical attributes)
1003                (setq value (get-char-attribute char 'cns-radical)))
1004       (insert line-separator)
1005       (insert (format "{\"cns-radical\":\t%S},\t\"_comment\": \"%c\"%s"
1006                       value
1007                       (ideographic-radical value)
1008                       line-breaking))
1009       (setq attributes (delq 'cns-radical attributes))
1010       )
1011     (when (and (memq 'cns-strokes attributes)
1012                (setq value (get-char-attribute char 'cns-strokes)))
1013       (unless (eq value strokes)
1014         (insert line-separator)
1015         (insert (format "{\"cns-strokes\":\t%S}%s"
1016                         value
1017                         line-breaking))
1018         (or strokes
1019             (setq strokes value)))
1020       (setq attributes (delq 'cns-strokes attributes))
1021       )
1022     (when (and (memq 'total-strokes attributes)
1023                (setq value (get-char-attribute char 'total-strokes)))
1024       (insert (format "%s    ideo:total-strokes    %2d ;"
1025                       line-separator value))
1026       (setq attributes (delq 'total-strokes attributes))
1027       )
1028     (if (equal (get-char-attribute char '->titlecase)
1029                (get-char-attribute char '->uppercase))
1030         (setq attributes (delq '->titlecase attributes)))
1031     (unless readable
1032       (dolist (ignored '(composition
1033                          ->denotational <-subsumptive ->ucs-unified
1034                          ->ideographic-component-forms))
1035         (setq attributes (delq ignored attributes))))
1036     (while attributes
1037       (setq name (car attributes))
1038       (setq ret (chise-split-feature-name name))
1039       (setq name-base (car ret)
1040             name-domain (nth 1 ret))
1041       (when (setq value (chise-get-char-attribute-with-metadata
1042                          char name-base name-domain))
1043         (setq metadata (nth 1 value)
1044               value (car value))
1045         (cond ((setq ret (find-charset name))
1046                (setq name (charset-name ret))
1047                (when (not (memq name dest-ccss))
1048                  (setq dest-ccss (cons name dest-ccss))
1049                  (if (null value)
1050                      (insert (format "%s    :%-25s rdf:nil ;" line-separator
1051                                      (chise-turtle-uri-encode-ccs-name name)))
1052                    (setq ret (chise-turtle-format-ccs-code-point name value))
1053                    (insert (format "%s    :eq %-25s ; # %c" line-separator
1054                                    ret
1055                                    (char-db-decode-isolated-char name value)))
1056                    (setq eq-cpos-list (cons (list ret name value) eq-cpos-list))))
1057                (if (find-charset
1058                     (setq ret (if (eq name '=ucs)
1059                                   (if (< value #x10000)
1060                                       '==ucs@unicode
1061                                     '==ucs@iso)
1062                                 (intern (format "=%s" name)))))
1063                    (setq child-ccs-list (cons ret child-ccs-list)))
1064                )
1065               ((and
1066                 (not readable)
1067                 (not (eq name '->subsumptive))
1068                 (not (eq name '->uppercase))
1069                 (not (eq name '->lowercase))
1070                 (not (eq name '->titlecase))
1071                 (not (eq name '->canonical))
1072                 (not (eq name '->Bopomofo))
1073                 (not (eq name '->mistakable))
1074                 (not (eq name '->ideographic-variants))
1075                 (or (eq name '<-identical)
1076                     (eq name '<-uppercase)
1077                     (eq name '<-lowercase)
1078                     (eq name '<-titlecase)
1079                     (eq name '<-canonical)
1080                     (eq name '<-ideographic-variants)
1081                     ;; (eq name '<-synonyms)
1082                     (string-match "^<-synonyms" (symbol-name name))
1083                     (eq name '<-mistakable)
1084                     (when (string-match "^->" (symbol-name name))
1085                       (cond
1086                        ((string-match "^->fullwidth" (symbol-name name))
1087                         (not (and (consp value)
1088                                   (characterp (car value))
1089                                   (encode-char
1090                                    (car value) '=ucs 'defined-only)))
1091                         )
1092                        (t)))
1093                     ))
1094                )
1095               ((eq name 'ideographic-structure)
1096                (insert (isd-turtle-format-char nil nil value nil
1097                                                'isd 'without-head-char))
1098                (insert " ;")
1099                )
1100               ((eq name '->subsumptive)
1101                (insert line-separator)
1102                (char-db-turtle-insert-relation-feature char name value
1103                                                        line-breaking
1104                                                        ccss readable)
1105                (setq children value)
1106                )
1107               (t
1108                (insert (format "%s    %-20s "
1109                                line-separator
1110                                (chise-turtle-uri-encode-feature-name name-base)))
1111                (unless (char-db-turtle-insert-feature-value
1112                         value metadata name-domain name-base)
1113                  (insert " ;"))
1114                )
1115               ))
1116       (setq attributes (cdr attributes)))
1117     (insert (format "%s    ." line-breaking))
1118     (dolist (eq-cpos (nreverse eq-cpos-list))
1119       (setq ret (chise-split-ccs-name (nth 1 eq-cpos)))
1120       (insert (format "%s    %s" line-breaking
1121                       (car eq-cpos)))
1122       (insert (format "%s        %25s" line-breaking
1123                       (format ":%s-of" (nth 1 ret))))
1124       (if (null (nth 2 ret))
1125           (insert (format " %14s:%-7s ."
1126                           (chise-turtle-uri-encode-ccs-name (car ret))
1127                           (nth 1 (split-string (car eq-cpos) ":"))))
1128         (insert " [ ")
1129         (setq col (current-column))
1130         (insert (format ":context domain:%-7s ;\n%s:target %7s:%-7s ] ."
1131                         (chise-turtle-uri-encode-ccs-name (nth 2 ret))
1132                         (make-string col ?\ )
1133                         (chise-turtle-uri-encode-ccs-name (car ret))
1134                         (nth 1 (split-string (car eq-cpos) ":"))))))
1135     (setq est-coded-charset-priority-list
1136           (append est-coded-charset-priority-list
1137                   (nreverse child-ccs-list)))
1138     (when children
1139       (dolist (child children)
1140         (insert (format "%s    " line-breaking))
1141         (char-db-turtle-insert-char-features child nil nil nil 'for-sub-node)))
1142     ))
1143
1144 (defun char-db-turtle-insert-char-data (char &optional readable attributes)
1145   (save-restriction
1146     (narrow-to-region (point)(point))
1147     (char-db-turtle-insert-char-features char readable attributes)
1148     (insert "\n\n")
1149     ))
1150
1151 (defun char-db-turtle-insert-prefix ()
1152   (let (base-ccs-list ret)
1153     (insert "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
1154 @prefix : <http://rdf.chise.org/rdf/property/character/main/> .
1155 @prefix ideo: <http://rdf.chise.org/rdf/property/character/ideo/> .
1156 @prefix isd: <http://rdf.chise.org/rdf/property/character/isd/> .
1157 @prefix idc: <http://rdf.chise.org/rdf/type/character/idc/> .
1158 @prefix chisegg: <http://rdf.chise.org/rdf/type/character/ggg/> .
1159 @prefix domain: <http://rdf.chise.org/data/domain/> .
1160 @prefix script: <http://rdf.chise.org/data/script/> .
1161 @prefix chisebib: <http://rdf.chise.org/data/bibliography/> .
1162 @prefix ruimoku: <http://www.chise.org/est/view/article@ruimoku/rep.id=/> .
1163 @prefix zob1959: <http://chise.zinbun.kyoto-u.ac.jp/koukotsu/rubbings/> .
1164
1165 ")
1166     (dolist (cell (sort chise-turtle-ccs-prefix-alist
1167                         (lambda (a b)
1168                           (char-attribute-name< (cdr a)(cdr b)))))
1169       (insert (format "@prefix %s: <%s/%s=> .\n"
1170                       (car cell)
1171                       "http://www.chise.org/est/view/character"
1172                       (www-uri-encode-feature-name (cdr cell))))
1173       (setq ret (chise-split-ccs-name (cdr cell)))
1174       (unless (memq (car ret) base-ccs-list)
1175         (setq base-ccs-list (cons (car ret) base-ccs-list))))
1176     (insert "\n")
1177     (dolist (base-ccs (nreverse base-ccs-list))
1178       (insert (format "@prefix %s: <%s/%s/code-point/> .\n"
1179                       (chise-turtle-uri-encode-ccs-name base-ccs)
1180                       "http://rdf.chise.org/data/ccs"
1181                       (www-uri-encode-feature-name base-ccs))))))
1182
1183 (defun char-db-turtle-insert-ideograph-radical-char-data (radical)
1184   (let ((chars
1185          (sort (copy-list (aref ideograph-radical-chars-vector radical))
1186                (lambda (a b)
1187                  (ideograph-char< a b radical))))
1188         attributes)
1189     (dolist (name (char-attribute-list))
1190       (unless (memq name char-db-ignored-attributes)
1191         (push name attributes)
1192         ))
1193     (setq attributes (sort attributes #'char-attribute-name<))
1194     (aset ideograph-radical-chars-vector radical chars)
1195     (dolist (char chars)
1196       (when (not (some (lambda (atr)
1197                          (get-char-attribute char atr))
1198                        char-db-ignored-attributes))
1199         (char-db-turtle-insert-char-data char nil attributes)))
1200     ))
1201
1202 (defun char-db-turtle-write-ideograph-radical-char-data (radical file)
1203   (if (file-directory-p file)
1204       (let ((name (char-feature (decode-char 'ucs (+ #x2EFF radical))
1205                                 'name)))
1206         (if (string-match "KANGXI RADICAL " name)
1207             (setq name (capitalize (substring name (match-end 0)))))
1208         (setq name (mapconcat (lambda (char)
1209                                 (if (eq char ? )
1210                                     "-"
1211                                   (char-to-string char))) name ""))
1212         (setq file
1213               (expand-file-name
1214                (format "Ideograph-R%03d-%s.ttl" radical name)
1215                file))))
1216   (let (chise-turtle-ccs-prefix-alist)
1217     (with-temp-buffer
1218       (char-db-turtle-insert-ideograph-radical-char-data radical)
1219       (goto-char (point-min))
1220       (char-db-turtle-insert-prefix)
1221       (insert "\n")
1222       (goto-char (point-min))
1223       (insert (format "# -*- coding: %s -*-\n"
1224                       char-db-file-coding-system))
1225       (let ((coding-system-for-write char-db-file-coding-system))
1226         (write-region (point-min)(point-max) file)))))
1227
1228
1229 ;;; @ end
1230 ;;;
1231
1232 (provide 'char-db-turtle)
1233
1234 ;;; char-db-turtle.el ends here