]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/dict.rb
plugins: remove excess requires and Net::HTTP.version setups
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / dict.rb
1 #-- vim:sw=2:et\r
2 #++\r
3 #\r
4 # :title: Dictionary lookup plugin for rbot\r
5 #\r
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>\r
7 # Copyright:: (C) 2006-2007 Giuseppe Bilotta\r
8 # License:: GPL v2\r
9 #\r
10 # Provides a link to the definition of a word in one of the supported\r
11 # dictionaries. Currently available are\r
12 #   * the Oxford dictionary for (British) English\r
13 #   * the De Mauro/Paravia dictionary for Italian\r
14 #   * the Chambers dictionary for English (accepts both US and UK)\r
15 #\r
16 # Other plugins can use this one to check if a given word is valid in italian\r
17 # or english by using the is_italian?/is_british?/is_english? methods\r
18 #\r
19 # TODO: cache results and reuse them if get_cached returns a cache copy\r
20 \r
21 DEMAURO_LEMMA = /<anchor>(.*?)(?: - (.*?))<go href="lemma.php\?ID=(\d+)"\/><\/anchor>/\r
22 \r
23 class DictPlugin < Plugin\r
24   BotConfig.register BotConfigIntegerValue.new('dict.hits',\r
25     :default => 3,\r
26     :desc => "Number of hits to return from a dictionary lookup")\r
27   BotConfig.register BotConfigIntegerValue.new('dict.first_par',\r
28     :default => 0,\r
29     :desc => "When set to n > 0, the bot will return the first paragraph from the first n dictionary hits")\r
30 \r
31   def initialize\r
32     super\r
33     @dmurl = "http://www.demauroparavia.it/"\r
34     @dmwapurl = "http://wap.demauroparavia.it/index.php?lemma=%s"\r
35     @dmwaplemma = "http://wap.demauroparavia.it/lemma.php?ID=%s"\r
36     @oxurl = "http://www.askoxford.com/concise_oed/%s"\r
37     @chambersurl = "http://www.chambersharrap.co.uk/chambers/features/chref/chref.py/main?query=%s&title=21st"\r
38   end\r
39 \r
40 \r
41   def help(plugin, topic="")\r
42     case topic\r
43     when "demauro"\r
44       return "demauro <word> => provides a link to the definition of <word> from the De Mauro/Paravia dictionary"\r
45     when "oxford"\r
46       return "oxford <word> => provides a link to the definition of <word> (it can also be an expression) from the Concise Oxford dictionary"\r
47     when "chambers"\r
48       return "chambers <word> => provides a link to the definition of <word> (it can also be an expression) from the Chambers 21st Century Dictionary"\r
49     end\r
50     return "<dictionary> <word>: check for <word> on <dictionary> where <dictionary> can be one of: demauro, oxford, chambers"\r
51   end\r
52 \r
53   def demauro(m, params)\r
54     justcheck = params[:justcheck]\r
55 \r
56     word = params[:word].downcase\r
57     url = @dmwapurl % URI.escape(word)\r
58     xml = nil\r
59     info = @bot.httputil.get_response(url) rescue nil\r
60     xml = info.body if info\r
61     if xml.nil?\r
62       info = info ? " (#{info.code} - #{info.message})" : ""\r
63       return false if justcheck\r
64       m.reply "An error occurred while looking for #{word}#{info}"\r
65       return\r
66     end\r
67     if xml=~ /Non ho trovato occorrenze per/\r
68       return false if justcheck\r
69       m.reply "Nothing found for #{word}"\r
70       return\r
71     end\r
72     entries = xml.scan(DEMAURO_LEMMA)\r
73     text = word\r
74     urls = []\r
75     if !entries.assoc(word) and !entries.assoc(word.upcase)\r
76       return false if justcheck\r
77       text += " not found. Similar words"\r
78     end\r
79     return true if justcheck\r
80     text += ": "\r
81     n = 0\r
82     hits = @bot.config['dict.hits']\r
83     text += entries[0...hits].map { |ar|\r
84       n += 1\r
85       urls << @dmwaplemma % ar[2]\r
86       "#{n}. #{Bold}#{ar[0]}#{Bold} - #{ar[1].gsub(/<\/?em>/,'')}: #{@dmurl}#{ar[2]}"\r
87     }.join(" | ")\r
88     m.reply text\r
89 \r
90     first_pars = @bot.config['dict.first_par']\r
91 \r
92     return unless first_pars > 0\r
93 \r
94     Utils.get_first_pars urls, first_pars, :message => m,\r
95       :strip => /^\S+\s+-\s+/\r
96 \r
97   end\r
98 \r
99   def is_italian?(word)\r
100     return demauro(nil, :word => word, :justcheck => true)\r
101   end\r
102 \r
103 \r
104   def oxford(m, params)\r
105     justcheck = params[:justcheck]\r
106 \r
107     word = params[:word].join\r
108     [word, word + "_1"].each { |check|\r
109       url = @oxurl % URI.escape(check)\r
110       h = @bot.httputil.head(url, :max_redir => 5)\r
111       if h\r
112         m.reply("#{word} found: #{url}") unless justcheck\r
113         return true\r
114       end\r
115     }\r
116     return false if justcheck\r
117     m.reply "#{word} not found"\r
118   end\r
119 \r
120   def is_british?(word)\r
121     return oxford(nil, :word => word, :justcheck => true)\r
122   end\r
123 \r
124 \r
125   def chambers(m, params)\r
126     justcheck = params[:justcheck]\r
127 \r
128     word = params[:word].to_s.downcase\r
129     url = @chambersurl % URI.escape(word)\r
130     xml = nil\r
131     info = @bot.httputil.get_response(url) rescue nil\r
132     xml = info.body if info\r
133     case xml\r
134     when nil\r
135       info = info ? " (#{info.code} - #{info.message})" : ""\r
136       return false if justcheck\r
137       m.reply "An error occurred while looking for #{word}#{info}"\r
138       return\r
139     when /Sorry, no entries for <b>.*?<\/b> were found./\r
140       return false if justcheck\r
141       m.reply "Nothing found for #{word}"\r
142       return\r
143     when /No exact matches for <b>.*?<\/b>, but the following may be helpful./\r
144       return false if justcheck\r
145       m.reply "Nothing found for #{word}, but see #{url} for possible suggestions"\r
146     else\r
147       return true if justcheck\r
148       m.reply "#{word}: #{url}"\r
149     end\r
150   end\r
151 \r
152   def is_english?(word)\r
153     return chambers(nil, :word => word, :justcheck => true)\r
154   end\r
155 \r
156 end\r
157 \r
158 plugin = DictPlugin.new\r
159 plugin.map 'demauro :word', :action => 'demauro'\r
160 plugin.map 'oxford *word', :action => 'oxford'\r
161 plugin.map 'chambers *word', :action => 'chambers'\r
162 \r