diff options
author | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2006-10-25 14:07:43 +0000 |
---|---|---|
committer | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2006-10-25 14:07:43 +0000 |
commit | 87bba678599323e8bd66ea63ac54c6bcb70ccf07 (patch) | |
tree | a4507e54744dd66c110f2a98711d2f982bda3537 | |
parent | 508744d7063ae7f519b0bd490bd421c581604f1f (diff) |
Renamen demauro to dict, add chambers dictionary
-rw-r--r-- | data/rbot/plugins/demauro.rb | 94 | ||||
-rw-r--r-- | data/rbot/plugins/dict.rb | 136 |
2 files changed, 136 insertions, 94 deletions
diff --git a/data/rbot/plugins/demauro.rb b/data/rbot/plugins/demauro.rb deleted file mode 100644 index 04722add..00000000 --- a/data/rbot/plugins/demauro.rb +++ /dev/null @@ -1,94 +0,0 @@ -# vim: set sw=2 et:
-#
-# dict plugin: provides a link to the definition of a word in one of the supported
-# dictionaries. Currently available are
-# * the Oxford dictionary for (British) English
-# * the De Mauro/Paravia dictionary for Italian
-#
-# other plugins can use this one to check if a given word is valid in italian
-# or british english by using the is_italian?/is_british? methods
-#
-# Author: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
-#
-# TODO: cache results and reuse them if get_cached returns a cache copy
-
-require 'uri'
-
-DEMAURO_LEMMA = /<anchor>(.*?)(?: - (.*?))<go href="lemma.php\?ID=(\d+)"\/><\/anchor>/
-
-class DictPlugin < Plugin
- def initialize
- super
- @dmurl = "http://www.demauroparavia.it/"
- @dmwapurl = "http://wap.demauroparavia.it/"
- @oxurl = "http://www.askoxford.com/concise_oed/"
- end
-
-
- def help(plugin, topic="")
- return "demauro <word> => provides a link to the definition of the word from the Italian dictionary De Mauro/Paravia"
- end
-
- def demauro(m, params)
- justcheck = params[:justcheck]
-
- word = params[:word].downcase
- url = @dmwapurl + "index.php?lemma=#{URI.escape(word)}"
- xml = @bot.httputil.get_cached(url)
- if xml.nil?
- info = @bot.httputil.last_response
- info = info ? "(#{info.code} - #{info.message})" : ""
- return false if justcheck
- m.reply "An error occurred while looking for #{word}#{info}"
- return
- end
- if xml=~ /Non ho trovato occorrenze per/
- return false if justcheck
- m.reply "Nothing found for #{word}"
- return
- end
- entries = xml.scan(DEMAURO_LEMMA)
- text = word
- if !entries.assoc(word) and !entries.assoc(word.upcase)
- return false if justcheck
- text += " not found. Similar words"
- end
- return true if justcheck
- text += ": "
- text += entries[0...5].map { |ar|
- "#{ar[0]} - #{ar[1].gsub(/<\/?em>/,'')}: #{@dmurl}#{ar[2]}"
- }.join(" | ")
- m.reply text
- end
-
- def is_italian?(word)
- return demauro(nil, :word => word, :justcheck => true)
- end
-
-
- def oxford(m, params)
- justcheck = params[:justcheck]
-
- word = params[:word].downcase.gsub(/\s+/,'')
- [word, word + "_1"].each { |check|
- url = @oxurl + "#{URI.escape(check)}"
- h = @bot.httputil.head(url)
- if h
- m.reply("#{word} found: #{url}") unless justcheck
- return true
- end
- }
- return false if justcheck
- m.reply "#{word} not found"
- end
-
- def is_british?(word)
- return oxford(nil, :word => word, :justcheck => true)
- end
-
-end
-
-plugin = DictPlugin.new
-plugin.map 'demauro :word', :action => 'demauro'
-plugin.map 'oxford :word', :action => 'oxford'
-
diff --git a/data/rbot/plugins/dict.rb b/data/rbot/plugins/dict.rb new file mode 100644 index 00000000..d9fafa5c --- /dev/null +++ b/data/rbot/plugins/dict.rb @@ -0,0 +1,136 @@ +# vim: set sw=2 et:
+#
+# dict plugin: provides a link to the definition of a word in one of the supported
+# dictionaries. Currently available are
+# * the Oxford dictionary for (British) English
+# * the De Mauro/Paravia dictionary for Italian
+# * the Chambers dictionary for English (accepts both US and UK)
+#
+# other plugins can use this one to check if a given word is valid in italian
+# or english by using the is_italian?/is_british?/is_english? methods
+#
+# Author: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
+#
+# TODO: cache results and reuse them if get_cached returns a cache copy
+
+require 'uri'
+
+DEMAURO_LEMMA = /<anchor>(.*?)(?: - (.*?))<go href="lemma.php\?ID=(\d+)"\/><\/anchor>/
+
+class DictPlugin < Plugin
+ def initialize
+ super
+ @dmurl = "http://www.demauroparavia.it/"
+ @dmwapurl = "http://wap.demauroparavia.it/index.php?lemma=%s"
+ @oxurl = "http://www.askoxford.com/concise_oed/%s"
+ @chambersurl = "http://www.chambersharrap.co.uk/chambers/features/chref/chref.py/main?query=%s&title=21st"
+ end
+
+
+ def help(plugin, topic="")
+ case topic
+ when "demauro"
+ return "demauro <word> => provides a link to the definition of <word> from the De Mauro/Paravia dictionary"
+ when "oxford"
+ return "oxford <word> => provides a link to the definition of <word> (it can also be an expression) from the Concise Oxford dictionary"
+ when "chambers"
+ return "chambers <word> => provides a link to the definition of <word> (it can also be an expression) from the Chambers 21st Century Dictionary"
+ end
+ return "<dictionary> <word>: check for <word> on <dictionary> where <dictionary> can be one of: demauro, oxford, chambers"
+ end
+
+ def demauro(m, params)
+ justcheck = params[:justcheck]
+
+ word = params[:word].downcase
+ url = @dmwapurl % URI.escape(word)
+ xml = @bot.httputil.get_cached(url)
+ if xml.nil?
+ info = @bot.httputil.last_response
+ info = info ? " (#{info.code} - #{info.message})" : ""
+ return false if justcheck
+ m.reply "An error occurred while looking for #{word}#{info}"
+ return
+ end
+ if xml=~ /Non ho trovato occorrenze per/
+ return false if justcheck
+ m.reply "Nothing found for #{word}"
+ return
+ end
+ entries = xml.scan(DEMAURO_LEMMA)
+ text = word
+ if !entries.assoc(word) and !entries.assoc(word.upcase)
+ return false if justcheck
+ text += " not found. Similar words"
+ end
+ return true if justcheck
+ text += ": "
+ text += entries[0...5].map { |ar|
+ "#{ar[0]} - #{ar[1].gsub(/<\/?em>/,'')}: #{@dmurl}#{ar[2]}"
+ }.join(" | ")
+ m.reply text
+ end
+
+ def is_italian?(word)
+ return demauro(nil, :word => word, :justcheck => true)
+ end
+
+
+ def oxford(m, params)
+ justcheck = params[:justcheck]
+
+ word = params[:word].join
+ [word, word + "_1"].each { |check|
+ url = @oxurl % URI.escape(check)
+ h = @bot.httputil.head(url)
+ if h
+ m.reply("#{word} found: #{url}") unless justcheck
+ return true
+ end
+ }
+ return false if justcheck
+ m.reply "#{word} not found"
+ end
+
+ def is_british?(word)
+ return oxford(nil, :word => word, :justcheck => true)
+ end
+
+
+ def chambers(m, params)
+ justcheck = params[:justcheck]
+
+ word = params[:word].to_s.downcase
+ url = @chambersurl % URI.escape(word)
+ xml = @bot.httputil.get_cached(url)
+ case xml
+ when nil
+ info = @bot.httputil.last_response
+ info = info ? " (#{info.code} - #{info.message})" : ""
+ return false if justcheck
+ m.reply "An error occurred while looking for #{word}#{info}"
+ return
+ when /Sorry, no entries for <b>.*?<\/b> were found./
+ return false if justcheck
+ m.reply "Nothing found for #{word}"
+ return
+ when /No exact matches for <b>.*?<\/b>, but the following may be helpful./
+ return false if justcheck
+ m.reply "Nothing found for #{word}, but see #{url} for possible suggestions"
+ else
+ return false if justcheck
+ m.reply "#{word}: #{url}"
+ end
+ end
+
+ def is_english?(word)
+ return chambers(nil, :word => word, :justcheck => true)
+ end
+
+end
+
+plugin = DictPlugin.new
+plugin.map 'demauro :word', :action => 'demauro'
+plugin.map 'oxford *word', :action => 'oxford'
+plugin.map 'chambers *word', :action => 'chambers'
+
|