f0852d93a1cc24dd1d85e058f7ea75004ddf68f7
[chise/isd.git] / isd-turtle.el
1 ;;; isd-turtle.el --- Utility to dump ideographic-structure as Turtle files
2
3 ;; Copyright (C) 2017 MORIOKA Tomohiko
4
5 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
6 ;; Keywords: Ideographic Structures (漢字構造、解字), IDS, CHISE, RDF, Turtle
7
8 ;; This file is a part of CHISE-ISD (Ideographic Structure Database).
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 (require 'cwiki-common)
28
29 (defvar isd-url-prefix "http://rdf.chise.org/data/")
30
31 (setq est-coded-charset-priority-list
32   '(; =ucs
33     =mj
34     =adobe-japan1-0
35     =adobe-japan1-1
36     =adobe-japan1-2
37     =adobe-japan1-3
38     =adobe-japan1-4
39     =adobe-japan1-5
40     =adobe-japan1-6
41     =ucs@iso
42     =jis-x0208 =jis-x0208@1990
43     =jis-x0213-1
44     =jis-x0213-1@2000 =jis-x0213-1@2004
45     =jis-x0213-2
46     =jis-x0212
47     =gt
48     =hanyo-denshi/ks
49     =hanyo-denshi/tk
50     =ucs-itaiji-001
51     =ucs-itaiji-002
52     =ucs-itaiji-003
53     =ucs-itaiji-004
54     =ucs-itaiji-005
55     =ucs-itaiji-006
56     =ucs-itaiji-009
57     =ucs-itaiji-084
58     =ucs-var-001
59     =ucs-var-002
60     =ucs-var-003
61     =ucs-var-004
62     =cns11643-1 =cns11643-2 =cns11643-3
63     =cns11643-4 =cns11643-5 =cns11643-6 =cns11643-7
64     =gb2312
65     =big5-cdp
66     =ks-x1001
67     =gt-k
68     =ucs@unicode
69     =ucs@JP/hanazono
70     =gb12345
71     =ucs@cns
72     =ucs@gb
73     =zinbun-oracle =>zinbun-oracle
74     =daikanwa
75     =ruimoku-v6
76     =cbeta =jef-china3
77     =daikanwa/+2p
78     =+>ucs@iso =+>ucs@unicode
79     =+>ucs@jis
80     =+>ucs@cns
81     =+>ucs@ks
82     =+>ucs@jis/1990
83     =>mj
84     =>jis-x0208 =>jis-x0213-1
85     =>jis-x0208@1997
86     =>ucs@iwds-1
87     =>ucs@component
88     =>iwds-1
89     =>ucs@iso
90     =>ucs@unicode
91     =>ucs@jis =>ucs@cns =>ucs@ks
92     =>gt
93     =>gt-k
94     =>>ucs@iso =>>ucs@unicode
95     =>>ucs@jis =>>ucs@cns =>>ucs@ks
96     =>>gt-k
97     =>>hanyo-denshi/ks
98     ==mj
99     ==ucs@iso
100     ==ucs@unicode
101     ==adobe-japan1-0
102     ==adobe-japan1-1
103     ==adobe-japan1-2
104     ==adobe-japan1-3
105     ==adobe-japan1-4
106     ==adobe-japan1-5
107     ==adobe-japan1-6
108     ==ks-x1001
109     ==hanyo-denshi/ks
110     ==hanyo-denshi/tk
111     ==ucs@jis
112     ==gt
113     ==cns11643-1 ==cns11643-2 ==cns11643-3
114     ==cns11643-4 ==cns11643-5 ==cns11643-6 ==cns11643-7
115     ==jis-x0212
116     ==ucs@cns
117     ==koseki
118     ==daikanwa
119     ==gt-k
120     ==ucs@gb
121     ==ucs-itaiji-003
122     ==ucs@JP/hanazono
123     ==daikanwa/+2p
124     =>>jis-x0208 =>>jis-x0213-1 =>>jis-x0213-2
125     =+>jis-x0208 =+>jis-x0213-1 =+>jis-x0213-2
126     =+>hanyo-denshi/jt
127     =+>jis-x0208@1978
128     =>>gt
129     =+>adobe-japan1
130     =>>adobe-japan1
131     =jis-x0208@1983 =jis-x0208@1978
132     =>ucs-itaiji-001
133     =>ucs-itaiji-002
134     =>ucs-itaiji-005
135     ==>ucs@bucs
136     =big5
137     =>cbeta
138     ===mj
139     ===ucs@iso
140     ===ucs@unicode
141     ===hanyo-denshi/ks
142     ===ks-x1001
143     ===gt
144     ===gt-k
145     ===ucs@ks
146     ===ucs@gb
147     =shinjigen
148     =shinjigen@rev
149     =shinjigen@1ed
150     =shinjigen/+p@rev
151     ==shinjigen
152     ==shinjigen@rev
153     ==daikanwa/+p
154     ==shinjigen@1ed
155     ===daikanwa/+p
156     =>daikanwa/ho
157     ===daikanwa/ho
158     ))
159
160 ;; (defvar isd-turtle-ccs-list nil)
161 (defvar chise-turtle-ccs-prefix-alist nil)
162
163 (defun charset-code-point-format-spec (ccs)
164   (cond ((memq ccs '(=ucs))
165          "0x%04X")
166         (t
167          (let ((ccs-name (symbol-name ccs)))
168            (cond
169             ((string-match
170               "\\(shinjigen\\|daikanwa/ho\\|=>iwds-1\\)"
171               ccs-name)
172              "%04d")
173             ((string-match
174               "\\(gt\\|daikanwa\\|adobe-japan1\\|cbeta\\|zinbun-oracle\\|hng\\)"
175               ccs-name)
176              "%05d")
177             ((string-match "\\(hanyo-denshi/ks\\|koseki\\|mj\\)" ccs-name)
178              "%06d")
179             ((string-match "hanyo-denshi/tk" ccs-name)
180              "%08d")
181             (t
182              "0x%X"))))))
183
184 ;; (defun isd-turtle-uri-encode-feature-name (feature-name)
185 ;;   (cond
186 ;;    ((eq '=ucs feature-name)
187 ;;     "a.ucs")
188 ;;    ((eq '==>ucs@bucs feature-name)
189 ;;     "bucs")
190 ;;    (t
191 ;;     (mapconcat (lambda (c)
192 ;;                  (if (eq c ?@)
193 ;;                      "_"
194 ;;                    (char-to-string c)))
195 ;;                (www-uri-encode-feature-name feature-name)
196 ;;                ""))))
197 (defun chise-turtle-uri-encode-ccs-name (feature-name)
198   (cond
199    ((eq '=ucs feature-name)
200     "a.ucs")
201    ((eq '=big5 feature-name)
202     "a.big5")
203    ((eq '==>ucs@bucs feature-name)
204     "bucs")
205    (t
206     (mapconcat (lambda (c)
207                  (cond
208                   ((eq c ?@)
209                    "_")
210                   ((eq c ?+)
211                    "._.")
212                   ((eq c ?=)
213                    ".:.")
214                   (t
215                    (char-to-string c))))
216                (www-uri-encode-feature-name feature-name)
217                ""))))
218
219 ;; (defun isd-turtle-format-ccs-code-point (ccs code-point)
220 ;;   (unless (memq ccs isd-turtle-ccs-list)
221 ;;     (setq isd-turtle-ccs-list (cons ccs isd-turtle-ccs-list)))
222 ;;   (format "%s:%s"
223 ;;           (isd-turtle-uri-encode-feature-name ccs)
224 ;;           (format (charset-code-point-format-spec ccs)
225 ;;                   code-point)))
226 (defun chise-turtle-format-ccs-code-point (ccs code-point)
227   (let ((ccs-uri (chise-turtle-uri-encode-ccs-name ccs)))
228     (unless (assoc ccs-uri chise-turtle-ccs-prefix-alist)
229       (setq chise-turtle-ccs-prefix-alist
230             (cons (cons ccs-uri ccs)
231                   chise-turtle-ccs-prefix-alist)))
232     (format "%s:%s"
233             ccs-uri
234             (format (charset-code-point-format-spec ccs)
235                     code-point))))
236
237 (defun isd-turtle-encode-char (object)
238   (let ((ccs-list est-coded-charset-priority-list)
239         ccs ret)
240     (if (setq ret (encode-char object '=ucs))
241         (chise-turtle-format-ccs-code-point '=ucs ret)
242       (while (and ccs-list
243                   (setq ccs (pop ccs-list))
244                   (not (setq ret (encode-char object ccs 'defined-only)))))
245       (cond (ret
246              (chise-turtle-format-ccs-code-point ccs ret)
247              )
248             ((and (setq ccs (car (split-char object)))
249                   (setq ret (encode-char object ccs)))
250              (chise-turtle-format-ccs-code-point ccs ret)
251              )
252             (t
253              (format (if est-hide-cgi-mode
254                          "system-char-id=0x%X"
255                        "system-char-id:0x%X")
256                      (encode-char object 'system-char-id))
257              )))))
258
259 (defun isd-turtle-format-component (component separator level prefix)
260   (cond ((characterp component)
261          (format "%s %c # %c"
262                  (isd-turtle-encode-char component)
263                  separator
264                  component)
265          )
266         ((consp component)
267          (let ((ret (find-char component)))
268            (cond (ret
269                   (format "%s %c # %c"
270                           (isd-turtle-encode-char ret) separator ret))
271                  ((setq ret (assq 'ideographic-structure component))
272                   (if (eq separator ?\;)
273                       (format "%s ;"
274                               (isd-turtle-format-char nil nil (cdr ret) (1+ level)
275                                                       prefix))
276                     (isd-turtle-format-char nil nil (cdr ret) (1+ level)
277                                             prefix))))))))
278
279 (defun isd-turtle-format-char (ccs code-point &optional ids-list level
280                                    prefix without-head-char)
281   (unless level
282     (setq level 0))
283   (unless prefix
284     (setq prefix ""))
285   (let ((indent (make-string (* level 4) ?\ ))
286         char
287         idc
288         p1 p2 p3
289         c1 c2 c3
290         ret)
291     (unless ids-list
292       (if (and ccs code-point
293                (setq char (decode-char ccs code-point)))
294           (setq ids-list (get-char-attribute char 'ideographic-structure))))
295     (setq idc (car ids-list))
296     (setq c1 (nth 1 ids-list)
297           c2 (nth 2 ids-list)
298           c3 (nth 3 ids-list))
299     (if (char-ref-p idc)
300         (setq idc (plist-get idc :char)))
301     (if (and (consp idc)
302              (setq ret (find-char idc)))
303         (setq idc ret))
304     (if (and (consp c1)
305              (setq ret (find-char c1)))
306         (setq c1 ret))
307     (if (and (consp c2)
308              (setq ret (find-char c2)))
309         (setq c2 ret))
310     (if (and (consp c3)
311              (setq ret (find-char c3)))
312         (setq c3 ret))
313     (cond
314      ((eq idc ?\u2FF0) ; ⿰
315       (setq p1 'left
316             p2 'right)
317       )
318      ((eq idc ?⿱)
319       (setq p1 'above
320             p2 'below)
321       )
322      ((eq idc ?⿲)
323       (setq p1 'left
324             p2 'middle
325             p3 'right)
326       )
327      ((eq idc ?⿳)
328       (setq p1 'above
329             p2 'middle
330             p3 'below)
331       )
332      ((memq idc '(?⿴ ?⿵ ?⿶ ?⿷ ?⿸ ?⿹ ?⿺))
333       (setq p1 'surround
334             p2 'filling)
335       )
336      ((eq idc ?⿻)
337       (setq p1 'underlying
338             p2 'overlaying)
339       ))
340     (cond
341      (p3
342       (format "%s
343 %s    %s:structure [ a idc:%c ;
344 %s        %s:%-8s %s
345 %s        %s:%-8s %s
346 %s        %s:%-8s %s
347 %s    ]%s"
348               (if without-head-char
349                   ""
350                 (if (and ccs code-point)
351                     (format "%s   # %c"
352                             (chise-turtle-format-ccs-code-point ccs code-point)
353                             char)
354                   "["))
355               indent prefix idc
356               indent prefix p1 (isd-turtle-format-component c1 ?\; (1+ level) prefix)
357               indent prefix p2 (isd-turtle-format-component c2 ?\; (1+ level) prefix)
358               indent prefix p3 (isd-turtle-format-component c3 ?\  (1+ level) prefix)
359               indent
360               (if without-head-char
361                   ""
362                 (if (null char)
363                     (format "\n%s]"
364                             indent)
365                   "")))
366       )
367      (idc
368       (format "%s
369 %s    %s:structure [ a idc:%c ;
370 %s        %s:%-8s %s
371 %s        %s:%-8s %s
372 %s    ]%s"
373               (if without-head-char
374                   ""
375                 (if (and ccs code-point)
376                     (format "%s   # %c"
377                             (chise-turtle-format-ccs-code-point ccs code-point)
378                             char)
379                   "["))
380               indent prefix idc
381               indent prefix p1 (isd-turtle-format-component c1 ?\; (1+ level) prefix)
382               indent prefix p2 (isd-turtle-format-component c2 ?\  (1+ level) prefix)
383               indent
384               (if without-head-char
385                   ""
386                 (if (null char)
387                     (format "\n%s]"
388                             indent)
389                   "")))))
390     ))
391
392 (defun isd-turtle-insert-char (ccs code-point)
393   (let ((ret (isd-turtle-format-char ccs code-point)))
394     (when ret
395       (insert ret)
396       (insert " .\n"))))
397
398 (defun isd-turtle-insert-ccs-ranges (ccs &rest ranges)
399   (let (range code max-code)
400     (while ranges
401       (setq range (car ranges))
402       (cond ((consp range)
403              (setq code (car range)
404                    max-code (cdr range))
405              (while (<= code max-code)
406                (isd-turtle-insert-char ccs code)
407                (setq code (1+ code)))
408              )
409             ((integerp range)
410              (isd-turtle-insert-char ccs range)
411              )
412             (t (error 'wrong-type-argument range)))
413       (setq ranges (cdr ranges)))))
414
415 (defun isd-turtle-dump-range (file path func &rest args)
416   (with-temp-buffer
417     (let ((coding-system-for-write 'utf-8-mcs-er)
418           ;; isd-turtle-ccs-list
419           chise-turtle-ccs-prefix-alist)
420       (if (file-directory-p path)
421           (setq path (expand-file-name file path)))
422       (apply func args)
423       (goto-char (point-min))
424       ;; (dolist (ccs (sort isd-turtle-ccs-list
425       ;;                    #'char-attribute-name<))
426       ;;   (insert (format "@prefix %s: <%s%s=> .\n"
427       ;;                   (isd-turtle-uri-encode-feature-name ccs)
428       ;;                   "http://www.chise.org/est/view/character/"
429       ;;                   (www-uri-encode-feature-name ccs))))
430       (dolist (cell (sort chise-turtle-ccs-prefix-alist
431                           (lambda (a b)
432                             (char-attribute-name< (cdr a)(cdr b)))))
433         (insert (format "@prefix %s: <%s/%s=> .\n"
434                         (car cell)
435                         "http://www.chise.org/est/view/character"
436                         (www-uri-encode-feature-name (cdr cell)))))
437       (insert "\n")
438       (goto-char (point-min))
439       (insert "# -*- coding: utf-8-mcs-er -*-\n")
440       (insert "@prefix : <http://rdf.chise.org/rdf/property/character/isd/> .
441 @prefix idc: <http://rdf.chise.org/rdf/type/character/idc/> .\n")
442       (write-region (point-min)(point-max) path))))
443
444 ;;;###autoload
445 (defun isd-turtle-dump-ucs-basic (filename)
446   (interactive "Fdump ISD-UCS-Basic : ")
447   (isd-turtle-dump-range "ISD-UCS-Basic.ttl" filename
448                          #'isd-turtle-insert-ccs-ranges
449                          '=ucs '(#x4E00 . #x9FA5)))
450
451 ;;;###autoload
452 (defun isd-turtle-dump-ucs-ext-a (filename)
453   (interactive "Fdump ISD-UCS-Ext-A : ")
454   (isd-turtle-dump-range "ISD-UCS-Ext-A.ttl" filename
455                          #'isd-turtle-insert-ccs-ranges
456                          '=ucs '(#x3400 . #x4DB5) #xFA1F #xFA23))
457
458 ;;;###autoload
459 (defun isd-turtle-dump-ucs-ext-b-1 (filename)
460   (interactive "Fdump IDS-UCS-Ext-B-1 : ")
461   (isd-turtle-dump-range "ISD-UCS-Ext-B-1.ttl" filename
462                          #'isd-turtle-insert-ccs-ranges
463                          'ucs '(#x20000 . #x21FFF)))
464
465 ;;;###autoload
466 (defun isd-turtle-dump-ucs-ext-b-2 (filename)
467   (interactive "Fdump IDS-UCS-Ext-B-2 : ")
468   (isd-turtle-dump-range "ISD-UCS-Ext-B-2.ttl" filename
469                          #'isd-turtle-insert-ccs-ranges
470                          'ucs '(#x22000 . #x23FFF)))
471
472 ;;;###autoload
473 (defun isd-turtle-dump-ucs-ext-b-3 (filename)
474   (interactive "Fdump IDS-UCS-Ext-B-3 : ")
475   (isd-turtle-dump-range "ISD-UCS-Ext-B-3.ttl" filename
476                          #'isd-turtle-insert-ccs-ranges
477                          'ucs '(#x24000 . #x25FFF)))
478
479 ;;;###autoload
480 (defun isd-turtle-dump-ucs-ext-b-4 (filename)
481   (interactive "Fdump IDS-UCS-Ext-B-4 : ")
482   (isd-turtle-dump-range "ISD-UCS-Ext-B-4.ttl" filename
483                          #'isd-turtle-insert-ccs-ranges
484                          'ucs '(#x26000 . #x27FFF)))
485
486 ;;;###autoload
487 (defun isd-turtle-dump-ucs-ext-b-5 (filename)
488   (interactive "Fdump IDS-UCS-Ext-B-5 : ")
489   (isd-turtle-dump-range "ISD-UCS-Ext-B-5.ttl" filename
490                          #'isd-turtle-insert-ccs-ranges
491                          'ucs '(#x28000 . #x29FFF)))
492
493 ;;;###autoload
494 (defun isd-turtle-dump-ucs-ext-b-6 (filename)
495   (interactive "Fdump IDS-UCS-Ext-B-6 : ")
496   (isd-turtle-dump-range "ISD-UCS-Ext-B-6.ttl" filename
497                          #'isd-turtle-insert-ccs-ranges
498                          'ucs '(#x2A000 . #x2A6D6)))
499
500 ;;;###autoload
501 (defun isd-turtle-dump-ucs-ext-c (filename)
502   (interactive "Fdump IDS-UCS-Ext-C : ")
503   (isd-turtle-dump-range "ISD-UCS-Ext-C.ttl" filename
504                          #'isd-turtle-insert-ccs-ranges
505                          'ucs '(#x2A700 . #x2B734)))
506
507 ;;;###autoload
508 (defun isd-turtle-dump-ucs-ext-d (filename)
509   (interactive "Fdump IDS-UCS-Ext-D : ")
510   (isd-turtle-dump-range "ISD-UCS-Ext-D.ttl" filename
511                          #'isd-turtle-insert-ccs-ranges
512                          'ucs '(#x2B740 . #x2B81D)))
513
514 ;;;###autoload
515 (defun isd-turtle-dump-ucs-ext-e (filename)
516   (interactive "Fdump IDS-UCS-Ext-E : ")
517   (isd-turtle-dump-range "ISD-UCS-Ext-E.ttl" filename
518                          #'isd-turtle-insert-ccs-ranges
519                          'ucs '(#x2B820 . #x2CEA1)))
520
521 ;;;###autoload
522 (defun isd-turtle-dump-mj-0 (filename)
523   (interactive "Fdump ISD-MJ-0 : ")
524   (isd-turtle-dump-range "ISD-MJ-0.ttl" filename
525                          #'isd-turtle-insert-ccs-ranges
526                          '=mj '(1 . 9999)))
527
528 ;;;###autoload
529 (defun isd-turtle-dump-mj-1 (filename)
530   (interactive "Fdump ISD-MJ-1 : ")
531   (isd-turtle-dump-range "ISD-MJ-1.ttl" filename
532                          #'isd-turtle-insert-ccs-ranges
533                          '=mj '(10000 . 19999)))
534
535 ;;;###autoload
536 (defun isd-turtle-dump-mj-2 (filename)
537   (interactive "Fdump ISD-MJ-2 : ")
538   (isd-turtle-dump-range "ISD-MJ-2.ttl" filename
539                          #'isd-turtle-insert-ccs-ranges
540                          '=mj '(20000 . 29999)))
541
542 ;;;###autoload
543 (defun isd-turtle-dump-mj-3 (filename)
544   (interactive "Fdump ISD-MJ-3 : ")
545   (isd-turtle-dump-range "ISD-MJ-3.ttl" filename
546                          #'isd-turtle-insert-ccs-ranges
547                          '=mj '(30000 . 39999)))
548
549 ;;;###autoload
550 (defun isd-turtle-dump-mj-4 (filename)
551   (interactive "Fdump ISD-MJ-4 : ")
552   (isd-turtle-dump-range "ISD-MJ-4.ttl" filename
553                          #'isd-turtle-insert-ccs-ranges
554                          '=mj '(40000 . 49999)))
555
556 ;;;###autoload
557 (defun isd-turtle-dump-mj-5 (filename)
558   (interactive "Fdump ISD-MJ-5 : ")
559   (isd-turtle-dump-range "ISD-MJ-5.ttl" filename
560                          #'isd-turtle-insert-ccs-ranges
561                          '=mj '(50000 . 59999)))
562
563 ;;;###autoload
564 (defun isd-turtle-dump-mj-6 (filename)
565   (interactive "Fdump ISD-MJ-6 : ")
566   (isd-turtle-dump-range "ISD-MJ-6.ttl" filename
567                          #'isd-turtle-insert-ccs-ranges
568                          '=mj '(60000 . 69999)))
569
570 ;;;###autoload
571 (defun isd-turtle-dump-all (directory)
572   (interactive "DISD directory : ")
573   (isd-turtle-dump-ucs-basic directory)
574   (isd-turtle-dump-ucs-ext-a directory)
575   (isd-turtle-dump-ucs-ext-b-1 directory)
576   (isd-turtle-dump-ucs-ext-b-2 directory)
577   (isd-turtle-dump-ucs-ext-b-3 directory)
578   (isd-turtle-dump-ucs-ext-b-4 directory)
579   (isd-turtle-dump-ucs-ext-b-5 directory)
580   (isd-turtle-dump-ucs-ext-b-6 directory)
581   (isd-turtle-dump-ucs-ext-c directory)
582   (isd-turtle-dump-ucs-ext-d directory)
583   (isd-turtle-dump-ucs-ext-e directory)
584   (isd-turtle-dump-mj-0 directory)
585   (isd-turtle-dump-mj-1 directory)
586   (isd-turtle-dump-mj-2 directory)
587   (isd-turtle-dump-mj-3 directory)
588   (isd-turtle-dump-mj-4 directory)
589   (isd-turtle-dump-mj-5 directory)
590   (isd-turtle-dump-mj-6 directory)
591   )
592
593
594 ;;; @ End.
595 ;;;
596
597 (provide 'isd-turtle)
598
599 ;;; isd-turtle.el ends here