1 ;;; isd-turtle.el --- Utility to dump ideographic-structure as Turtle files
3 ;; Copyright (C) 2017 MORIOKA Tomohiko
5 ;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
6 ;; Keywords: Ideographic Structures (漢字構造、解字), IDS, CHISE, RDF, Turtle
8 ;; This file is a part of CHISE-ISD (Ideographic Structure Database).
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 (require 'cwiki-common)
29 (defvar isd-url-prefix "http://rdf.chise.org/data/")
31 (setq est-coded-charset-priority-list
42 =jis-x0208 =jis-x0208@1990
44 =jis-x0213-1@2000 =jis-x0213-1@2004
62 =cns11643-1 =cns11643-2 =cns11643-3
63 =cns11643-4 =cns11643-5 =cns11643-6 =cns11643-7
73 =zinbun-oracle =>zinbun-oracle
78 =+>ucs@iso =+>ucs@unicode
84 =>jis-x0208 =>jis-x0213-1
91 =>ucs@jis =>ucs@cns =>ucs@ks
94 =>>ucs@iso =>>ucs@unicode
95 =>>ucs@jis =>>ucs@cns =>>ucs@ks
113 ==cns11643-1 ==cns11643-2 ==cns11643-3
114 ==cns11643-4 ==cns11643-5 ==cns11643-6 ==cns11643-7
124 =>>jis-x0208 =>>jis-x0213-1 =>>jis-x0213-2
125 =+>jis-x0208 =+>jis-x0213-1 =+>jis-x0213-2
131 =jis-x0208@1983 =jis-x0208@1978
160 ;; (defvar isd-turtle-ccs-list nil)
161 (defvar chise-turtle-ccs-prefix-alist nil)
163 (defun charset-code-point-format-spec (ccs)
164 (cond ((memq ccs '(=ucs))
167 (let ((ccs-name (symbol-name ccs)))
170 "\\(shinjigen\\|daikanwa/ho\\|=>iwds-1\\)"
174 "\\(gt\\|daikanwa\\|adobe-japan1\\|cbeta\\|zinbun-oracle\\|hng\\)"
177 ((string-match "\\(hanyo-denshi/ks\\|koseki\\|mj\\)" ccs-name)
179 ((string-match "hanyo-denshi/tk" ccs-name)
184 ;; (defun isd-turtle-uri-encode-feature-name (feature-name)
186 ;; ((eq '=ucs feature-name)
188 ;; ((eq '==>ucs@bucs feature-name)
191 ;; (mapconcat (lambda (c)
194 ;; (char-to-string c)))
195 ;; (www-uri-encode-feature-name feature-name)
197 (defun chise-turtle-uri-encode-ccs-name (feature-name)
199 ((eq '=ucs feature-name)
201 ((eq '=big5 feature-name)
203 ((eq '==>ucs@bucs feature-name)
206 (mapconcat (lambda (c)
215 (char-to-string c))))
216 (www-uri-encode-feature-name feature-name)
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)))
223 ;; (isd-turtle-uri-encode-feature-name ccs)
224 ;; (format (charset-code-point-format-spec ccs)
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)))
234 (format (charset-code-point-format-spec ccs)
237 (defun isd-turtle-encode-char (object)
238 (let ((ccs-list est-coded-charset-priority-list)
240 (if (setq ret (encode-char object '=ucs))
241 (chise-turtle-format-ccs-code-point '=ucs ret)
243 (setq ccs (pop ccs-list))
244 (not (setq ret (encode-char object ccs 'defined-only)))))
246 (chise-turtle-format-ccs-code-point ccs ret)
248 ((and (setq ccs (car (split-char object)))
249 (setq ret (encode-char object ccs)))
250 (chise-turtle-format-ccs-code-point ccs ret)
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))
259 (defun isd-turtle-format-component (component separator level prefix)
260 (cond ((characterp component)
262 (isd-turtle-encode-char component)
267 (let ((ret (find-char component)))
270 (isd-turtle-encode-char ret) separator ret))
271 ((setq ret (assq 'ideographic-structure component))
272 (if (eq separator ?\;)
274 (isd-turtle-format-char nil nil (cdr ret) (1+ level)
276 (isd-turtle-format-char nil nil (cdr ret) (1+ level)
279 (defun isd-turtle-format-char (ccs code-point &optional ids-list level
280 prefix without-head-char)
285 (let ((indent (make-string (* level 4) ?\ ))
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)
300 (setq idc (plist-get idc :char)))
302 (setq ret (find-char idc)))
305 (setq ret (find-char c1)))
308 (setq ret (find-char c2)))
311 (setq ret (find-char c3)))
314 ((eq idc ?\u2FF0) ; ⿰
332 ((memq idc '(?⿴ ?⿵ ?⿶ ?⿷ ?⿸ ?⿹ ?⿺))
343 %s %s:structure [ a idc:%c ;
348 (if without-head-char
350 (if (and ccs code-point)
352 (chise-turtle-format-ccs-code-point ccs code-point)
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)
360 (if without-head-char
369 %s %s:structure [ a idc:%c ;
373 (if without-head-char
375 (if (and ccs code-point)
377 (chise-turtle-format-ccs-code-point ccs code-point)
381 indent prefix p1 (isd-turtle-format-component c1 ?\; (1+ level) prefix)
382 indent prefix p2 (isd-turtle-format-component c2 ?\ (1+ level) prefix)
384 (if without-head-char
392 (defun isd-turtle-insert-char (ccs code-point)
393 (let ((ret (isd-turtle-format-char ccs code-point)))
398 (defun isd-turtle-insert-ccs-ranges (ccs &rest ranges)
399 (let (range code max-code)
401 (setq range (car ranges))
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)))
410 (isd-turtle-insert-char ccs range)
412 (t (error 'wrong-type-argument range)))
413 (setq ranges (cdr ranges)))))
415 (defun isd-turtle-dump-range (file path func &rest args)
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)))
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
432 (char-attribute-name< (cdr a)(cdr b)))))
433 (insert (format "@prefix %s: <%s/%s=> .\n"
435 "http://www.chise.org/est/view/character"
436 (www-uri-encode-feature-name (cdr cell)))))
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))))
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)))
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))
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)))
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)))
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)))
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)))
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)))
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)))
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)))
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)))
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)))
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
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)))
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)))
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)))
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)))
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)))
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)))
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)
597 (provide 'isd-turtle)
599 ;;; isd-turtle.el ends here