1 require 'rexml/document'
3 LINE_REGEX = /w[0-9ab]{4}([0-9]{2})([0-9]{2})/
4 SISHENG = {'sp' => '上平聲', 'xp' => '下平聲', 's' => '上聲', 'q' => '去聲', 'r' => '入聲'}
6 class SbgyController < ApplicationController
14 # (ucs, char) = process_char(c)
16 # wordheads = Wordhead.find(:all, :conditions => ["word = ?", char])
17 # wordheads.each do |wordhead|
18 # voice = Voice.find(:first, :conditions => ["id = ?", wordhead.voice_id])
19 # rhyme = Rhyme.find(:first, :conditions => ["id = ?", voice.rhyme_id])
20 # @chars.push([wordhead, voice, rhyme])
24 @volumes = Volume.find(:all)
26 @volumes.each do |volume|
27 rhymes = Rhyme.find(:all, :conditions => ["volume_id = ?", volume.id])
32 rhyme_id = params[:id]
33 @rhyme = Rhyme.find(:first, :conditions => ["id = ?", rhyme_id])
34 @volume = Volume.find(:first, :conditions => ["id = ?", @rhyme.volume_id])
35 @voices = Voice.find(:all, :conditions => ["rhyme_id = ?", rhyme_id])
40 @piece = params[:piece]
41 @plane = params[:plane]
42 return nil unless @piece
43 return nil if @piece.empty?
44 @regex = wordid_regex(@vol, @piece, @plane)
45 wordheads = Wordhead.find(:all, :conditions => ["xmlid like ?", @regex])
46 @chars = get_matrix(wordheads)
47 (@previous_piece, @previous_plane) = previous_page(@piece, @plane)
48 (@next_piece, @next_plane) = next_page(@piece, @plane)
51 fanqie = params[:fanqie]
52 # return nil unless id
54 f = format("<fanqie>%s</fanqie>", fanqie)
55 @voice = Voice.find(:first, :conditions => ["fanqie = ?", f])
58 @voice = Voice.find(:first, :conditions => ["id = ?", id])
59 # elsif not(xiaoyun.empty?) then
60 # @voice = Voice.find(:first, :conditions => ["name = ?", xiaoyun])
62 return nil unless @voice
63 @rhyme = Rhyme.find(:first, :conditions => ["id = ?", @voice.rhyme_id])
64 @volume = Volume.find(:first, :conditions => ["id = ?", @rhyme.volume_id])
66 Wordhead.find(:all, :conditions => ["voice_id = ?", @voice.id])
78 def previous_page(piece, plane)
79 return [nil, nil] if piece == "0" and plane == "a"
81 return [(piece.to_i - 1).to_s, "b"]
86 def next_page(piece, plane)
88 return [(piece.to_i + 1).to_s, "a"]
93 def get_matrix(wordheads)
94 page = get_empty_page(LINES_PER_PAGE)
95 wordheads.each do |wordhead|
96 wordhead.xmlid =~ LINE_REGEX
99 push_to_page(page, line, num, wordhead)
103 def get_empty_page(lines_nums)
105 (0..(lines_nums - 1)).each do |line|
106 page[line] = Array.new
110 def push_to_page(page, line, num, wordhead)
113 page[i][j] = wordhead
116 wordheads = Wordhead.find(:all)
117 wordheads.each do |wordhead|
118 word = wordhead.word.to_s
119 wordhead.name = get_rewrite_word(word)
125 voices = Voice.find(:all)
126 voices.each do |voice|
127 word = Wordhead.find(:first, :conditions => ["id = ?", voice.wordhead_id])
128 voice.name = word.word
130 note = "<n>" + word.note.to_s + "</n>"
131 doc = REXML::Document.new note
132 # fanqie = doc.elements.to_a("//fanqie")[0].text
133 doc.root.each_child do |child|
134 if child.node_type == :element and child.name == "fanqie" then
136 voice.fanqie = get_rewrite_text(fanqie)
144 rhymes = Rhyme.find(:all)
145 rhymes.each do |rhyme|
146 voice = Voice.find(:first, :conditions => ["id = ?", rhyme.voice_id])
147 sisheng = get_sisheng(rhyme.xmlid)
148 # rhyme.name = sisheng + rhyme.num + voice.name 変更 <24 Mar>
149 rhyme.name = rhyme.num + voice.name
155 volumes = Volume.find(:all)
156 volumes.each do |volume|
157 if volume.title =~ /^廣韻(.+聲)/u then
163 def get_sisheng(xmlid) # Rhyme
164 xmlid =~ /([spxqr]+)[0-9]+/
166 return SISHENG[sisheng_id]
168 # TODO: Rewrite a child node
169 def get_rewrite_word(word)
170 string = "<w>" + word + "</w>"
171 doc = REXML::Document.new string
172 original = doc.elements.to_a("//original_word")
173 if original.size > 0 then
174 rewrite = original[0].elements.to_a("//rewrite_word")
175 if rewrite.size > 0 then
176 name = rewrite[0].text
178 name = original[0].text
185 def get_rewrite_fanqie(fanqie)
187 fanqie.each_child do |child|
188 if child.node_type == :text then
190 elsif child.name == "original_word" then
192 child.elements.each("rewrite_word") do |rewrite|
193 ftext += rewrite.text
201 def get_rewrite_text(text)
202 return "" unless text
203 string = "<t>" + text + "</t>"
204 doc = REXML::Document.new string
205 original = doc.elements.to_a("//original_text")
206 if original and original.size > 0 then
207 rtext = doc.elements.to_a("//rewrite_text")[0].text
213 def wordid_regex(vol, piece, plane)
214 fpiece = sprintf("%02d", piece.to_i)
215 return "w#{vol}#{fpiece}#{plane}%%%%"