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
36 =jis-x0208 =jis-x0208@1990
38 =jis-x0213-1@2000 =jis-x0213-1@2004
47 =cns11643-1 =cns11643-2 =cns11643-3
48 =cns11643-4 =cns11643-5 =cns11643-6 =cns11643-7
55 =zinbun-oracle =>zinbun-oracle
59 =>jis-x0208 =>jis-x0213-1
66 =+>ucs@iso =+>ucs@unicode
67 =>ucs@jis =>ucs@cns =>ucs@ks
68 =>>ucs@iso =>>ucs@unicode
69 =>>ucs@jis =>>ucs@cns =>>ucs@ks
71 =>>jis-x0208 =>>jis-x0213-1 =>>jis-x0213-2
72 =+>jis-x0208 =+>jis-x0213-1 =+>jis-x0213-2
77 =jis-x0208@1983 =jis-x0208@1978
85 (defvar isd-turtle-ccs-list nil)
87 (defun isd-turtle-uri-encode-feature-name (feature-name)
89 ((eq '=ucs feature-name)
91 ((eq '==>ucs@bucs feature-name)
94 (mapconcat (lambda (c)
98 (www-uri-encode-feature-name feature-name)
101 ;; (defun isd-turtle-encode-char (char)
102 ;; (let ((ucs (encode-char char '=ucs)))
104 ;; (format "ucs:0x%04X" ucs)
105 ;; (www-uri-encode-object char))))
107 (defun isd-turtle-encode-char (object)
108 (let ((ccs-list est-coded-charset-priority-list)
110 (if (setq ret (encode-char object '=ucs))
112 (format "a.ucs:0x%04X" ret)
113 (unless (memq '=ucs isd-turtle-ccs-list)
114 (setq isd-turtle-ccs-list (cons '=ucs isd-turtle-ccs-list))))
116 (setq ccs (pop ccs-list))
117 (not (setq ret (encode-char object ccs 'defined-only)))))
119 (unless (memq ccs isd-turtle-ccs-list)
120 (setq isd-turtle-ccs-list (cons ccs isd-turtle-ccs-list)))
121 (format (cond ((memq ccs '(=gt
122 =gt-k =daikanwa =adobe-japan1
123 =cbeta =zinbun-oracle))
125 ((memq ccs '(=hanyo-denshi/ks
131 (isd-turtle-uri-encode-feature-name ccs)
133 ((and (setq ccs (car (split-char object)))
134 (setq ret (encode-char object ccs)))
135 (unless (memq ccs isd-turtle-ccs-list)
136 (setq isd-turtle-ccs-list (cons ccs isd-turtle-ccs-list)))
138 (isd-turtle-uri-encode-feature-name ccs)
141 (format (if est-hide-cgi-mode
142 "system-char-id=0x%X"
143 "system-char-id:0x%X")
144 (encode-char object 'system-char-id))
147 (defun isd-turtle-format-component (component separator level)
148 (cond ((characterp component)
150 (isd-turtle-encode-char component)
155 (let ((ret (find-char component)))
158 (isd-turtle-encode-char ret) separator ret))
159 ((setq ret (assq 'ideographic-structure component))
160 (if (eq separator ?\;)
162 (isd-turtle-format-char nil (cdr ret) (1+ level)))
163 (isd-turtle-format-char nil (cdr ret) (1+ level)))))))))
165 (defun isd-turtle-format-char (char &optional ids-list level)
167 (setq ids-list (get-char-attribute char 'ideographic-structure)))
170 (let ((indent (make-string (* level 4) ?\ ))
173 (c1 (nth 1 ids-list))
174 (c2 (nth 2 ids-list))
175 (c3 (nth 3 ids-list))
178 (setq idc (plist-get idc :char)))
180 (setq ret (find-char idc)))
183 (setq ret (find-char c1)))
186 (setq ret (find-char c2)))
189 (setq ret (find-char c3)))
192 ((eq idc ?\u2FF0) ; ⿰
210 ((memq idc '(?⿴ ?⿵ ?⿶ ?⿷ ?⿸ ?⿹ ?⿺))
221 %s :structure [ a idc:%c ;
227 (isd-turtle-format-component char ?\ 0)
230 indent p1 (isd-turtle-format-component c1 ?\; (1+ level))
231 indent p2 (isd-turtle-format-component c2 ?\; (1+ level))
232 indent p3 (isd-turtle-format-component c3 ?\ (1+ level))
241 %s :structure [ a idc:%c ;
246 (isd-turtle-format-component char ?\ 0)
249 indent p1 (isd-turtle-format-component c1 ?\; (1+ level))
250 indent p2 (isd-turtle-format-component c2 ?\ (1+ level))
258 (defun isd-turtle-insert-char (char)
259 (let ((ret (isd-turtle-format-char char)))
264 (defun isd-turtle-insert-ccs-ranges (ccs &rest ranges)
265 (let (range code max-code char)
267 (setq range (car ranges))
269 (setq code (car range)
270 max-code (cdr range))
271 (while (<= code max-code)
272 (if (setq char (decode-char ccs code))
273 (isd-turtle-insert-char char))
274 (setq code (1+ code))))
276 (if (setq char (decode-char ccs code))
277 (isd-turtle-insert-char char)))
278 (t (error 'wrong-type-argument range)))
279 (setq ranges (cdr ranges)))))
281 (defun isd-turtle-dump-range (file path func &rest args)
283 (let ((coding-system-for-write 'utf-8-mcs-er)
285 (if (file-directory-p path)
286 (setq path (expand-file-name file path)))
288 (goto-char (point-min))
289 (dolist (ccs (sort isd-turtle-ccs-list
290 #'char-attribute-name<))
291 (insert (format "@prefix %s: <%s%s=> .\n"
292 (isd-turtle-uri-encode-feature-name ccs)
293 "http://www.chise.org/est/view/character/"
294 (www-uri-encode-feature-name ccs))))
296 (goto-char (point-min))
297 (insert "# -*- coding: utf-8-mcs-er -*-\n")
298 (insert "@prefix : <http://rdf.chise.org/rdf/property/character/isd/> .
299 @prefix idc: <http://rdf.chise.org/rdf/type/character/idc/> .\n")
300 (write-region (point-min)(point-max) path))))
303 (defun isd-turtle-dump-ucs-basic (filename)
304 (interactive "Fdump ISD-UCS-Basic : ")
305 (isd-turtle-dump-range "ISD-UCS-Basic.ttl" filename
306 #'isd-turtle-insert-ccs-ranges
307 'ucs '(#x4E00 . #x9FA5)))
310 (defun isd-turtle-dump-ucs-ext-a (filename)
311 (interactive "Fdump ISD-UCS-Ext-A : ")
312 (isd-turtle-dump-range "ISD-UCS-Ext-A.txt" filename
313 #'isd-turtle-insert-ccs-ranges
314 'ucs '(#x3400 . #x4DB5) #xFA1F #xFA23))
321 (provide 'isd-turtle)
323 ;;; isd-turtle.el ends here