-# vim: set sw=2 et:\r
+#-- vim:sw=2:et\r
+#++\r
#\r
-# dict plugin: provides a link to the definition of a word in one of the supported\r
+# :title: Dictionary lookup plugin for rbot\r
+#\r
+# Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>\r
+# Copyright:: (C) 2006-2007 Giuseppe Bilotta\r
+# License:: GPL v2\r
+#\r
+# Provides a link to the definition of a word in one of the supported\r
# dictionaries. Currently available are\r
# * the Oxford dictionary for (British) English\r
# * the De Mauro/Paravia dictionary for Italian\r
# * the Chambers dictionary for English (accepts both US and UK)\r
#\r
-# other plugins can use this one to check if a given word is valid in italian\r
+# Other plugins can use this one to check if a given word is valid in italian\r
# or english by using the is_italian?/is_british?/is_english? methods\r
#\r
-# Author: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>\r
-#\r
# TODO: cache results and reuse them if get_cached returns a cache copy\r
\r
-require 'uri'\r
-\r
DEMAURO_LEMMA = /<anchor>(.*?)(?: - (.*?))<go href="lemma.php\?ID=(\d+)"\/><\/anchor>/\r
+CHAMBERS_LEMMA = /<p><span class="hwd">(.*?)<\/span> <span class="psa">(.*?)<\/span>(.*?)<\/p>/\r
\r
class DictPlugin < Plugin\r
+ Config.register Config::IntegerValue.new('dict.hits',\r
+ :default => 3,\r
+ :desc => "Number of hits to return from a dictionary lookup")\r
+ Config.register Config::IntegerValue.new('dict.first_par',\r
+ :default => 0,\r
+ :desc => "When set to n > 0, the bot will return the first paragraph from the first n dictionary hits")\r
+\r
def initialize\r
super\r
@dmurl = "http://www.demauroparavia.it/"\r
@dmwapurl = "http://wap.demauroparavia.it/index.php?lemma=%s"\r
+ @dmwaplemma = "http://wap.demauroparavia.it/lemma.php?ID=%s"\r
@oxurl = "http://www.askoxford.com/concise_oed/%s"\r
@chambersurl = "http://www.chambersharrap.co.uk/chambers/features/chref/chref.py/main?query=%s&title=21st"\r
end\r
justcheck = params[:justcheck]\r
\r
word = params[:word].downcase\r
- url = @dmwapurl % URI.escape(word)\r
- xml = @bot.httputil.get_cached(url)\r
+ url = @dmwapurl % CGI.escape(word)\r
+ xml = nil\r
+ info = @bot.httputil.get_response(url) rescue nil\r
+ xml = info.body if info\r
if xml.nil?\r
- info = @bot.httputil.last_response\r
info = info ? " (#{info.code} - #{info.message})" : ""\r
return false if justcheck\r
m.reply "An error occurred while looking for #{word}#{info}"\r
end\r
entries = xml.scan(DEMAURO_LEMMA)\r
text = word\r
- if !entries.assoc(word) and !entries.assoc(word.upcase)\r
+ urls = []\r
+ if not entries.transpose.first.grep(/\b#{word}\b/)\r
return false if justcheck\r
text += " not found. Similar words"\r
end\r
return true if justcheck\r
text += ": "\r
- text += entries[0...5].map { |ar|\r
- "#{ar[0]} - #{ar[1].gsub(/<\/?em>/,'')}: #{@dmurl}#{ar[2]}"\r
+ n = 0\r
+ hits = @bot.config['dict.hits']\r
+ text += entries[0...hits].map { |ar|\r
+ n += 1\r
+ urls << @dmwaplemma % ar[2]\r
+ "#{n}. #{Bold}#{ar[0]}#{Bold} - #{ar[1].gsub(/<\/?em>/,'')}: #{@dmurl}#{ar[2]}"\r
}.join(" | ")\r
m.reply text\r
+\r
+ first_pars = @bot.config['dict.first_par']\r
+\r
+ return unless first_pars > 0\r
+\r
+ Utils.get_first_pars urls, first_pars, :message => m,\r
+ :strip => /^.+?\s+-\s+/\r
+\r
end\r
\r
def is_italian?(word)\r
\r
word = params[:word].join\r
[word, word + "_1"].each { |check|\r
- url = @oxurl % URI.escape(check)\r
- h = @bot.httputil.head(url)\r
- if h\r
- m.reply("#{word} found: #{url}") unless justcheck\r
+ url = @oxurl % CGI.escape(check)\r
+ if params[:british]\r
+ url << "?view=uk"\r
+ end\r
+ h = @bot.httputil.get(url, :max_redir => 5)\r
+ if h and h.match(%r!<h2>#{word}(?:<sup>1</sup>)?</h2>!)\r
+ m.reply("#{word} : #{url}") unless justcheck\r
+ defn = $'\r
+ m.reply("#{Bold}%s#{Bold}: %s" % [word, defn.ircify_html(:nbsp => :space)], :overlong => :truncate)\r
return true\r
end\r
}\r
end\r
\r
def is_british?(word)\r
- return oxford(nil, :word => word, :justcheck => true)\r
+ return oxford(nil, :word => word, :justcheck => true, :british => true)\r
end\r
\r
\r
justcheck = params[:justcheck]\r
\r
word = params[:word].to_s.downcase\r
- url = @chambersurl % URI.escape(word)\r
- xml = @bot.httputil.get_cached(url)\r
+ url = @chambersurl % CGI.escape(word)\r
+ xml = nil\r
+ info = @bot.httputil.get_response(url) rescue nil\r
+ xml = info.body if info\r
case xml\r
when nil\r
- info = @bot.httputil.last_response\r
info = info ? " (#{info.code} - #{info.message})" : ""\r
return false if justcheck\r
m.reply "An error occurred while looking for #{word}#{info}"\r
when /No exact matches for <b>.*?<\/b>, but the following may be helpful./\r
return false if justcheck\r
m.reply "Nothing found for #{word}, but see #{url} for possible suggestions"\r
- else\r
- return false if justcheck\r
- m.reply "#{word}: #{url}"\r
+ return\r
end\r
+ # Else, we have a hit\r
+ return true if justcheck\r
+ m.reply "#{word}: #{url}"\r
+ entries = xml.scan(CHAMBERS_LEMMA)\r
+ hits = @bot.config['dict.hits']\r
+ entries[0...hits].map { |ar|\r
+ m.reply(("#{Bold}%s#{Bold} #{Underline}%s#{Underline}%s" % ar).ircify_html, :overlong => :truncate)\r
+ }\r
end\r
\r
def is_english?(word)\r