OSDN Git Service

Refactoring bd2tex using tex_commands
[dianzhuhui/script.git] / sw2bd.rb
1 # 標点ファイルの生成
2 #  巻別に標点を付けるためのファイルを生成する
3 #  出力はYAML
4 #  History
5 #    21 July 11: part_wordnum, volume_wordnumの追加
6
7 # require 'yaml'
8 require 'rexml/parsers/sax2parser'
9 require 'rexml/sax2listener'
10
11 SOURCE_DIR = File.join(File.dirname(__FILE__), '..', 'data')
12 TARGET_DIR = File.join(File.dirname(__FILE__), '..', 'yml', 'swjz')
13 SWJZ = File.new(File.join(SOURCE_DIR, 'swjz.xml'))
14
15 class SwjzCollector
16   include REXML::SAX2Listener
17   def initialize
18     @out = nil
19     @doc_count = 0  # yaml documents in a file
20     @sw_count = 0
21     @word_count = 0
22
23     # states
24     @in_chapter = false
25     @in_chaptertitle = false
26     @in_shuowen = false
27     @in_wordhead = false
28     @in_part_wordnum = false
29     @in_volume_wordnum = false
30
31     # stacks
32     @wordhead = ''
33     @wordhead_id = nil
34     @position = nil
35     @chaptertitle = ''
36     @explanation = ''
37     @duan_note = ''
38     @wordnum = ''
39   end
40   def start_element(uri, localname, qname, attributes)
41     before_chapter(qname)
42     before_chaptertitle(qname, attributes)
43     before_shuowen(qname)
44     before_wordhead(qname, attributes)
45     before_explanation(qname)
46     before_duan_note(qname)
47     before_part_wordnum(qname)
48     before_volume_wordnum(qname)
49   end
50   def end_element(uri, localname, qname)
51     after_chapter(qname)
52     after_chaptertitle(qname)
53     after_shuowen(qname)
54     after_wordhead(qname)
55     after_explanation(qname)
56     after_duan_note(qname)
57     after_part_wordnum(qname)
58     after_volume_wordnum(qname)
59   end
60   def characters(text)
61     text.strip!
62     push_chaptertitle(text)
63     push_wordhead(text)
64     push_explanation(text)
65     push_duan_note(text)
66     push_wordnum(text)
67   end
68   def end_document
69     printf("\nSW: %s\n", @sw_count)
70     printf("Word: %s\n", @word_count)
71   end
72
73   #--- Process elements ---
74   # chapter
75   def before_chapter(qname)
76     if qname == 'chapter' then
77       @in_chapter = true
78       @doc_count = 0
79     end
80   end
81   def after_chapter(qname)
82     @in_chapter = false if qname == 'chapter'
83   end
84   # chaptertitle
85   def before_chaptertitle(qname, attributes)
86     if qname == 'chaptertitle' and @in_chapter then
87       @in_chaptertitle = true
88       chapter_id = attributes['id']
89 #      printf("chapter_id: %s\n", chapter_id)
90       setup_outfile(chapter_id)
91     end
92   end
93   def after_chaptertitle(qname)
94     if qname == 'chaptertitle' and @in_chapter then
95       @out.printf("chapter: %s\n", @chaptertitle + @duan_note)
96       @chaptertitle = ''
97       @in_chaptertitle = false
98     end
99   end
100   def push_chaptertitle(text)
101     @chaptertitle += text if @in_chaptertitle
102   end
103   # shuowen
104   def before_shuowen(qname)
105     if qname == 'shuowen'
106       @in_shuowen = true
107       @sw_count += 1
108       @doc_count += 1
109       @out.printf("\n--- #%i\ncontent:\n", @doc_count)  # sequence of sw's
110     end
111   end
112   def after_shuowen(qname)
113     @in_shuowen = false if qname == 'shuowen'
114   end
115   # wordhead
116   def before_wordhead(qname, attributes)
117     if qname == 'wordhead' then
118       @in_wordhead = true 
119       @wordhead_id = attributes['id']
120       @position = attributes['img']
121     end
122   end
123   def after_wordhead(qname)
124     if qname == 'wordhead' then
125       @out.printf("  - word: %s\n", @wordhead)
126       @out.printf("    position: %s\n", @position) if @position
127       if @wordhead_id then
128         @out.printf("    id: %s\n", @wordhead_id) 
129         @word_count += 1
130       end
131       @out.printf("    content:\n")
132       # Reset flags
133       @in_wordhead = false
134       @position = ''
135       @wordhead = ''
136       @wordhead_id = nil
137     end
138   end
139   def push_wordhead(text)
140     @wordhead += text if @in_wordhead
141   end
142   # explanation
143   def before_explanation(qname)
144     @in_explanation = true if qname == 'explanation'
145   end
146   def after_explanation(qname)
147     if qname == 'explanation'
148       if @in_shuowen then
149         @out.printf("      - ex: %s\n", @explanation)
150       end
151       @in_explanation = false
152       @explanation = ''
153     end
154   end
155   def push_explanation(text)
156     @explanation += text if @in_explanation
157   end
158   # duan_note
159   def before_duan_note(qname)
160     if qname == 'duan_note' then
161       @in_duan_note = true
162       after_wordnum
163     end
164   end
165   def after_duan_note(qname)
166     if qname == 'duan_note' then
167       if @in_shuowen then
168         @out.printf("      - dn: %s\n", @duan_note)
169       elsif @in_part_wordnum or @in_volume_wordnum then
170         @out.printf("  - dn: %s\n", @duan_note)
171       end
172       @duan_note = ''
173       @in_duan_note = false
174     end
175   end
176   def push_duan_note(text)
177     @duan_note += text if @in_duan_note
178   end
179   # part_wordnum
180   def before_part_wordnum(qname)
181     if qname == 'part_wordnum' then
182       @in_part_wordnum = true
183       before_wordnum
184       @doc_count += 1
185       @out.printf("\n--- #%i\npart:\n", @doc_count)  # sequence number
186     end
187   end
188   def after_part_wordnum(qname)
189     if qname == 'part_wordnum' then
190       @in_part_wordnum = false
191       after_wordnum
192     end
193   end
194   # volume_wordnum
195   def before_volume_wordnum(qname)
196     if qname == 'volume_wordnum' then
197       @in_volume_wordnum = true
198       before_wordnum
199       @doc_count += 1
200       @out.printf("\n--- #%i\nvolume:\n", @doc_count)  # sequence number
201     end
202   end
203   def after_volume_wordnum(qname)
204     if qname == 'volume_wordnum' then
205       @in_volume_wordnum = false
206       after_wordnum
207     end
208   end
209
210   #--- virtual element processing: wordnum ---
211   def before_wordnum
212     @in_wordnum = true
213   end
214   def after_wordnum
215     @out.printf("  - ex: %s\n", @wordnum) unless @wordnum.empty?
216     @in_wordnum = false
217     @wordnum = ''
218   end
219   def push_wordnum(text)
220     @wordnum += text if @in_wordnum
221   end
222
223   #--- utilities ---
224   def setup_outfile(chapter_id)
225     @out = File.open(File.join(TARGET_DIR, chapter_id + '.yml'), "w")
226   end
227 end
228
229 parser = REXML::Parsers::SAX2Parser.new SWJZ
230 listener = SwjzCollector.new
231 parser.listen(listener)
232 parser.parse