]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/demauro.rb
04722add6731f6ea633a3502035308a25398bcc3
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / demauro.rb
1 # vim: set sw=2 et:\r
2 #\r
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
7 #\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
10 #\r
11 # Author: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>\r
12 #\r
13 # TODO: cache results and reuse them if get_cached returns a cache copy\r
14 \r
15 require 'uri'\r
16 \r
17 DEMAURO_LEMMA = /<anchor>(.*?)(?: - (.*?))<go href="lemma.php\?ID=(\d+)"\/><\/anchor>/\r
18 \r
19 class DictPlugin < Plugin\r
20   def initialize\r
21     super\r
22     @dmurl = "http://www.demauroparavia.it/"\r
23     @dmwapurl = "http://wap.demauroparavia.it/"\r
24     @oxurl = "http://www.askoxford.com/concise_oed/"\r
25   end\r
26 \r
27 \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
30   end\r
31 \r
32   def demauro(m, params)\r
33     justcheck = params[:justcheck]\r
34 \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
38     if xml.nil?\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
43       return\r
44     end\r
45     if xml=~ /Non ho trovato occorrenze per/\r
46       return false if justcheck\r
47       m.reply "Nothing found for #{word}"\r
48       return\r
49     end\r
50     entries = xml.scan(DEMAURO_LEMMA)\r
51     text = word\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
55     end\r
56     return true if justcheck\r
57     text += ": "\r
58     text += entries[0...5].map { |ar|\r
59       "#{ar[0]} - #{ar[1].gsub(/<\/?em>/,'')}: #{@dmurl}#{ar[2]}"\r
60     }.join(" | ")\r
61     m.reply text\r
62   end\r
63 \r
64   def is_italian?(word)\r
65     return demauro(nil, :word => word, :justcheck => true)\r
66   end\r
67 \r
68 \r
69   def oxford(m, params)\r
70     justcheck = params[:justcheck]\r
71 \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
76       if h\r
77         m.reply("#{word} found: #{url}") unless justcheck\r
78         return true\r
79       end\r
80     }\r
81     return false if justcheck\r
82     m.reply "#{word} not found"\r
83   end\r
84 \r
85   def is_british?(word)\r
86     return oxford(nil, :word => word, :justcheck => true)\r
87   end\r
88 \r
89 end\r
90 \r
91 plugin = DictPlugin.new\r
92 plugin.map 'demauro :word', :action => 'demauro'\r
93 plugin.map 'oxford :word', :action => 'oxford'\r
94 \r