OSDN Git Service

Adds sbgy without layout
[dianzhuhui/site.git] / hanMorph / app / controllers / sbgy_controller.rb
1 require 'rexml/document'
2 LINES_PER_PAGE = 10
3 LINE_REGEX = /w[0-9ab]{4}([0-9]{2})([0-9]{2})/
4 SISHENG = {'sp' => '上平聲', 'xp' => '下平聲', 's' => '上聲', 'q' => '去聲', 'r' => '入聲'}
5
6 class SbgyController < ApplicationController
7   layout "main"
8   caches_page :index
9   caches_action :index
10
11   # def index
12   #   c = params[:char]
13   #   return nil unless c
14   #   (ucs, char) = process_char(c)
15   #   @chars = Array.new
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])
21   #   end
22   # end
23   def index
24     @volumes = Volume.find(:all)
25     @rhymes = Array.new
26     @volumes.each do |volume|
27       rhymes = Rhyme.find(:all, :conditions => ["volume_id = ?", volume.id])
28       @rhymes.push(rhymes)
29     end
30   end
31   def rhyme
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])
36   end
37   # TODO: piece -> leaf
38   def show
39     @vol = params[:vol]
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)
49   end
50   def xiaoyun
51     fanqie = params[:fanqie]
52 #    return nil unless id
53     if fanqie != nil then
54       f = format("<fanqie>%s</fanqie>", fanqie)
55       @voice = Voice.find(:first, :conditions => ["fanqie = ?", f])
56     else 
57       id = params[:id]
58       @voice = Voice.find(:first, :conditions => ["id = ?", id])
59 #    elsif not(xiaoyun.empty?) then
60 #      @voice = Voice.find(:first, :conditions => ["name = ?", xiaoyun])
61     end
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])
65     @wordheads = 
66       Wordhead.find(:all, :conditions => ["voice_id = ?", @voice.id])
67   end
68   def setup
69     @wordheads = 0
70     @voices = 0
71     @rhymes = 0
72     setup_wordheads
73     setup_voices
74     setup_rhymes
75     setup_volumes
76   end
77   private
78   def previous_page(piece, plane)
79     return [nil, nil] if piece == "0" and plane == "a"
80     if plane == "a" then
81       return [(piece.to_i - 1).to_s, "b"]
82     else
83       return [piece, "a"]
84     end
85   end
86   def next_page(piece, plane)
87     if plane == "b" then
88       return [(piece.to_i + 1).to_s, "a"]
89     else
90       return [piece, "b"]
91     end
92   end
93   def get_matrix(wordheads)
94     page = get_empty_page(LINES_PER_PAGE)
95     wordheads.each do |wordhead|
96       wordhead.xmlid =~ LINE_REGEX
97       line = $1
98       num = $2
99       push_to_page(page, line, num, wordhead)
100     end
101     return page
102   end
103   def get_empty_page(lines_nums)
104     page = Array.new
105     (0..(lines_nums - 1)).each do |line|
106       page[line] = Array.new
107     end
108     return page
109   end
110   def push_to_page(page, line, num, wordhead)
111     i = line.to_i - 1
112     j = num.to_i - 1
113     page[i][j] = wordhead
114   end
115   def setup_wordheads
116     wordheads = Wordhead.find(:all)
117     wordheads.each do |wordhead|
118       word = wordhead.word.to_s
119       wordhead.name = get_rewrite_word(word)
120       @wordheads += 1
121       wordhead.save!
122     end
123   end
124   def setup_voices
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
129       # TODO: Fanqie mixed
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
135           fanqie = child.to_s
136           voice.fanqie = get_rewrite_text(fanqie)
137         end
138       end
139       @voices += 1
140       voice.save!
141     end
142   end
143   def setup_rhymes
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
150       rhyme.save!
151       @rhymes += 1
152     end
153   end
154   def setup_volumes
155     volumes = Volume.find(:all)
156     volumes.each do |volume|
157       if volume.title =~ /^廣韻(.+聲)/u then
158         volume.name = $1
159       end
160       volume.save!
161     end
162   end
163   def get_sisheng(xmlid) # Rhyme
164     xmlid =~ /([spxqr]+)[0-9]+/
165     sisheng_id = $1
166     return SISHENG[sisheng_id]
167   end
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
177       else
178         name = original[0].text
179       end
180     else
181       name = word
182     end
183     return name
184   end
185   def get_rewrite_fanqie(fanqie)
186     ftext = ""
187     fanqie.each_child do |child|
188       if child.node_type == :text then
189         ftext += child.to_s
190       elsif  child.name == "original_word" then
191         otext = child.text
192         child.elements.each("rewrite_word") do |rewrite|
193           ftext += rewrite.text
194         end
195       else
196         ftext += child.to_s
197       end
198     end
199     return ftext
200   end
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
208     else
209       rtext = text
210     end
211     return rtext
212   end
213   def wordid_regex(vol, piece, plane)
214     fpiece = sprintf("%02d", piece.to_i)
215     return "w#{vol}#{fpiece}#{plane}%%%%"
216   end
217 end