X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fdict.rb;h=ca5f25882147c94cb7c268cb5d27e31dda216fa6;hb=24bb60775741d3731400f1e430ef6bf3a2e1b933;hp=ef162215ef4ee5044363ba24ce4b11139d775832;hpb=a7c40c145f5be6c7c99752bb0af93c0735971694;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/dict.rb b/data/rbot/plugins/dict.rb index ef162215..ca5f2588 100644 --- a/data/rbot/plugins/dict.rb +++ b/data/rbot/plugins/dict.rb @@ -19,12 +19,13 @@ # TODO: cache results and reuse them if get_cached returns a cache copy DEMAURO_LEMMA = /(.*?)(?: - (.*?))<\/anchor>/ +CHAMBERS_LEMMA = /

(.*?)<\/span> (.*?)<\/span>(.*?)<\/p>/ class DictPlugin < Plugin - BotConfig.register BotConfigIntegerValue.new('dict.hits', + Config.register Config::IntegerValue.new('dict.hits', :default => 3, :desc => "Number of hits to return from a dictionary lookup") - BotConfig.register BotConfigIntegerValue.new('dict.first_par', + Config.register Config::IntegerValue.new('dict.first_par', :default => 0, :desc => "When set to n > 0, the bot will return the first paragraph from the first n dictionary hits") @@ -54,7 +55,7 @@ class DictPlugin < Plugin justcheck = params[:justcheck] word = params[:word].downcase - url = @dmwapurl % URI.escape(word) + url = @dmwapurl % CGI.escape(word) xml = nil info = @bot.httputil.get_response(url) rescue nil xml = info.body if info @@ -72,7 +73,7 @@ class DictPlugin < Plugin entries = xml.scan(DEMAURO_LEMMA) text = word urls = [] - if !entries.assoc(word) and !entries.assoc(word.upcase) + if not entries.transpose.first.grep(/\b#{word}\b/) return false if justcheck text += " not found. Similar words" end @@ -92,7 +93,7 @@ class DictPlugin < Plugin return unless first_pars > 0 Utils.get_first_pars urls, first_pars, :message => m, - :strip => /^\S+\s+-\s+/ + :strip => /^.+?\s+-\s+/ end @@ -106,10 +107,15 @@ class DictPlugin < Plugin word = params[:word].join [word, word + "_1"].each { |check| - url = @oxurl % URI.escape(check) - h = @bot.httputil.head(url, :max_redir => 5) - if h - m.reply("#{word} found: #{url}") unless justcheck + url = @oxurl % CGI.escape(check) + if params[:british] + url << "?view=uk" + end + h = @bot.httputil.get(url, :max_redir => 5) + if h and h.match(%r!

#{word}(?:1)?

!) + m.reply("#{word} : #{url}") unless justcheck + defn = $' + m.reply("#{Bold}%s#{Bold}: %s" % [word, defn.ircify_html(:nbsp => :space)], :overlong => :truncate) return true end } @@ -118,7 +124,7 @@ class DictPlugin < Plugin end def is_british?(word) - return oxford(nil, :word => word, :justcheck => true) + return oxford(nil, :word => word, :justcheck => true, :british => true) end @@ -126,7 +132,7 @@ class DictPlugin < Plugin justcheck = params[:justcheck] word = params[:word].to_s.downcase - url = @chambersurl % URI.escape(word) + url = @chambersurl % CGI.escape(word) xml = nil info = @bot.httputil.get_response(url) rescue nil xml = info.body if info @@ -143,10 +149,16 @@ class DictPlugin < Plugin when /No exact matches for .*?<\/b>, but the following may be helpful./ return false if justcheck m.reply "Nothing found for #{word}, but see #{url} for possible suggestions" - else - return true if justcheck - m.reply "#{word}: #{url}" + return end + # Else, we have a hit + return true if justcheck + m.reply "#{word}: #{url}" + entries = xml.scan(CHAMBERS_LEMMA) + hits = @bot.config['dict.hits'] + entries[0...hits].map { |ar| + m.reply(("#{Bold}%s#{Bold} #{Underline}%s#{Underline}%s" % ar).ircify_html, :overlong => :truncate) + } end def is_english?(word)