3 # dict plugin: provides a link to the definition of a word in one of the supported
\r
4 # dictionaries. Currently available are
\r
5 # * the Oxford dictionary for (British) English
\r
6 # * the De Mauro/Paravia dictionary for Italian
\r
8 # other plugins can use this one to check if a given word is valid in italian
\r
9 # or british english by using the is_italian?/is_british? methods
\r
11 # Author: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
\r
13 # TODO: cache results and reuse them if get_cached returns a cache copy
\r
17 DEMAURO_LEMMA = /<anchor>(.*?)(?: - (.*?))<go href="lemma.php\?ID=(\d+)"\/><\/anchor>/
\r
19 class DictPlugin < Plugin
\r
22 @dmurl = "http://www.demauroparavia.it/"
\r
23 @dmwapurl = "http://wap.demauroparavia.it/"
\r
24 @oxurl = "http://www.askoxford.com/concise_oed/"
\r
28 def help(plugin, topic="")
\r
29 return "demauro <word> => provides a link to the definition of the word from the Italian dictionary De Mauro/Paravia"
\r
32 def demauro(m, params)
\r
33 justcheck = params[:justcheck]
\r
35 word = params[:word].downcase
\r
36 url = @dmwapurl + "index.php?lemma=#{URI.escape(word)}"
\r
37 xml = @bot.httputil.get_cached(url)
\r
39 info = @bot.httputil.last_response
\r
40 info = info ? "(#{info.code} - #{info.message})" : ""
\r
41 return false if justcheck
\r
42 m.reply "An error occurred while looking for #{word}#{info}"
\r
45 if xml=~ /Non ho trovato occorrenze per/
\r
46 return false if justcheck
\r
47 m.reply "Nothing found for #{word}"
\r
50 entries = xml.scan(DEMAURO_LEMMA)
\r
52 if !entries.assoc(word) and !entries.assoc(word.upcase)
\r
53 return false if justcheck
\r
54 text += " not found. Similar words"
\r
56 return true if justcheck
\r
58 text += entries[0...5].map { |ar|
\r
59 "#{ar[0]} - #{ar[1].gsub(/<\/?em>/,'')}: #{@dmurl}#{ar[2]}"
\r
64 def is_italian?(word)
\r
65 return demauro(nil, :word => word, :justcheck => true)
\r
69 def oxford(m, params)
\r
70 justcheck = params[:justcheck]
\r
72 word = params[:word].downcase.gsub(/\s+/,'')
\r
73 [word, word + "_1"].each { |check|
\r
74 url = @oxurl + "#{URI.escape(check)}"
\r
75 h = @bot.httputil.head(url)
\r
77 m.reply("#{word} found: #{url}") unless justcheck
\r
81 return false if justcheck
\r
82 m.reply "#{word} not found"
\r
85 def is_british?(word)
\r
86 return oxford(nil, :word => word, :justcheck => true)
\r
91 plugin = DictPlugin.new
\r
92 plugin.map 'demauro :word', :action => 'demauro'
\r
93 plugin.map 'oxford :word', :action => 'oxford'
\r