Changeset 751 for lang/ruby/chokan/trunk
- Timestamp:
- 10/27/07 14:02:35 (6 years ago)
- Files:
-
- 1 modified
-
lang/ruby/chokan/trunk/plugins/dictionary.rb (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/ruby/chokan/trunk/plugins/dictionary.rb
r283 r751 2 2 3 3 require "chokan/plugin_base" 4 require "rubygems" 5 require "hpricot" 4 6 5 7 class Dictionary < Chokan::PluginBase … … 17 19 Thread.start($1) do |e| 18 20 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 21 26 notice(channel, l) 22 27 end … … 24 29 notice(channel, "上位5個のみ表示しました。→Google「英和 #{e}」") 25 30 end 26 else27 notice(channel, "スペルミス?")28 31 end 29 32 end … … 31 34 Thread.start($1) do |j| 32 35 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, "見つからないよ?") 36 38 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 38 43 end 39 44 end … … 42 47 43 48 private 44 45 49 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 = nil50 result = [] 51 uri = URI("http://eow.alc.co.jp/#{URI::escape(e)}/UTF-8/") 48 52 Net::HTTP.start(uri.host, uri.port) do |http| 49 53 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 51 63 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 60 65 end 61 66 62 67 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/") 65 70 Net::HTTP.start(uri.host, uri.port) do |http| 66 71 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 68 81 end 69 dic.empty? ? nil : [dic[0][0].gsub(/<.+?>/, ""), dic[0][1].gsub(/<.+?>/, "")]82 result 70 83 end 71 84 end
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)