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
33 =>jis-x0208 =>jis-x0213-1
41 =jis-x0208 =jis-x0208@1990
42 =jis-x0213-1@2000 =jis-x0213-1@2004
51 =cns11643-1 =cns11643-2 =cns11643-3
52 =cns11643-4 =cns11643-5 =cns11643-6 =cns11643-7
59 =+>ucs@iso =+>ucs@unicode
60 =>ucs@jis =>ucs@cns =>ucs@ks
61 =>>ucs@iso =>>ucs@unicode
62 =>>ucs@jis =>>ucs@cns =>>ucs@ks
64 =>>jis-x0208 =>>jis-x0213-1 =>>jis-x0213-2
65 =+>jis-x0208 =+>jis-x0213-1 =+>jis-x0213-2
70 =jis-x0208@1983 =jis-x0208@1978
76 =zinbun-oracle =>zinbun-oracle
84 (defvar isd-turtle-ccs-list nil)
86 (defun isd-turtle-uri-encode-feature-name (feature-name)
88 ((eq '=ucs feature-name)
90 ((eq '==>ucs@bucs feature-name)
93 (mapconcat (lambda (c)
97 (www-uri-encode-feature-name feature-name)
100 ;; (defun isd-turtle-encode-char (char)
101 ;; (let ((ucs (encode-char char '=ucs)))
103 ;; (format "ucs:0x%04X" ucs)
104 ;; (www-uri-encode-object char))))
106 (defun isd-turtle-encode-char (object)
107 (let ((ccs-list est-coded-charset-priority-list)
109 (if (setq ret (encode-char object '=ucs))
111 (format "a.ucs:0x%04X" ret)
112 (unless (memq '=ucs isd-turtle-ccs-list)
113 (setq isd-turtle-ccs-list (cons '=ucs isd-turtle-ccs-list))))
115 (setq ccs (pop ccs-list))
116 (not (setq ret (encode-char object ccs 'defined-only)))))
118 (unless (memq ccs isd-turtle-ccs-list)
119 (setq isd-turtle-ccs-list (cons ccs isd-turtle-ccs-list)))
120 (format (cond ((memq ccs '(=gt
121 =gt-k =daikanwa =adobe-japan1-6
122 =cbeta =zinbun-oracle))
124 ((memq ccs '(=hanyo-denshi/ks
130 (isd-turtle-uri-encode-feature-name ccs)
132 ((and (setq ccs (car (split-char object)))
133 (setq ret (encode-char object ccs)))
134 (unless (memq ccs isd-turtle-ccs-list)
135 (setq isd-turtle-ccs-list (cons ccs isd-turtle-ccs-list)))
137 (isd-turtle-uri-encode-feature-name ccs)
140 (format (if est-hide-cgi-mode
141 "system-char-id=0x%X"
142 "system-char-id:0x%X")
143 (encode-char object 'system-char-id))
146 (defun isd-turtle-format-component (component separator level)
147 (cond ((characterp component)
149 (isd-turtle-encode-char component)
154 (let ((ret (find-char component)))
157 (isd-turtle-encode-char ret) separator ret))
158 ((setq ret (assq 'ideographic-structure component))
159 (if (eq separator ?\;)
161 (isd-turtle-format-char nil (cdr ret) (1+ level)))
162 (isd-turtle-format-char nil (cdr ret) (1+ level)))))))))
164 (defun isd-turtle-format-char (char &optional ids-list level)
166 (setq ids-list (get-char-attribute char 'ideographic-structure)))
169 (let ((indent (make-string (* level 4) ?\ ))
172 (c1 (nth 1 ids-list))
173 (c2 (nth 2 ids-list))
174 (c3 (nth 3 ids-list))
177 (setq idc (plist-get idc :char)))
179 (setq ret (find-char idc)))
182 (setq ret (find-char c1)))
185 (setq ret (find-char c2)))
188 (setq ret (find-char c3)))
191 ((eq idc ?\u2FF0) ; ⿰
209 ((memq idc '(?⿴ ?⿵ ?⿶ ?⿷ ?⿸ ?⿹ ?⿺))
220 %s :structure [ a idc:%c ;
226 (isd-turtle-format-component char ?\ 0)
229 indent p1 (isd-turtle-format-component c1 ?\; (1+ level))
230 indent p2 (isd-turtle-format-component c2 ?\; (1+ level))
231 indent p3 (isd-turtle-format-component c3 ?\ (1+ level))
240 %s :structure [ a idc:%c ;
245 (isd-turtle-format-component char ?\ 0)
248 indent p1 (isd-turtle-format-component c1 ?\; (1+ level))
249 indent p2 (isd-turtle-format-component c2 ?\ (1+ level))
257 (defun isd-turtle-insert-char (char)
258 (let ((ret (isd-turtle-format-char char)))
263 (defun isd-turtle-insert-ccs-ranges (ccs &rest ranges)
264 (let (range code max-code char)
266 (setq range (car ranges))
268 (setq code (car range)
269 max-code (cdr range))
270 (while (<= code max-code)
271 (if (setq char (decode-char ccs code))
272 (isd-turtle-insert-char char))
273 (setq code (1+ code))))
275 (if (setq char (decode-char ccs code))
276 (isd-turtle-insert-char char)))
277 (t (error 'wrong-type-argument range)))
278 (setq ranges (cdr ranges)))))
280 (defun isd-turtle-dump-range (file path func &rest args)
282 (let ((coding-system-for-write 'utf-8-mcs-er)
284 (if (file-directory-p path)
285 (setq path (expand-file-name file path)))
287 (goto-char (point-min))
288 (dolist (ccs (sort isd-turtle-ccs-list
289 #'char-attribute-name<))
290 (insert (format "@prefix %s: <%s%s=> .\n"
291 (isd-turtle-uri-encode-feature-name ccs)
292 "http://www.chise.org/est/view/character/"
293 (www-uri-encode-feature-name ccs))))
295 (goto-char (point-min))
296 (insert "# -*- coding: utf-8-mcs-er -*-\n")
297 (insert "@prefix : <http://rdf.chise.org/rdf/property/character/isd/> .
298 @prefix idc: <http://rdf.chise.org/rdf/type/character/idc/> .\n")
299 (write-region (point-min)(point-max) path))))
302 (defun isd-turtle-dump-ucs-basic (filename)
303 (interactive "Fdump ISD-UCS-Basic : ")
304 (isd-turtle-dump-range "ISD-UCS-Basic.ttl" filename
305 #'isd-turtle-insert-ccs-ranges
306 'ucs '(#x4E00 . #x9FA5)))
309 (defun isd-turtle-dump-ucs-ext-a (filename)
310 (interactive "Fdump ISD-UCS-Ext-A : ")
311 (isd-turtle-dump-range "ISD-UCS-Ext-A.txt" filename
312 #'isd-turtle-insert-ccs-ranges
313 'ucs '(#x3400 . #x4DB5) #xFA1F #xFA23))
320 (provide 'isd-turtle)
322 ;;; isd-turtle.el ends here