From ec67cbca6543d4eb7a95e83fb10f340383bb47e7 Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Wed, 5 Jul 2006 16:19:40 +0000 Subject: Add plugin for dictionary lookup using the (Italian) De Mauro/Paravia online dictionary --- data/rbot/plugins/demauro.rb | 95 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 data/rbot/plugins/demauro.rb diff --git a/data/rbot/plugins/demauro.rb b/data/rbot/plugins/demauro.rb new file mode 100644 index 00000000..7e5705a1 --- /dev/null +++ b/data/rbot/plugins/demauro.rb @@ -0,0 +1,95 @@ +# TODO: cache definitions + +require 'erb' + +class DeMauroPlugin < Plugin + include ERB::Util + + def initialize + super + @dmurl = "http://www.demauroparavia.it/" + end + + + def help(plugin, topic="") + return "demauro => fornisce il link della definizione della parola dal dizionario De Mauro/Paravia" + end + + def demauro(m, params) + parola = params[:parola] + url = @dmurl + "cerca?stringa=#{url_encode(parola)}" + uri = URI.parse(url) + http = @bot.httputil.get_proxy(uri) + xml = nil + defurls = Array.new + begin + http.start() { |http| + resp = http.get(uri) + case resp.code + when "200" + xml = resp.body + when "302" + loc = resp['location'] + if loc =~ /#{@dmurl}\d+/ + defurls << loc + end + else + debug resp.to_a + end + } + rescue => e + debug "HttpUtil.get exception: #{e}, while trying to get #{uri}" + m.reply "Errore" + return + end + if xml + if xml=~ /Non ho trovato occorrenze per/ + m.reply "Parola non trovata" + return + else + xml.gsub(/href="(\d+)"/) { |match| + debug match.to_a.join(" || ") + defurls << "#{@dmurl}#{$1}" + } + end + end + lemmas = Array.new + defurls.each { |url| + uri = URI.parse(url) + http = @bot.httputil.get_proxy(uri) + begin + debug "Scanning #{url}" + http.start() { |http| + resp = http.get(uri) + case resp.code + when "200" + debug "Got data" + matched = /(.*)<\/span>(.*?)<\/span>/.match(resp.body) + dirtylemma = matched[1] + qual = matched[2] + lemma = dirtylemma.gsub(/<\/?span(?: class="pipelemma")?>/,"") + debug lemma + lemma = lemma.gsub(/1<\/sup>/,'¹').gsub(/2<\/sup>/,'²').gsub(/3<\/sup>/,'³') + lemma = lemma.gsub(/4<\/sup>/,'⁴').gsub(/5<\/sup>/,'⁵').gsub(/6<\/sup>/,'⁶') + lemma = lemma.gsub(/7<\/sup>/,'⁷').gsub(/8<\/sup>/,'⁸').gsub(/9<\/sup>/,'⁹') + debug lemma + lemma += " #{qual} (#{uri})" + lemmas << lemma + else + debug resp.to_a.join("\r") + end + } + rescue => e + debug "Exception '#{e}' while trying to get and parse #{uri}" + m.reply "Errore" + return + end + } + pre = lemmas.length > 1 ? "Lemmi trovati" : "Lemma trovato" + m.reply "#{pre}: #{lemmas.join(' ; ')}" + end +end + +plugin = DeMauroPlugin.new +plugin.map 'demauro :parola', :action => 'demauro' + -- cgit v1.2.3