chiset-common.el 12.4 KB
Newer Older
MORIOKA Tomohiko's avatar
MORIOKA Tomohiko committed
1
2
;;; chiset-common.el --- CHISET common utility -*- coding: utf-8-er; -*-

MORIOKA Tomohiko's avatar
MORIOKA Tomohiko committed
3
4
;; Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018,
;;   2019, 2021 MORIOKA Tomohiko.
MORIOKA Tomohiko's avatar
MORIOKA Tomohiko committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

;; Author: MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
;; Keywords: CHISE, RDF, Turtle, WWW

;; This file is part of CHISET (CHISE/Turtle).

;; XEmacs CHISE is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or (at
;; your option) any later version.

;; XEmacs CHISE is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with XEmacs CHISE; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Code:

(defun decode-uri-string (string &optional coding-system)
  (if (> (length string) 0)
      (let ((i 0)
	    dest)
	(setq string
	      (mapconcat (lambda (char)
			   (if (eq char ?+)
			       " "
			     (char-to-string char)))
			 string ""))
	(while (string-match "%\\([0-9A-F][0-9A-F]\\)" string i)
	  (setq dest (concat dest
			     (substring string i (match-beginning 0))
			     (char-to-string
			      (int-char
			       (string-to-int (match-string 1 string) 16))))
		i (match-end 0)))
	(decode-coding-string
	 (concat dest (substring string i))
	 coding-system))))


;;; @ URI representation
;;;

(defun est-uri-decode-feature-name-body (uri-feature)
  (let ((len (length uri-feature))
	(i 0)
	ch dest)
    (while (< i len)
      (setq dest
	    (concat
	     dest
	     (if (eq (aref uri-feature i) ?\.)
		 (if (and (< (+ i 2) len)
			  (eq (aref uri-feature (+ i 2)) ?\.))
		     (prog1
			 (cond
			  ((eq (setq ch (aref uri-feature (1+ i))) ?\.)
			   "/")
			  ((eq ch ?-)
			   "*")
70
71
			  ((eq ch ?_)
			   "+")
MORIOKA Tomohiko's avatar
MORIOKA Tomohiko committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
			  (t
			   (substring uri-feature i (+ i 3))
			   ))
		       (setq i (+ i 3)))
		   (setq i (1+ i))
		   ".")
	       (prog1
		   (char-to-string (aref uri-feature i))
		 (setq i (1+ i)))))))
    dest))

(defun est-uri-encode-feature-name-body (feature)
  (mapconcat (lambda (c)
	       (cond ((eq c ?*)
		      ".-.")
		     ((eq c ?/)
		      "...")
89
90
		     ((eq c ?+)
		      "._.")
MORIOKA Tomohiko's avatar
MORIOKA Tomohiko committed
91
92
93
		     (t (char-to-string c))))
	     feature ""))

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
(defun est-uri-decode-feature-name (uri-feature)
  (let (feature)
    (setq uri-feature (decode-uri-string uri-feature 'utf-8-mcs-er))
    (cond
     ((string-match "^from\\." uri-feature)
      (intern (format "<-%s"
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
     ((string-match "^to\\." uri-feature)
      (intern (format "->%s"
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
     ((string-match "^meta\\." uri-feature)
      (intern (format "*%s"
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
     ((string-match "^rep\\." uri-feature)
      (intern (format "=%s"
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
     ((string-match "^rep[2i]\\." uri-feature)
      (intern (format "===%s"
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
     ((string-match "^g\\." uri-feature)
      (intern (format "=>>%s"
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
     ((string-match "^g[i2]\\." uri-feature)
      (intern (format "==%s"
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
     ((string-match "^gi\\([0-9]+\\)\\." uri-feature)
      (intern (format "=>>%s%s"
		      (make-string (string-to-int
				    (match-string 1 uri-feature))
				   ?>)
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
     ((string-match "^o\\." uri-feature)
      (intern (format "=+>%s"
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
     ((string-match "^a\\." uri-feature)
      (intern (format "=>%s"
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
     ((string-match "^a\\([0-9]+\\)\\." uri-feature)
      (intern (format "%s>%s"
		      (make-string (string-to-int
				    (match-string 1 uri-feature))
				   ?=)
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
     (t
      (intern (est-uri-decode-feature-name-body uri-feature))
      ))))

MORIOKA Tomohiko's avatar
MORIOKA Tomohiko committed
163
164
165
166
167
168
169
170
171
172
173
174
175
176
(defun www-uri-decode-feature-name (uri-feature)
  (let (feature)
    (setq uri-feature (decode-uri-string uri-feature 'utf-8-mcs-er))
    (cond
     ((string-match "^from\\." uri-feature)
      (intern (format "<-%s"
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
     ((string-match "^to\\." uri-feature)
      (intern (format "->%s"
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
177
178
179
180
181
     ((string-match "^meta\\." uri-feature)
      (intern (format "*%s"
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
MORIOKA Tomohiko's avatar
MORIOKA Tomohiko committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
     ((string-match "^rep\\." uri-feature)
      (intern (format "=%s"
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
     ((string-match "^rep[2i]\\." uri-feature)
      (intern (format "===%s"
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
     ((string-match "^g\\." uri-feature)
      (intern (format "=>>%s"
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
     ((string-match "^g[i2]\\." uri-feature)
      (intern (format "==%s"
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
     ((string-match "^gi\\([0-9]+\\)\\." uri-feature)
      (intern (format "=>>%s%s"
		      (make-string (string-to-int
				    (match-string 1 uri-feature))
				   ?>)
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
     ((string-match "^o\\." uri-feature)
      (intern (format "=+>%s"
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
     ((string-match "^a\\." uri-feature)
      (intern (format "=>%s"
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
     ((string-match "^a\\([0-9]+\\)\\." uri-feature)
      (intern (format "%s>%s"
		      (make-string (string-to-int
				    (match-string 1 uri-feature))
				   ?=)
		      (est-uri-decode-feature-name-body
		       (substring uri-feature (match-end 0)))))
      )
     ((and (setq uri-feature (est-uri-decode-feature-name-body uri-feature))
	   (setq feature (intern (format "=>%s" uri-feature)))
	   (find-charset feature))
      feature)
     ((and (setq feature (intern (format "=>>%s" uri-feature)))
	   (find-charset feature))
      feature)
     ((and (setq feature (intern (format "=>>>%s" uri-feature)))
	   (find-charset feature))
      feature)
     ((and (setq feature (intern (format "=%s" uri-feature)))
	   (find-charset feature))
      feature)
     (t (intern uri-feature)))))

(defun www-uri-encode-feature-name (feature-name)
244
  (setq feature-name (format "%s" feature-name))
MORIOKA Tomohiko's avatar
MORIOKA Tomohiko committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
  (cond
   ((string-match "^=\\+>\\([^=>]+\\)" feature-name)
    (concat "o."
	    (est-uri-encode-feature-name-body
	     (substring feature-name (match-beginning 1))))
    )
   ((string-match "^=\\([^=>]+\\)" feature-name)
    (concat "rep."
	    (est-uri-encode-feature-name-body
	     (substring feature-name (match-beginning 1))))
    )
   ((string-match "^==\\([^=>]+\\)" feature-name)
    (concat "g2."
	    (est-uri-encode-feature-name-body
	     (substring feature-name (match-beginning 1))))
    )
   ((string-match "^===\\([^=>]+\\)" feature-name)
    (concat "repi."
	    (est-uri-encode-feature-name-body
	     (substring feature-name (match-beginning 1))))
    )
   ((string-match "^=>>\\([^=>]+\\)" feature-name)
    (concat "g."
	    (est-uri-encode-feature-name-body
	     (substring feature-name (match-beginning 1))))
    )
   ((string-match "^=>>>\\([^=>]+\\)" feature-name)
    (concat "gi."
	    (est-uri-encode-feature-name-body
	     (substring feature-name (match-beginning 1))))
    )
   ((string-match "^=>>\\(>+\\)" feature-name)
    (format "gi%d.%s"
	    (length (match-string 1 feature-name))
	    (est-uri-encode-feature-name-body
	     (substring feature-name (match-end 1))))
    )
   ((string-match "^=>\\([^=>]+\\)" feature-name)
    (concat "a."
	    (est-uri-encode-feature-name-body
	     (substring feature-name (match-beginning 1))))
    )
   ((string-match "^\\(=+\\)>" feature-name)
    (format "a%d.%s"
	    (length (match-string 1 feature-name))
	    (est-uri-encode-feature-name-body
	     (substring feature-name (match-end 0))))
    )
   ((string-match "^->" feature-name)
    (concat "to."
	    (est-uri-encode-feature-name-body
	     (substring feature-name (match-end 0))))
    )
   ((string-match "^<-" feature-name)
    (concat "from."
	    (est-uri-encode-feature-name-body
	     (substring feature-name (match-end 0))))
    )
303
304
305
306
307
   ((string-match "^\\*" feature-name)
    (concat "meta."
	    (est-uri-encode-feature-name-body
	     (substring feature-name (match-end 0))))
    )
MORIOKA Tomohiko's avatar
MORIOKA Tomohiko committed
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
   (t (est-uri-encode-feature-name-body feature-name))))


(defvar chise-turtle-ccs-prefix-alist nil)

(defun chise-turtle-uri-decode-feature-name (uri-feature)
  (cond ((string= "a.ucs" uri-feature)
	 '=ucs)
	((string= "a.big5" uri-feature)
	 '=big5)
	(t
	 (www-uri-decode-feature-name uri-feature))))

(defun chise-turtle-uri-encode-ccs-name (feature-name)
  (cond
   ((eq '=ucs feature-name)
    "a.ucs")
   ((eq '=big5 feature-name)
    "a.big5")
   ((eq '==>ucs@bucs feature-name)
    "bucs")
   (t
    (mapconcat (lambda (c)
		 (cond
		  ((eq c ?@)
		   "_")
		  ((eq c ?+)
		   "._.")
		  ((eq c ?=)
		   ".:.")
		  ((eq c ?|)
		   "._cmp_.")
		  (t
		   (char-to-string c))))
	       (www-uri-encode-feature-name feature-name)
	       ""))))

(defun charset-code-point-format-spec (ccs)
  (cond ((memq ccs '(=ucs))
	 "0x%04X")
	(t
	 (let ((ccs-name (symbol-name ccs)))
	   (cond
	    ((string-match
	      "\\(shinjigen\\|daikanwa/ho\\|=>iwds-1\\)"
	      ccs-name)
	     "%04d")
	    ((string-match
	      "\\(gt\\|daikanwa\\|adobe-japan1\\|cbeta\\|zinbun-oracle\\|hng\\)"
	      ccs-name)
	     "%05d")
	    ((string-match "\\(hanyo-denshi/ks\\|koseki\\|mj\\)" ccs-name)
	     "%06d")
	    ((string-match "hanyo-denshi/tk" ccs-name)
	     "%08d")
	    (t
	     "0x%X"))))))

(defun chise-turtle-format-ccs-code-point (ccs code-point)
  (let ((ccs-uri (chise-turtle-uri-encode-ccs-name ccs)))
    (unless (assoc ccs-uri chise-turtle-ccs-prefix-alist)
      (setq chise-turtle-ccs-prefix-alist
	    (cons (cons ccs-uri ccs)
		  chise-turtle-ccs-prefix-alist)))
    (format "%s:%s"
	    ccs-uri
	    (format (charset-code-point-format-spec ccs)
		    code-point))))

(defun chise-turtle-encode-char (object)
  (let (spec cell dest
	ccs ret ret2)
    (if (setq ret (encode-char object '=ucs))
	(chise-turtle-format-ccs-code-point '=ucs ret)
      (setq spec (char-attribute-alist object))
      (while (and spec
		  (setq cell (pop spec)))
	(if (and (find-charset (car cell))
		 (setq ret (cdr cell)))
	    (setq dest (cons cell dest))))
      (setq ret (car (sort dest (lambda (a b)
				  (char-attribute-name< (car a)(car b)))))
	    ccs (car ret)
	    ret (cdr ret))
      (cond (ret
	     (chise-turtle-format-ccs-code-point ccs ret)
	     )
	    ((and (setq ccs (car (split-char object)))
		  (setq ret (encode-char object ccs)))
	     (chise-turtle-format-ccs-code-point ccs ret)
	     )
	    ((setq ret (get-char-attribute object 'ideographic-combination))
	     (format "ideocomb:%s"
		     (mapconcat (lambda (cell)
				  (cond ((characterp cell)
					 (char-to-string cell)
					 )
					((setq ret2 (find-char cell))
					 (char-to-string ret2)
					 )
					(t
					 (format "%S" cell)
					 )))
				ret ""))
	     )
	    (t
	     (format "system-char-id:0x%X"
		     (encode-char object 'system-char-id))
	     )))))


;;; @ end
;;;

(provide 'chiset-common)

;;; chiset-common.el ends here