File: skk-xml.el

package info (click to toggle)
skktools 1.3.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,380 kB
  • ctags: 349
  • sloc: sh: 11,551; ansic: 1,300; ruby: 1,049; perl: 798; lisp: 431; python: 241; makefile: 126; awk: 94; cpp: 73; sed: 1
file content (312 lines) | stat: -rw-r--r-- 11,335 bytes parent folder | download | duplicates (6)
1
2
3
4
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
244
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
303
304
305
306
307
308
309
310
311
312
;;; skk-xml.el --- SKK dictionary XML conversion tool
;; Copyright (C) 2002 NAKAJIMA Mikio <minakaji@osaka.email.ne.jp>

;; Author: NAKAJIMA Mikio <minakaji@osaka.email.ne.jp>
;; Version: $Id: skk-xml.el,v 1.3 2006/01/04 10:35:06 skk-cvs Exp $
;; Keywords: japanese
;; Created: Aug. 15, 2002
;; Last Modified: $Date: 2006/01/04 10:35:06 $

;; This file is part of Daredevil SKK.

;; Daredevil SKK 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.

;; Daredevil SKK 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 Daredevil SKK, see the file COPYING.  If not, write to
;; the Free Software Foundation Inc., 51 Franklin St, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

(defun skk-convert-jisyo-to-xml (file &optional reference reference-name)
  (interactive "fFile: ")
  (let ((working-buffer
	 (let ((temp (get-buffer-create " *skk-xml-temp*")))
	   (save-excursion
	     (set-buffer temp)
	     (erase-buffer)
	     (insert-file-contents (expand-file-name file))
	     temp)))
	(output-buffer
	 (let ((temp (get-buffer-create "*skk-xml-dictionary*")))
	   (save-excursion
	     (set-buffer temp)
	     (erase-buffer)
	     (insert "<?xml version=\"1.0\" encoding=\"EUC-JP\" standalone=\"yes\"?>\n")
	     (insert "<skk-dictionary>\n  <okuri-ari>\n")
	     temp)))
	(reference-buffer
	 (if reference
	     (let ((temp (get-buffer-create " *skk-xml-reference*")))
	       (save-excursion
		 (set-buffer temp)
		 (erase-buffer)
		 (insert-file-contents (expand-file-name reference))
		 temp))))
	okuri-ari okuri-nasi)
    (if (and reference-buffer (not reference-name))
	(progn
	  (setq reference-name (file-name-nondirectory reference))
	  (if (string-match "^SKK-JISYO\\.\\(.+\\)" reference-name)
	      (setq reference-name (match-string 1 reference-name)))))
    (unwind-protect
	(save-excursion
	  (if reference-buffer
	      (progn
		(set-buffer reference-buffer)
		(goto-char (point-min))
		(if (not (search-forward ";; okuri-ari entries." nil t nil))
		    (error "This file is not SKK dictionary format!")
		  (setq okuri-ari (point)))
		(if (not (search-forward ";; okuri-nasi entries." nil t nil))
		    (error "This file is not SKK dictionary format!")
		  (setq okuri-nasi (point)))))
	  (set-buffer working-buffer)
	  (goto-char (point-min))
	  (or (search-forward ";; okuri-nasi entries." nil t nil)
	      (error "This file is not SKK dictionary format!"))
	  (goto-char (point-min))
	  (or (search-forward ";; okuri-ari entries." nil t nil)
	      (error "This file is not SKK dictionary format!"))
	  (skk-convert-jisyo-to-xml-1
	   output-buffer reference-buffer reference-name okuri-ari okuri-nasi))
      (if reference
	(progn
	  (kill-buffer working-buffer)
	  (kill-buffer reference-buffer))))
    (pop-to-buffer output-buffer)))

(defun skk-convert-jisyo-to-xml-1
  (output-buffer reference-buffer reference-name okuri-ari okuri-nasi)
  ;;to be called in WORKING-BUFFER (original SKK-JISYO format buffer)
  (let ((max (point-max))
	(min (point-min))
	okuri-nasi-flag)
    (while (not (eobp))
      (forward-line 1)
      (message "Converting SKK dictionary to XML format...%3d%% done"
	       (if (fboundp 'float)
		   (round (* (/ (float (- (point) min)) max) 100))
		 ;; maybe Emacs integer will be overflowed when file has too many lines.
		 (/ (* 100 (- (point) min)) max)))
      (cond
       ;; next we are processing okuri-nasi entries.
       ((and (not okuri-nasi-flag) (looking-at ";; okuri-nasi entries."))
	(save-excursion
	  (set-buffer output-buffer)
	  (goto-char (point-max))
	  (insert "  </okuri-ari>\n  <okuri-nasi>\n")
	  (setq okuri-nasi-flag t)))
       ;; skip comments
       ((looking-at ";"))
       ;; final line
       ((eobp))
       (t
	(skk-convert-jisyo-to-xml-2
	 output-buffer reference-buffer reference-name
	 okuri-ari okuri-nasi okuri-nasi-flag))))
    (save-excursion
      (set-buffer output-buffer)
      (goto-char (point-max))
      (insert "  </okuri-nasi>\n</skk-dictionary>\n"))))

(defun skk-convert-jisyo-to-xml-2
  (output-buffer reference-buffer reference-name okuri-ari okuri-nasi okuri-nasi-flag)
  ;;to be called in WORKING-BUFFER (original SKK-JISYO format buffer)
  (let (midasi candidates temp-candidates okurigana word)
    (beginning-of-line)
    (setq midasi (buffer-substring-no-properties
		  (point) (1- (search-forward " "))))
    (setq midasi (skk-xml-substitute-special midasi))
    (search-forward "/")
    (setq temp-candidates (buffer-substring-no-properties
			   (point) (progn (end-of-line) (1- (point)))))
    (if (string-match "/\\(\\[[��-��]/.+\\)" temp-candidates)
	(progn
	  (setq temp-candidates (match-string 1 temp-candidates))
	  (or (eq (aref temp-candidates (1- (length temp-candidates)))
		  ?\])
	      (setq temp-candidates (concat temp-candidates "]")))))
    (setq candidates (split-string temp-candidates "/"))
    (save-excursion
      (set-buffer output-buffer)
      (goto-char (point-max))
      (insert "    <entry>\n    <key>" midasi "</key>\n")
      (while (setq word (car candidates))
	(catch 'exit
	  (let (annotation)
	    (if (string-equal word "]")
		(progn
		  (setq okurigana nil)
		  (throw 'exit nil)))
	    (if (and (not okurigana)
		     (string-match "\\[\\([��-��]\\)" word))
		(progn
		  (setq okurigana (match-string 1 word))
		  (throw 'exit nil)))
	    (if (string-match "\\([^;]+\\);\\([^;]+\\)" word)
		(setq annotation (skk-xml-substitute-special
				  (match-string 2 word))
		      word (skk-xml-substitute-special
			    (match-string 1 word)))
	      (setq word (skk-xml-substitute-special word)))
	    (insert "      <candidate>\n")
	    (insert "        <word>" word "</word>\n")
	    (if okurigana
		(insert "          <okurigana>" okurigana "</okurigana>\n"))
	    (if annotation
		(insert "          <annotation>" annotation "</annotation>\n"))
	    (if reference-buffer
		(let (pos max found)
		  (save-excursion
		    (set-buffer reference-buffer)
		    (goto-char (if okuri-nasi-flag okuri-nasi okuri-ari))
		    (search-forward (concat "\n" midasi " ")
				    (if okuri-nasi-flag nil okuri-nasi)
				    t nil)
		    (setq pos (point))
		    (end-of-line)
		    (setq max (point))
		    (goto-char pos)
		    (setq found (search-forward (concat "/" word "/") max t nil)))
		  (if found
		      (insert "          <" reference-name "/>\n")))))
	  (insert "      </candidate>\n"))
	(setq candidates (cdr candidates)))
      (insert "    </entry>\n"))))

(defun skk-xml-substitute-special (string)
  (let ((list (append string nil))
	c val)
    (while (setq c (car list))
      (cond ((eq c ?&)
	     (setq val (append '(?\; ?p ?m ?a ?&) val)))
	    ((eq c ?<)
	     (setq val (append '(?\; ?t ?l ?&) val)))
	    ((eq c ?>)
	     (setq val (append '(?\; ?t ?g ?&) val)))
	    ((eq c ?')
	     (setq val (append '(?\; ?s ?o ?p ?a ?&) val)))
	    ((eq c ?\")
	     (setq val (append '(?\; ?t ?o ?u ?q ?&) val)))
	    (t (setq val (cons c val))))
      (setq list (cdr list)))
    (mapconcat 'char-to-string (nreverse val) "")))

(defun skk-xml-make-jisyo-reference (xml skk-jisyo)
  (let* ((output-buffer
	  (let ((temp (get-buffer-create "*skk-xml-dictionary*"))
		(comment (concat "<!-- " (file-name-nondirectory xml) " -->")))
	    (save-excursion
	      (set-buffer temp)
	      (erase-buffer)
	      (insert-file-contents (expand-file-name xml))
	      (goto-char (point-min))
	      (or (re-search-forward (concat "^" comment "$") nil t nil)
		  (progn
		    (search-forward "?>\n" nil t nil)
		    (insert comment "\n")))
	      temp)))
	 (reference-buffer
	  (let ((temp (get-buffer-create " *skk-xml-reference*")))
	    (save-excursion
	      (set-buffer temp)
	      (erase-buffer)
	      (insert-file-contents (expand-file-name skk-jisyo))
	      temp)))
	 (reference-tag
	  (if (string-match "^SKK-JISYO\\.\\(.+\\)"
			    (file-name-nondirectory skk-jisyo))
	      (concat "<"
		      (match-string 1 (file-name-nondirectory skk-jisyo))
		      "/>\n")))
	 max min)
    (unwind-protect
	(save-excursion
	  (set-buffer reference-buffer)
	  (goto-char (point-min))
	  (or (search-forward ";; okuri-nasi entries." nil t nil)
	      (error "This file is not SKK dictionary format!"))
	  (goto-char (point-min))
	  (or (search-forward ";; okuri-ari entries." nil t nil)
	      (error "This file is not SKK dictionary format!"))
	  (setq max (point-max)
		min (point-min))
	  (while (not (eobp))
	    (forward-line 1)
	    (message "Making reference tags to %s...%3d%% done"
		     (file-name-nondirectory skk-jisyo)
		     (if (fboundp 'float)
			 (round (* (/ (float (- (point) min)) max) 100))
		       (/ (* 100 (- (point) min)) max)))
	    (cond
	     ;; skip comments
	     ((looking-at ";"))
	     ;; final line
	     ((eobp))
	     (t
	      (skk-xml-make-jisyo-reference-1 output-buffer reference-tag)
	      ))))
      (kill-buffer reference-buffer))
    (pop-to-buffer output-buffer)))

(defun skk-xml-make-jisyo-reference-1 (output-buffer reference-tag)
  (let (midasi temp-candidates candidates word tags
	       annotation pos max found r0 r1)
    (setq midasi (buffer-substring-no-properties
		  (point) (1- (search-forward " "))))
    (setq midasi (skk-xml-substitute-special midasi))
    (search-forward "/")
    (setq temp-candidates (buffer-substring-no-properties
			   (point) (progn (end-of-line) (1- (point)))))
    (if (string-match "/\\([^[]+\\)\\[[��-��]/.+" temp-candidates)
	(setq temp-candidates (match-string 1 temp-candidates)))
    (setq candidates (split-string temp-candidates "/"))
    (save-excursion
      (set-buffer output-buffer)
      (while (setq word (car candidates))
	(if (string-match "\\([^;]+\\);[^;]+" word)
	    (setq word (match-string 1 word)))
	(setq word (skk-xml-substitute-special word))
	(goto-char (point-min))
	(search-forward (concat "<key>" midasi "</key>") nil t nil)
	(setq pos (point))
	(or (search-forward "</entry>" nil t nil)
	    (error "Missing closing tag </entry>!"))
	(setq max (point))
	(goto-char pos)
	(setq found (search-forward (concat "<word>" word "</word>") max t nil))
	(if (not found)
	    nil
	  (forward-char 1)
	  (setq r0 (point))
	  (or (search-forward "</candidate>" nil t nil)
	      (error "Missing closing tag </canidate>!"))
	  (beginning-of-line)
	  (setq r1 (point))
	  (setq tags (buffer-substring-no-properties r0 r1))
	  (if (string-match "\\(<annotation>[^<>]+</annotation>\\)" tags)
	      (setq annotation (match-string 1 tags)
		    tags (concat (substring tags 0 (match-beginning 0))
				 (substring tags (match-end 0)))))
	  (if (string-match "\\([-_<>/a-zA-Z0-9]+\\)" tags)
	      (setq tags (match-string 1 tags))
	    (setq tags ""))
	  (or (string-match reference-tag tags)
	      (setq tags (concat tags reference-tag)))
	  (delete-region r0 r1)
	  (goto-char r0)
	  (if annotation
	      (insert "          " annotation "\n"))
	  (insert "          " tags))
	(setq candidates (cdr candidates))))))

;; end of skk-xml.el