From 72cc0c98f1ef2cac7566773922bc84a5c25ad773 Mon Sep 17 00:00:00 2001 From: nangxiang Date: Wed, 14 Sep 2011 23:04:38 +0900 Subject: [PATCH 1/1] Adds sbgy without layout --- hanMorph/app/controllers/sbgy_controller.rb | 217 ++++++++++++++++++++++++++++ hanMorph/app/views/sbgy/index.html.erb | 15 ++ hanMorph/app/views/sbgy/rhyme.html.erb | 23 +++ hanMorph/app/views/sbgy/show.html.erb | 67 +++++++++ hanMorph/app/views/sbgy/xiaoyun.html.erb | 71 +++++++++ 5 files changed, 393 insertions(+) create mode 100644 hanMorph/app/controllers/sbgy_controller.rb create mode 100644 hanMorph/app/views/sbgy/index.html.erb create mode 100644 hanMorph/app/views/sbgy/rhyme.html.erb create mode 100644 hanMorph/app/views/sbgy/show.html.erb create mode 100644 hanMorph/app/views/sbgy/xiaoyun.html.erb diff --git a/hanMorph/app/controllers/sbgy_controller.rb b/hanMorph/app/controllers/sbgy_controller.rb new file mode 100644 index 0000000..3c9e349 --- /dev/null +++ b/hanMorph/app/controllers/sbgy_controller.rb @@ -0,0 +1,217 @@ +require 'rexml/document' +LINES_PER_PAGE = 10 +LINE_REGEX = /w[0-9ab]{4}([0-9]{2})([0-9]{2})/ +SISHENG = {'sp' => '上平聲', 'xp' => '下平聲', 's' => '上聲', 'q' => '去聲', 'r' => '入聲'} + +class SbgyController < ApplicationController + layout "main" + caches_page :index + caches_action :index + + # def index + # c = params[:char] + # return nil unless c + # (ucs, char) = process_char(c) + # @chars = Array.new + # wordheads = Wordhead.find(:all, :conditions => ["word = ?", char]) + # wordheads.each do |wordhead| + # voice = Voice.find(:first, :conditions => ["id = ?", wordhead.voice_id]) + # rhyme = Rhyme.find(:first, :conditions => ["id = ?", voice.rhyme_id]) + # @chars.push([wordhead, voice, rhyme]) + # end + # end + def index + @volumes = Volume.find(:all) + @rhymes = Array.new + @volumes.each do |volume| + rhymes = Rhyme.find(:all, :conditions => ["volume_id = ?", volume.id]) + @rhymes.push(rhymes) + end + end + def rhyme + rhyme_id = params[:id] + @rhyme = Rhyme.find(:first, :conditions => ["id = ?", rhyme_id]) + @volume = Volume.find(:first, :conditions => ["id = ?", @rhyme.volume_id]) + @voices = Voice.find(:all, :conditions => ["rhyme_id = ?", rhyme_id]) + end + # TODO: piece -> leaf + def show + @vol = params[:vol] + @piece = params[:piece] + @plane = params[:plane] + return nil unless @piece + return nil if @piece.empty? + @regex = wordid_regex(@vol, @piece, @plane) + wordheads = Wordhead.find(:all, :conditions => ["xmlid like ?", @regex]) + @chars = get_matrix(wordheads) + (@previous_piece, @previous_plane) = previous_page(@piece, @plane) + (@next_piece, @next_plane) = next_page(@piece, @plane) + end + def xiaoyun + fanqie = params[:fanqie] +# return nil unless id + if fanqie != nil then + f = format("%s", fanqie) + @voice = Voice.find(:first, :conditions => ["fanqie = ?", f]) + else + id = params[:id] + @voice = Voice.find(:first, :conditions => ["id = ?", id]) +# elsif not(xiaoyun.empty?) then +# @voice = Voice.find(:first, :conditions => ["name = ?", xiaoyun]) + end + return nil unless @voice + @rhyme = Rhyme.find(:first, :conditions => ["id = ?", @voice.rhyme_id]) + @volume = Volume.find(:first, :conditions => ["id = ?", @rhyme.volume_id]) + @wordheads = + Wordhead.find(:all, :conditions => ["voice_id = ?", @voice.id]) + end + def setup + @wordheads = 0 + @voices = 0 + @rhymes = 0 + setup_wordheads + setup_voices + setup_rhymes + setup_volumes + end + private + def previous_page(piece, plane) + return [nil, nil] if piece == "0" and plane == "a" + if plane == "a" then + return [(piece.to_i - 1).to_s, "b"] + else + return [piece, "a"] + end + end + def next_page(piece, plane) + if plane == "b" then + return [(piece.to_i + 1).to_s, "a"] + else + return [piece, "b"] + end + end + def get_matrix(wordheads) + page = get_empty_page(LINES_PER_PAGE) + wordheads.each do |wordhead| + wordhead.xmlid =~ LINE_REGEX + line = $1 + num = $2 + push_to_page(page, line, num, wordhead) + end + return page + end + def get_empty_page(lines_nums) + page = Array.new + (0..(lines_nums - 1)).each do |line| + page[line] = Array.new + end + return page + end + def push_to_page(page, line, num, wordhead) + i = line.to_i - 1 + j = num.to_i - 1 + page[i][j] = wordhead + end + def setup_wordheads + wordheads = Wordhead.find(:all) + wordheads.each do |wordhead| + word = wordhead.word.to_s + wordhead.name = get_rewrite_word(word) + @wordheads += 1 + wordhead.save! + end + end + def setup_voices + voices = Voice.find(:all) + voices.each do |voice| + word = Wordhead.find(:first, :conditions => ["id = ?", voice.wordhead_id]) + voice.name = word.word + # TODO: Fanqie mixed + note = "" + word.note.to_s + "" + doc = REXML::Document.new note +# fanqie = doc.elements.to_a("//fanqie")[0].text + doc.root.each_child do |child| + if child.node_type == :element and child.name == "fanqie" then + fanqie = child.to_s + voice.fanqie = get_rewrite_text(fanqie) + end + end + @voices += 1 + voice.save! + end + end + def setup_rhymes + rhymes = Rhyme.find(:all) + rhymes.each do |rhyme| + voice = Voice.find(:first, :conditions => ["id = ?", rhyme.voice_id]) + sisheng = get_sisheng(rhyme.xmlid) +# rhyme.name = sisheng + rhyme.num + voice.name 変更 <24 Mar> + rhyme.name = rhyme.num + voice.name + rhyme.save! + @rhymes += 1 + end + end + def setup_volumes + volumes = Volume.find(:all) + volumes.each do |volume| + if volume.title =~ /^廣韻(.+聲)/u then + volume.name = $1 + end + volume.save! + end + end + def get_sisheng(xmlid) # Rhyme + xmlid =~ /([spxqr]+)[0-9]+/ + sisheng_id = $1 + return SISHENG[sisheng_id] + end + # TODO: Rewrite a child node + def get_rewrite_word(word) + string = "" + word + "" + doc = REXML::Document.new string + original = doc.elements.to_a("//original_word") + if original.size > 0 then + rewrite = original[0].elements.to_a("//rewrite_word") + if rewrite.size > 0 then + name = rewrite[0].text + else + name = original[0].text + end + else + name = word + end + return name + end + def get_rewrite_fanqie(fanqie) + ftext = "" + fanqie.each_child do |child| + if child.node_type == :text then + ftext += child.to_s + elsif child.name == "original_word" then + otext = child.text + child.elements.each("rewrite_word") do |rewrite| + ftext += rewrite.text + end + else + ftext += child.to_s + end + end + return ftext + end + def get_rewrite_text(text) + return "" unless text + string = "" + text + "" + doc = REXML::Document.new string + original = doc.elements.to_a("//original_text") + if original and original.size > 0 then + rtext = doc.elements.to_a("//rewrite_text")[0].text + else + rtext = text + end + return rtext + end + def wordid_regex(vol, piece, plane) + fpiece = sprintf("%02d", piece.to_i) + return "w#{vol}#{fpiece}#{plane}%%%%" + end +end diff --git a/hanMorph/app/views/sbgy/index.html.erb b/hanMorph/app/views/sbgy/index.html.erb new file mode 100644 index 0000000..b2fa116 --- /dev/null +++ b/hanMorph/app/views/sbgy/index.html.erb @@ -0,0 +1,15 @@ +<% content_for :head do %>廣韻(巻)<% end %> + +

+ <%= link_to("Index", :controller => "kanjidb", :action => "index") %> + <%= link_to("Show", :action => "show") %> +

+ +<% @volumes.each do |volume| %> +

<%= volume.title %>

+

+ <% @rhymes.shift.each do |rhyme| %> + <%= link_to(rhyme.name, :action => "rhyme", :id => rhyme.id) %> + <% end %> +

+<% end %> diff --git a/hanMorph/app/views/sbgy/rhyme.html.erb b/hanMorph/app/views/sbgy/rhyme.html.erb new file mode 100644 index 0000000..e8837fd --- /dev/null +++ b/hanMorph/app/views/sbgy/rhyme.html.erb @@ -0,0 +1,23 @@ +<% content_for :head do %>廣韻(韻目)<% end %> + + +

+ <%= link_to("Index", :controller => "kanjidb", :action => "index") %> + <%= link_to("å·»", :action => "index") %> + <%= link_to("Show", :action => "show") %> +

+ +

+ + <%= @volume.name %><%= @rhyme.name %> + +

+

+ <% @voices.each do |voice| %> + + <%= link_to(voice.name, + :action => "xiaoyun", :id => voice.id) %> + <%= voice.fanqie %> + <% end %> +

+ diff --git a/hanMorph/app/views/sbgy/show.html.erb b/hanMorph/app/views/sbgy/show.html.erb new file mode 100644 index 0000000..c78b968 --- /dev/null +++ b/hanMorph/app/views/sbgy/show.html.erb @@ -0,0 +1,67 @@ +<% content_for :head do %>廣韻 版本形式<% end %> +<% require 'cgi' %> +<% unihan_g = "http://www.unicode.org/cgi-bin/refglyph?24-" %> +<% unihan_r = "http://www.unicode.org/cgi-bin/GetUnihanData.pl?codepoint=" %> + +

<%= link_to("Index", :controller => "kanjidb", :action => "index") %>

+<% form_tag(:controller => "sbgy", :action => "show", :method => "get") do %> + + + <%= select_tag(:vol, ' + + + + ') %> + <%= label_tag(:piece, "葉:") %> + <%= text_field_tag(:piece, "", :size => "2") %> + <%= select_tag(:plane, ' + ') %> + <%= submit_tag("Show") %> +<% end %> + +

+ <% if @previous_piece then %> + <%= link_to(image_tag("1leftarrow-32.png"), + :action => "show", :vol => @vol, :piece => @previous_piece, + :plane => @previous_plane) %> + <% end %> + <% if @piece and @piece.size > 0 then %> + <%= @vol %>巻 + <%= @piece %>葉 + <%= @plane %> + <% end %> + <% if @next_piece then %> + <%= link_to(image_tag("1rightarrow-32.png"), + :action => "show", :vol => @vol, + :piece => @next_piece, :plane => @next_plane) %> + <% end %> +

+ +<% if @chars then %> +

+ <% if @chars then %> + <% num = 0 %> + <% @chars.each do |line| %> + <% num += 1 %> + <%= sprintf("%02d:", num) %> + <% line.each do |char| %> + <% if char then %> + <% point = char.name.unpack("U")[0] %> + <% unihan_glyph = format("%s%x", unihan_g, point) %> + <% unihan_ref = CGI.escapeHTML(unihan_r + format("%x", point)) %> + + + <%= link_to("" + char.name + "", + :controller => "kanjidb", + :action => "index", :char => char.name) %> + <% end %> + <% end %> +
+ <% end %> + <% end %> +

+<% end %> diff --git a/hanMorph/app/views/sbgy/xiaoyun.html.erb b/hanMorph/app/views/sbgy/xiaoyun.html.erb new file mode 100644 index 0000000..4731044 --- /dev/null +++ b/hanMorph/app/views/sbgy/xiaoyun.html.erb @@ -0,0 +1,71 @@ +<% content_for :head do %>廣韻 小韻<% end %> + +<% require 'cgi' %> +<% unihan_g = "http://www.unicode.org/cgi-bin/refglyph?24-" %> +<% unihan_r = "http://www.unicode.org/cgi-bin/GetUnihanData.pl?codepoint=" %> +<% def print_note(note) + doc = REXML::Document.new note + doc.elements.each("//original_text") do |original| + original.elements.each("//rewrite_text") do |rewrite| + if rewrite then + rtext = rewrite.text + relem = REXML::Element.new("font") + relem.add_attribute("color", "red") + relem.add_text(rtext) + original.parent.insert_after(original, relem) + end + end + otext = original.text + replace = REXML::Element.new("del") + replace.add_text(otext) + original.parent.replace_child(original, replace) + end + return doc.to_s + end +%> +<% unihan_g = "http://www.unicode.org/cgi-bin/refglyph?24-" %> +<% unihan_r = "http://www.unicode.org/cgi-bin/GetUnihanData.pl?codepoint=" %> + +

+ <% form_tag(:controller => "sbgy", :action => "xiaoyun", :method => "get") do %> + <%= link_to("Index", :controller => 'kanjidb', :action => "index") %> + <%= link_to("Show", :action => "show") %> + + + <%= label_tag(:fanqie, "反切:") %> + <%= text_field_tag(:fanqie, "", :size => "5") %> + + <%= submit_tag("Search") %> + <% end %> +

+ +<% if @voice then %> +

+

+ <%= link_to(@volume.name + @rhyme.name, + :action => "rhyme", :id => @rhyme.id) %> + 小韻「<%= @voice.name %>」 + <%= @voice.fanqie %> + <%= @voice.ipa %> +

+ <% @wordheads.each do |wordhead| %> + <% point = wordhead.name.unpack("U")[0] %> + <%= link_to("" + wordhead.name + "", :controller => "kanjidb", :action => "index", :char => wordhead.name) %> + + + + <%= wordhead.note %> + <% end %> +

+<% end %> -- 2.11.0