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
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
12 # Author: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
\r
14 # TODO: cache results and reuse them if get_cached returns a cache copy
\r
18 DEMAURO_LEMMA = /<anchor>(.*?)(?: - (.*?))<go href="lemma.php\?ID=(\d+)"\/><\/anchor>/
\r
20 class DictPlugin < Plugin
\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
30 def help(plugin, topic="")
\r
33 return "demauro <word> => provides a link to the definition of <word> from the De Mauro/Paravia dictionary"
\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
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
39 return "<dictionary> <word>: check for <word> on <dictionary> where <dictionary> can be one of: demauro, oxford, chambers"
\r
42 def demauro(m, params)
\r
43 justcheck = params[:justcheck]
\r
45 word = params[:word].downcase
\r
46 url = @dmwapurl % URI.escape(word)
\r
47 xml = @bot.httputil.get_cached(url)
\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
55 if xml=~ /Non ho trovato occorrenze per/
\r
56 return false if justcheck
\r
57 m.reply "Nothing found for #{word}"
\r
60 entries = xml.scan(DEMAURO_LEMMA)
\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
66 return true if justcheck
\r
68 text += entries[0...5].map { |ar|
\r
69 "#{ar[0]} - #{ar[1].gsub(/<\/?em>/,'')}: #{@dmurl}#{ar[2]}"
\r
74 def is_italian?(word)
\r
75 return demauro(nil, :word => word, :justcheck => true)
\r
79 def oxford(m, params)
\r
80 justcheck = params[:justcheck]
\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
87 m.reply("#{word} found: #{url}") unless justcheck
\r
91 return false if justcheck
\r
92 m.reply "#{word} not found"
\r
95 def is_british?(word)
\r
96 return oxford(nil, :word => word, :justcheck => true)
\r
100 def chambers(m, params)
\r
101 justcheck = params[:justcheck]
\r
103 word = params[:word].to_s.downcase
\r
104 url = @chambersurl % URI.escape(word)
\r
105 xml = @bot.httputil.get_cached(url)
\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
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
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
121 return false if justcheck
\r
122 m.reply "#{word}: #{url}"
\r
126 def is_english?(word)
\r
127 return chambers(nil, :word => word, :justcheck => true)
\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