Changeset 751 for lang/ruby/chokan/trunk

Show
Ignore:
Timestamp:
10/27/07 14:02:35 (6 years ago)
Author:
cho45
Message:

lang/ruby/chokan/trunk/plugins/dictionary.rb:

新 ALC に対応

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/ruby/chokan/trunk/plugins/dictionary.rb

    r283 r751  
    22 
    33require "chokan/plugin_base" 
     4require "rubygems" 
     5require "hpricot" 
    46 
    57class Dictionary < Chokan::PluginBase 
     
    1719                        Thread.start($1) do |e| 
    1820                                j = e2j(e) 
    19                                 if j 
    20                                         j[1].first(5).each do |l| 
     21                                if j.empty? 
     22                                        notice(channel, "スペルミス?") 
     23                                else 
     24                                        j.first(5).each do |m,l| 
     25                                                l = l[/.{100}/] if l.size > 100 
    2126                                                notice(channel, l) 
    2227                                        end 
     
    2429                                                notice(channel, "上位5個のみ表示しました。→Google「英和 #{e}」") 
    2530                                        end 
    26                                 else 
    27                                         notice(channel, "スペルミス?") 
    2831                                end 
    2932                        end 
     
    3134                        Thread.start($1) do |j| 
    3235                                e = j2e(j) 
    33                                 if e 
    34                                         e[1] = e[1][/.{100}/] if e[1].size > 100 
    35                                         notice(channel, e[1]) 
     36                                if e.empty? 
     37                                        notice(channel, "見つからないよ?") 
    3638                                else 
    37                                         notice(channel, "見つからないよ?") 
     39                                        e.first(1).each do |m,l| 
     40                                                l = l[/.{100}/] if l.size > 100 
     41                                                notice(channel, l) 
     42                                        end 
    3843                                end 
    3944                        end 
     
    4247 
    4348        private 
    44          
    4549        def e2j(e) 
    46                 uri = URI("http://www2.alc.co.jp/ejr/index.php?word_in=#{URI::escape(e.to_sjis)}&word_in2=%82%A9%82%AB%82%AD%82%AF%82%B1&word_in3=zJPa7DCxJ15687987t") 
    47                 dic = nil 
     50                result = [] 
     51                uri = URI("http://eow.alc.co.jp/#{URI::escape(e)}/UTF-8/") 
    4852                Net::HTTP.start(uri.host, uri.port) do |http| 
    4953                        res = http.get(uri.request_uri, {"User-Agent" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20040913 Firefox/0.10.1"}) 
    50                         dic = res.body.to_u8.scan(%r|<li><span class="ejr_e"><font color="darkblue">\n\t\t\t\t(.+?)\n\t\t\t</font></span><br>(.+?)\n\t\t\t</li>|) 
     54                        doc = Hpricot(res.body) 
     55                        (doc/"#resultList ul li").each do |e| 
     56                                midashi =  (e/".midashi") 
     57                                next unless midashi 
     58                                midashi = midashi.text 
     59                                unless midashi.empty? 
     60                                        result << [midashi, (e/:div).text] 
     61                                end 
     62                        end 
    5163                end 
    52                 if dic.empty? 
    53                         nil 
    54                 else 
    55                         ret = dic[0][1].split(/<br\/?>/) 
    56 #                       ret.delete_if {|l| l =~ /^・|^【レベル】/ }.collect! {|l| l.gsub(/<.*?>|{.+?}/, "")} 
    57                         ret.delete_if {|l| l =~ /^・/ }.collect! {|l| l.gsub(/<[\w\s"'\/=]+?>|{.+?}/, "")} 
    58                         [dic[0][0].gsub(/<.*?>/, ""), ret] 
    59                 end 
     64                result 
    6065        end 
    61          
     66 
    6267        def j2e(j) 
    63                 uri = URI("http://www2.alc.co.jp/ejr/index.php?word_in=#{URI::escape(j.to_sjis)}&word_in2=%82%A9%82%AB%82%AD%82%AF%82%B1&word_in3=zJPa7DCxJ15687987t") 
    64                 dic = [] 
     68                result = [] 
     69                uri = URI("http://eow.alc.co.jp/#{URI::escape(j)}/UTF-8/") 
    6570                Net::HTTP.start(uri.host, uri.port) do |http| 
    6671                        res = http.get(uri.request_uri, {"User-Agent" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20040913 Firefox/0.10.1"}) 
    67                         dic = res.body.scan(%r|<li>\n\t\t\t\t\t<font color="darkblue"><span class="ejr_j">\n\t\t\t\t\t(.+?)\n\t\t\t\t\t</span></font>\n\t\t\t\t\t\n\t\t\t\t\t<ol>\n\t\t\t\t\t\t(.+?)\n\t\t\t\t\t</ol>\n\t\t\t\t</li>|) 
     72                        doc = Hpricot(res.body) 
     73                        (doc/"#resultList ul li").each do |e| 
     74                                midashi =  (e/".midashi") 
     75                                next unless midashi 
     76                                midashi = midashi.text 
     77                                unless midashi.empty? 
     78                                        result << [midashi, (e/:div).text] 
     79                                end 
     80                        end 
    6881                end 
    69                 dic.empty? ? nil : [dic[0][0].gsub(/<.+?>/, ""), dic[0][1].gsub(/<.+?>/, "")] 
     82                result 
    7083        end 
    7184end