]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/dict.rb
Renamen demauro to dict, add chambers dictionary
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / dict.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 #   * the Chambers dictionary for English (accepts both US and UK)\r
8 #\r
9 # other plugins can use this one to check if a given word is valid in italian\r
10 # or english by using the is_italian?/is_british?/is_english? methods\r
11 #\r
12 # Author: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>\r
13 #\r
14 # TODO: cache results and reuse them if get_cached returns a cache copy\r
15 \r
16 require 'uri'\r
17 \r
18 DEMAURO_LEMMA = /<anchor>(.*?)(?: - (.*?))<go href="lemma.php\?ID=(\d+)"\/><\/anchor>/\r
19 \r
20 class DictPlugin < Plugin\r
21   def initialize\r
22     super\r
23     @dmurl = "http://www.demauroparavia.it/"\r
24     @dmwapurl = "http://wap.demauroparavia.it/index.php?lemma=%s"\r
25     @oxurl = "http://www.askoxford.com/concise_oed/%s"\r
26     @chambersurl = "http://www.chambersharrap.co.uk/chambers/features/chref/chref.py/main?query=%s&title=21st"\r
27   end\r
28 \r
29 \r
30   def help(plugin, topic="")\r
31     case topic\r
32     when "demauro"\r
33       return "demauro <word> => provides a link to the definition of <word> from the De Mauro/Paravia dictionary"\r
34     when "oxford"\r
35       return "oxford <word> => provides a link to the definition of <word> (it can also be an expression) from the Concise Oxford dictionary"\r
36     when "chambers"\r
37       return "chambers <word> => provides a link to the definition of <word> (it can also be an expression) from the Chambers 21st Century Dictionary"\r
38     end\r
39     return "<dictionary> <word>: check for <word> on <dictionary> where <dictionary> can be one of: demauro, oxford, chambers"\r
40   end\r
41 \r
42   def demauro(m, params)\r
43     justcheck = params[:justcheck]\r
44 \r
45     word = params[:word].downcase\r
46     url = @dmwapurl % URI.escape(word)\r
47     xml = @bot.httputil.get_cached(url)\r
48     if xml.nil?\r
49       info = @bot.httputil.last_response\r
50       info = info ? " (#{info.code} - #{info.message})" : ""\r
51       return false if justcheck\r
52       m.reply "An error occurred while looking for #{word}#{info}"\r
53       return\r
54     end\r
55     if xml=~ /Non ho trovato occorrenze per/\r
56       return false if justcheck\r
57       m.reply "Nothing found for #{word}"\r
58       return\r
59     end\r
60     entries = xml.scan(DEMAURO_LEMMA)\r
61     text = word\r
62     if !entries.assoc(word) and !entries.assoc(word.upcase)\r
63       return false if justcheck\r
64       text += " not found. Similar words"\r
65     end\r
66     return true if justcheck\r
67     text += ": "\r
68     text += entries[0...5].map { |ar|\r
69       "#{ar[0]} - #{ar[1].gsub(/<\/?em>/,'')}: #{@dmurl}#{ar[2]}"\r
70     }.join(" | ")\r
71     m.reply text\r
72   end\r
73 \r
74   def is_italian?(word)\r
75     return demauro(nil, :word => word, :justcheck => true)\r
76   end\r
77 \r
78 \r
79   def oxford(m, params)\r
80     justcheck = params[:justcheck]\r
81 \r
82     word = params[:word].join\r
83     [word, word + "_1"].each { |check|\r
84       url = @oxurl % URI.escape(check)\r
85       h = @bot.httputil.head(url)\r
86       if h\r
87         m.reply("#{word} found: #{url}") unless justcheck\r
88         return true\r
89       end\r
90     }\r
91     return false if justcheck\r
92     m.reply "#{word} not found"\r
93   end\r
94 \r
95   def is_british?(word)\r
96     return oxford(nil, :word => word, :justcheck => true)\r
97   end\r
98 \r
99 \r
100   def chambers(m, params)\r
101     justcheck = params[:justcheck]\r
102 \r
103     word = params[:word].to_s.downcase\r
104     url = @chambersurl % URI.escape(word)\r
105     xml = @bot.httputil.get_cached(url)\r
106     case xml\r
107     when nil\r
108       info = @bot.httputil.last_response\r
109       info = info ? " (#{info.code} - #{info.message})" : ""\r
110       return false if justcheck\r
111       m.reply "An error occurred while looking for #{word}#{info}"\r
112       return\r
113     when /Sorry, no entries for <b>.*?<\/b> were found./\r
114       return false if justcheck\r
115       m.reply "Nothing found for #{word}"\r
116       return\r
117     when /No exact matches for <b>.*?<\/b>, but the following may be helpful./\r
118       return false if justcheck\r
119       m.reply "Nothing found for #{word}, but see #{url} for possible suggestions"\r
120     else\r
121       return false if justcheck\r
122       m.reply "#{word}: #{url}"\r
123     end\r
124   end\r
125 \r
126   def is_english?(word)\r
127     return chambers(nil, :word => word, :justcheck => true)\r
128   end\r
129 \r
130 end\r
131 \r
132 plugin = DictPlugin.new\r
133 plugin.map 'demauro :word', :action => 'demauro'\r
134 plugin.map 'oxford *word', :action => 'oxford'\r
135 plugin.map 'chambers *word', :action => 'chambers'\r
136 \r