4 # :title: Dictionary lookup plugin for rbot
\r
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
\r
7 # Copyright:: (C) 2006-2007 Giuseppe Bilotta
\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
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
19 # TODO: cache results and reuse them if get_cached returns a cache copy
\r
21 DEMAURO_LEMMA = /<anchor>(.*?)(?: - (.*?))<go href="lemma.php\?ID=(\d+)"\/><\/anchor>/
\r
22 CHAMBERS_LEMMA = /<p><span class="hwd">(.*?)<\/span> <span class="psa">(.*?)<\/span>(.*?)<\/p>/
\r
24 class DictPlugin < Plugin
\r
25 Config.register Config::IntegerValue.new('dict.hits',
\r
27 :desc => "Number of hits to return from a dictionary lookup")
\r
28 Config.register Config::IntegerValue.new('dict.first_par',
\r
30 :desc => "When set to n > 0, the bot will return the first paragraph from the first n dictionary hits")
\r
34 @dmurl = "http://www.demauroparavia.it/"
\r
35 @dmwapurl = "http://wap.demauroparavia.it/index.php?lemma=%s"
\r
36 @dmwaplemma = "http://wap.demauroparavia.it/lemma.php?ID=%s"
\r
37 @oxurl = "http://www.askoxford.com/concise_oed/%s"
\r
38 @chambersurl = "http://www.chambersharrap.co.uk/chambers/features/chref/chref.py/main?query=%s&title=21st"
\r
42 def help(plugin, topic="")
\r
45 return "demauro <word> => provides a link to the definition of <word> from the De Mauro/Paravia dictionary"
\r
47 return "oxford <word> => provides a link to the definition of <word> (it can also be an expression) from the Concise Oxford dictionary"
\r
49 return "chambers <word> => provides a link to the definition of <word> (it can also be an expression) from the Chambers 21st Century Dictionary"
\r
51 return "<dictionary> <word>: check for <word> on <dictionary> where <dictionary> can be one of: demauro, oxford, chambers"
\r
54 def demauro(m, params)
\r
55 justcheck = params[:justcheck]
\r
57 word = params[:word].downcase
\r
58 url = @dmwapurl % CGI.escape(word)
\r
60 info = @bot.httputil.get_response(url) rescue nil
\r
61 xml = info.body if info
\r
63 info = info ? " (#{info.code} - #{info.message})" : ""
\r
64 return false if justcheck
\r
65 m.reply "An error occurred while looking for #{word}#{info}"
\r
68 if xml=~ /Non ho trovato occorrenze per/
\r
69 return false if justcheck
\r
70 m.reply "Nothing found for #{word}"
\r
73 entries = xml.scan(DEMAURO_LEMMA)
\r
76 if not entries.transpose.first.grep(/\b#{word}\b/)
\r
77 return false if justcheck
\r
78 text += " not found. Similar words"
\r
80 return true if justcheck
\r
83 hits = @bot.config['dict.hits']
\r
84 text += entries[0...hits].map { |ar|
\r
86 urls << @dmwaplemma % ar[2]
\r
87 "#{n}. #{Bold}#{ar[0]}#{Bold} - #{ar[1].gsub(/<\/?em>/,'')}: #{@dmurl}#{ar[2]}"
\r
91 first_pars = @bot.config['dict.first_par']
\r
93 return unless first_pars > 0
\r
95 Utils.get_first_pars urls, first_pars, :message => m,
\r
96 :strip => /^.+?\s+-\s+/
\r
100 def is_italian?(word)
\r
101 return demauro(nil, :word => word, :justcheck => true)
\r
105 def oxford(m, params)
\r
106 justcheck = params[:justcheck]
\r
108 word = params[:word].join
\r
109 [word, word + "_1"].each { |check|
\r
110 url = @oxurl % CGI.escape(check)
\r
111 if params[:british]
\r
114 h = @bot.httputil.get(url, :max_redir => 5)
\r
115 if h and h.match(%r!<h2>#{word}(?:<sup>1</sup>)?</h2>!)
\r
116 m.reply("#{word} : #{url}") unless justcheck
\r
118 m.reply("#{Bold}%s#{Bold}: %s" % [word, defn.ircify_html(:nbsp => :space)], :overlong => :truncate)
\r
122 return false if justcheck
\r
123 m.reply "#{word} not found"
\r
126 def is_british?(word)
\r
127 return oxford(nil, :word => word, :justcheck => true, :british => true)
\r
131 def chambers(m, params)
\r
132 justcheck = params[:justcheck]
\r
134 word = params[:word].to_s.downcase
\r
135 url = @chambersurl % CGI.escape(word)
\r
137 info = @bot.httputil.get_response(url) rescue nil
\r
138 xml = info.body if info
\r
141 info = info ? " (#{info.code} - #{info.message})" : ""
\r
142 return false if justcheck
\r
143 m.reply "An error occurred while looking for #{word}#{info}"
\r
145 when /Sorry, no entries for <b>.*?<\/b> were found./
\r
146 return false if justcheck
\r
147 m.reply "Nothing found for #{word}"
\r
149 when /No exact matches for <b>.*?<\/b>, but the following may be helpful./
\r
150 return false if justcheck
\r
151 m.reply "Nothing found for #{word}, but see #{url} for possible suggestions"
\r
154 # Else, we have a hit
\r
155 return true if justcheck
\r
156 m.reply "#{word}: #{url}"
\r
157 entries = xml.scan(CHAMBERS_LEMMA)
\r
158 hits = @bot.config['dict.hits']
\r
159 entries[0...hits].map { |ar|
\r
160 m.reply(("#{Bold}%s#{Bold} #{Underline}%s#{Underline}%s" % ar).ircify_html, :overlong => :truncate)
\r
164 def is_english?(word)
\r
165 return chambers(nil, :word => word, :justcheck => true)
\r
170 plugin = DictPlugin.new
\r
171 plugin.map 'demauro :word', :action => 'demauro'
\r
172 plugin.map 'oxford *word', :action => 'oxford'
\r
173 plugin.map 'chambers *word', :action => 'chambers'
\r