diff options
author | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2007-12-24 14:01:42 +0000 |
---|---|---|
committer | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2007-12-24 14:01:42 +0000 |
commit | 9a565bfd48c59ac9c1b37d5afe9b8640889632ff (patch) | |
tree | 9697d60ca1eb874cfaa6cf9893092e956f767ec8 | |
parent | fb81f00c698e7d897ec9ba2186eb33b007c3aa87 (diff) |
factoids plugin: 'facts about' searches whole words, 'facts search' uses regular expressions, keyword-style '?' queries act like 'facts about'
-rw-r--r-- | data/rbot/plugins/factoids.rb | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/data/rbot/plugins/factoids.rb b/data/rbot/plugins/factoids.rb index 0b0913a3..59a38aba 100644 --- a/data/rbot/plugins/factoids.rb +++ b/data/rbot/plugins/factoids.rb @@ -280,12 +280,23 @@ class FactoidsPlugin < Plugin }) end + def words2rx(words) + # When looking for words we separate them with + # arbitrary whitespace, not whatever they came with + pre = words.map { |w| Regexp.escape(w)}.join("\\s+") + return Regexp.new("\\b#{pre}\\b", true) + end + def facts(m, params) total = @factoids.length - if params[:words].empty? + if params[:words].empty? and params[:rx].empty? m.reply _("I know %{total} facts" % { :total => total }) else - rx = Regexp.new(params[:words].to_s, true) + if params[:words].empty? + rx = Regexp.new(params[:rx].to_s, true) + else + rx = words2rx(params[:words]) + end known = @factoids.grep(rx) reply = [] if known.empty? @@ -319,7 +330,7 @@ class FactoidsPlugin < Plugin return unless m.message =~ /^(.*)\?\s*$/ query = $1.strip.downcase if @triggers.include?(query) - facts(m, :words => query) + facts(m, :words => query.split) end end @@ -343,7 +354,7 @@ class FactoidsPlugin < Plugin end known = @factoids else - rx = Regexp.new(params[:words].to_s, true) + rx = words2rx(params[:words]) known = @factoids.grep(rx) if known.empty? m.reply _("I know nothing about %{words}" % params) @@ -424,6 +435,7 @@ plugin.map 'learn that *stuff' plugin.map 'forget that *stuff', :auth_path => 'edit' plugin.map 'forget fact :index', :requirements => { :index => /^#?\d+$/ }, :auth_path => 'edit' plugin.map 'facts [about *words]' +plugin.map 'facts search *rx' plugin.map 'fact [about *words]' plugin.map 'fact :index', :requirements => { :index => /^#?\d+$/ } |