summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/demauro.rb
blob: 85f1f417c8b6cea0a621d9002adc6bcb08b636fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require 'uri'

DEMAURO_LEMMA = /<anchor>(.*?)(?: - (.*?))<go href="lemma.php\?ID=(\d+)"\/><\/anchor>/
class DeMauroPlugin < Plugin
  def initialize
    super
    @dmurl = "http://www.demauroparavia.it/"
    @wapurl = "http://wap.demauroparavia.it/"
  end


  def help(plugin, topic="")
    return "demauro <word> => provides a link to the definition of the word from the Italian dictionary De Mauro/Paravia"
  end

  def demauro(m, params)
    justcheck = params[:justcheck]

    parola = params[:parola].downcase
    url = @wapurl + "index.php?lemma=#{URI.escape(parola)}"
    xml = @bot.httputil.get_cached(url)
    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 #{parola}#{info}"
      return
    end
    if xml=~ /Non ho trovato occorrenze per/
      return false if justcheck
      m.reply "Nothing found for #{parola}"
      return
    end
    entries = xml.scan(DEMAURO_LEMMA)
    text = parola
    if !entries.assoc(parola) and !entries.assoc(parola.upcase)
      return false if justcheck
      text += " not found. Similar words"
    end
    return true if justcheck
    text += ": "
    text += entries[0...5].map { |ar|
      "#{ar[0]} - #{ar[1].gsub(/<\/?em>/,'')}: #{@dmurl}#{ar[2]}"
    }.join(" | ")
    m.reply text
  end

  def is_italian?(word)
    return demauro(nil, :parola => word, :justcheck => true)
  end

end

plugin = DeMauroPlugin.new
plugin.map 'demauro :parola', :action => 'demauro'