layout "main"
caches_page :xiesheng, :rime_groups
caches_action :xiesheng, :rime_groups
-
+
def index
- c = params[:char]
- @extext = params[:extext]
- if c then
- (@ucs, @char) = process_char(c)
- process_unihan
- process_sbgy
- process_dianzhu
- process_daxu
+ if request.post? then
+ @errors = []
+ (@ucs, @char, @extext) = check_targets
+# raise Exception
+ if @errors.empty? then
+ begin
+ main_procedure_at_home
+ rescue StandardError => bang
+ @message = bang.message
+ end
+ end
+ else
+ @char = params[:char]
+ main_procedure_at_home
end
+ end
+ def main_procedure_at_home
+ process_unihan
+ process_sbgy
+ process_dianzhu
+ process_daxu
if @extext then
- search_daxu
+ begin
+ search_daxu
+ rescue TooManyCandidatesError => bang
+ @errors << bang
+ end
end
end
def rime_groups
end
private
+ # Validation Input Parameters
+ def check_targets
+ c = params[:char]
+ extext = params[:extext]
+ # TODO: 不正な文字のチェック
+ if extext and extext.size == 0 then
+ @errors << "検索する説解が入力されていません"
+ elsif ! extext and c and c.size == 0 then
+ @errors << "検索する文字が入力されていません"
+ elsif c =~/[0-9A-Fa-f]+/ and not c =~ /[0-9A-Fa-f]{4,5}/ then
+ @errors << "#{c}: 正しいコードポイントではありません"
+ else
+ (ucs, char) = process_char(c)
+ return [ucs, char, extext]
+ end
+ end
+
# Main Procesures
def process_unihan
cp = ucs2codepoint(@ucs)
@pattern = sprintf("%%%s%%", @extext)
wordheads = DxWordhead.find_by_sql(["select * from dx_wordheads " +
"where exp like ?", @pattern])
+ searched = wordheads.size
+ if searched > 50 then
+ raise TooManyCandidatesError, "#{searched}件、見付かりました。検索条件を絞り込んでください。"
+ end
wordheads.each do |word|
wordid = word.wordid
rad = DxRadical.find(:first, :conditions => ["id = ?", word.radical_id])
return exp.sub(/#{pattern}/u, replaced)
end
end
+
+class TooManyCandidatesError < RuntimeError
+end
<li><%= link_to("大徐本", :controller => "daxu", :action => "index") %></li>
<li><span>段注</span></li>
<li><%= link_to("廣韻", :controller => "sbgy", :action => "index") %></li>
+ <li><%= link_to("漢語大字典", :controller => "unihan", :action => "hdz") %></li>
<li><%= link_to("十七部表", :controller => "hanmorph", :action => "rime_groups") %></li>
<li><%= link_to("諧聲表", :controller => "hanmorph", :action => "xiesheng") %></li>
<li><%= link_to("説明", :controller => "hanmorph", :action => "explanation") %></li>