X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fdict.rb;h=ca5f25882147c94cb7c268cb5d27e31dda216fa6;hb=24bb60775741d3731400f1e430ef6bf3a2e1b933;hp=a948fd06befc2bbfa2bc18f1642efb6aa680b982;hpb=bcacf025a0d2cbba181ad3e55fba30926286f9c8;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/dict.rb b/data/rbot/plugins/dict.rb index a948fd06..ca5f2588 100644 --- a/data/rbot/plugins/dict.rb +++ b/data/rbot/plugins/dict.rb @@ -1,23 +1,34 @@ -# vim: set sw=2 et: +#-- vim:sw=2:et +#++ # -# dict plugin: provides a link to the definition of a word in one of the supported +# :title: Dictionary lookup plugin for rbot +# +# Author:: Giuseppe "Oblomov" Bilotta +# Copyright:: (C) 2006-2007 Giuseppe Bilotta +# License:: GPL v2 +# +# Provides a link to the definition of a word in one of the supported # dictionaries. Currently available are # * the Oxford dictionary for (British) English # * the De Mauro/Paravia dictionary for Italian # * the Chambers dictionary for English (accepts both US and UK) # -# other plugins can use this one to check if a given word is valid in italian +# Other plugins can use this one to check if a given word is valid in italian # or english by using the is_italian?/is_british?/is_english? methods # -# Author: Giuseppe "Oblomov" Bilotta -# # TODO: cache results and reuse them if get_cached returns a cache copy -require 'uri' - DEMAURO_LEMMA = /(.*?)(?: - (.*?))<\/anchor>/ +CHAMBERS_LEMMA = /

(.*?)<\/span> (.*?)<\/span>(.*?)<\/p>/ class DictPlugin < Plugin + Config.register Config::IntegerValue.new('dict.hits', + :default => 3, + :desc => "Number of hits to return from a dictionary lookup") + 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") + def initialize super @dmurl = "http://www.demauroparavia.it/" @@ -44,10 +55,11 @@ class DictPlugin < Plugin justcheck = params[:justcheck] word = params[:word].downcase - url = @dmwapurl % URI.escape(word) - xml = @bot.httputil.get_cached(url) + url = @dmwapurl % CGI.escape(word) + xml = nil + info = @bot.httputil.get_response(url) rescue nil + xml = info.body if info if xml.nil? - info = @bot.httputil.last_response info = info ? " (#{info.code} - #{info.message})" : "" return false if justcheck m.reply "An error occurred while looking for #{word}#{info}" @@ -61,21 +73,27 @@ 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 return true if justcheck text += ": " n = 0 - text += entries[0...5].map { |ar| + hits = @bot.config['dict.hits'] + text += entries[0...hits].map { |ar| n += 1 urls << @dmwaplemma % ar[2] "#{n}. #{Bold}#{ar[0]}#{Bold} - #{ar[1].gsub(/<\/?em>/,'')}: #{@dmurl}#{ar[2]}" }.join(" | ") m.reply text - Utils.get_first_pars urls, 5, :http_util => @bot.httputil, :message => m + first_pars = @bot.config['dict.first_par'] + + return unless first_pars > 0 + + Utils.get_first_pars urls, first_pars, :message => m, + :strip => /^.+?\s+-\s+/ end @@ -89,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) - 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 } @@ -101,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 @@ -109,11 +132,12 @@ class DictPlugin < Plugin justcheck = params[:justcheck] word = params[:word].to_s.downcase - url = @chambersurl % URI.escape(word) - xml = @bot.httputil.get_cached(url) + url = @chambersurl % CGI.escape(word) + xml = nil + info = @bot.httputil.get_response(url) rescue nil + xml = info.body if info case xml when nil - info = @bot.httputil.last_response info = info ? " (#{info.code} - #{info.message})" : "" return false if justcheck m.reply "An error occurred while looking for #{word}#{info}" @@ -125,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 false 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)