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