]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/dict.rb
shortenurls plugin, merging tinyurl and rubyurl and adding access to most of ShortURL...
[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   BotConfig.register BotConfigIntegerValue.new('dict.hits',\r
22     :default => 3,\r
23     :desc => "Number of hits to return from a dictionary lookup")\r
24   BotConfig.register BotConfigIntegerValue.new('dict.first_par',\r
25     :default => 0,\r
26     :desc => "When set to n > 0, the bot will return the first paragraph from the first n dictionary hits")\r
27 \r
28   def initialize\r
29     super\r
30     @dmurl = "http://www.demauroparavia.it/"\r
31     @dmwapurl = "http://wap.demauroparavia.it/index.php?lemma=%s"\r
32     @dmwaplemma = "http://wap.demauroparavia.it/lemma.php?ID=%s"\r
33     @oxurl = "http://www.askoxford.com/concise_oed/%s"\r
34     @chambersurl = "http://www.chambersharrap.co.uk/chambers/features/chref/chref.py/main?query=%s&title=21st"\r
35   end\r
36 \r
37 \r
38   def help(plugin, topic="")\r
39     case topic\r
40     when "demauro"\r
41       return "demauro <word> => provides a link to the definition of <word> from the De Mauro/Paravia dictionary"\r
42     when "oxford"\r
43       return "oxford <word> => provides a link to the definition of <word> (it can also be an expression) from the Concise Oxford dictionary"\r
44     when "chambers"\r
45       return "chambers <word> => provides a link to the definition of <word> (it can also be an expression) from the Chambers 21st Century Dictionary"\r
46     end\r
47     return "<dictionary> <word>: check for <word> on <dictionary> where <dictionary> can be one of: demauro, oxford, chambers"\r
48   end\r
49 \r
50   def demauro(m, params)\r
51     justcheck = params[:justcheck]\r
52 \r
53     word = params[:word].downcase\r
54     url = @dmwapurl % URI.escape(word)\r
55     xml = @bot.httputil.get_cached(url)\r
56     if xml.nil?\r
57       info = @bot.httputil.last_response\r
58       info = info ? " (#{info.code} - #{info.message})" : ""\r
59       return false if justcheck\r
60       m.reply "An error occurred while looking for #{word}#{info}"\r
61       return\r
62     end\r
63     if xml=~ /Non ho trovato occorrenze per/\r
64       return false if justcheck\r
65       m.reply "Nothing found for #{word}"\r
66       return\r
67     end\r
68     entries = xml.scan(DEMAURO_LEMMA)\r
69     text = word\r
70     urls = []\r
71     if !entries.assoc(word) and !entries.assoc(word.upcase)\r
72       return false if justcheck\r
73       text += " not found. Similar words"\r
74     end\r
75     return true if justcheck\r
76     text += ": "\r
77     n = 0\r
78     hits = @bot.config['dict.hits']\r
79     text += entries[0...hits].map { |ar|\r
80       n += 1\r
81       urls << @dmwaplemma % ar[2]\r
82       "#{n}. #{Bold}#{ar[0]}#{Bold} - #{ar[1].gsub(/<\/?em>/,'')}: #{@dmurl}#{ar[2]}"\r
83     }.join(" | ")\r
84     m.reply text\r
85 \r
86     first_pars = @bot.config['dict.first_par']\r
87 \r
88     return unless first_pars > 0\r
89 \r
90     Utils.get_first_pars urls, first_pars, :http_util => @bot.httputil, :message => m,\r
91       :strip => /^\S+\s+-\s+/\r
92 \r
93   end\r
94 \r
95   def is_italian?(word)\r
96     return demauro(nil, :word => word, :justcheck => true)\r
97   end\r
98 \r
99 \r
100   def oxford(m, params)\r
101     justcheck = params[:justcheck]\r
102 \r
103     word = params[:word].join\r
104     [word, word + "_1"].each { |check|\r
105       url = @oxurl % URI.escape(check)\r
106       h = @bot.httputil.head(url)\r
107       if h\r
108         m.reply("#{word} found: #{url}") unless justcheck\r
109         return true\r
110       end\r
111     }\r
112     return false if justcheck\r
113     m.reply "#{word} not found"\r
114   end\r
115 \r
116   def is_british?(word)\r
117     return oxford(nil, :word => word, :justcheck => true)\r
118   end\r
119 \r
120 \r
121   def chambers(m, params)\r
122     justcheck = params[:justcheck]\r
123 \r
124     word = params[:word].to_s.downcase\r
125     url = @chambersurl % URI.escape(word)\r
126     xml = @bot.httputil.get_cached(url)\r
127     case xml\r
128     when nil\r
129       info = @bot.httputil.last_response\r
130       info = info ? " (#{info.code} - #{info.message})" : ""\r
131       return false if justcheck\r
132       m.reply "An error occurred while looking for #{word}#{info}"\r
133       return\r
134     when /Sorry, no entries for <b>.*?<\/b> were found./\r
135       return false if justcheck\r
136       m.reply "Nothing found for #{word}"\r
137       return\r
138     when /No exact matches for <b>.*?<\/b>, but the following may be helpful./\r
139       return false if justcheck\r
140       m.reply "Nothing found for #{word}, but see #{url} for possible suggestions"\r
141     else\r
142       return true if justcheck\r
143       m.reply "#{word}: #{url}"\r
144     end\r
145   end\r
146 \r
147   def is_english?(word)\r
148     return chambers(nil, :word => word, :justcheck => true)\r
149   end\r
150 \r
151 end\r
152 \r
153 plugin = DictPlugin.new\r
154 plugin.map 'demauro :word', :action => 'demauro'\r
155 plugin.map 'oxford *word', :action => 'oxford'\r
156 plugin.map 'chambers *word', :action => 'chambers'\r
157 \r