From c32e315dd6ed7899c8e4bf2fe70e904ef0e86317 Mon Sep 17 00:00:00 2001 From: Abhinav Sarkar Date: Tue, 7 Jun 2011 18:36:20 +0530 Subject: [PATCH] Added support for searching by prv msg --- src/irc_search_bot/bot.clj | 14 +++++++++----- src/irc_search_bot/core.clj | 36 +++++++++++++++++++++++++++++------ src/irc_search_bot/lucene.clj | 5 +++-- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/irc_search_bot/bot.clj b/src/irc_search_bot/bot.clj index e8335a5..6f85db0 100644 --- a/src/irc_search_bot/bot.clj +++ b/src/irc_search_bot/bot.clj @@ -1,5 +1,5 @@ (ns irc-search-bot.bot - (:import [org.pircbotx PircBotX Channel] + (:import [org.pircbotx PircBotX Channel User] [org.pircbotx.hooks Event Listener]) (:use [clojure.string :only [join lower-case]])) @@ -53,7 +53,11 @@ (defn send-message [^PircBotX bot channel message] (doto bot (.sendMessage - (if (instance? Channel channel) - channel - (.getChannel bot channel)) - message))) \ No newline at end of file + (if (instance? Channel channel) channel (.getChannel bot channel)) + message))) + +(defn send-prv-message [^PircBotX bot user message] + (doto bot + (.sendMessage + (if (instance? User user) user (.getUser bot user)) + message))) diff --git a/src/irc_search_bot/core.clj b/src/irc_search_bot/core.clj index 389a63f..d9fdcea 100644 --- a/src/irc_search_bot/core.clj +++ b/src/irc_search_bot/core.clj @@ -19,7 +19,9 @@ (stemmer-analyzer (standard-analyzer)) #{"message"})) -(def *max-hits* 3) +(def *msg-max-hits* 3) + +(def *prv-msg-max-hits* 5) (def *ignored-users* (if (.exists (as-file "ignored_users")) @@ -42,7 +44,7 @@ (let [qp (query-parser :message analyzer) raw-query (parse-query qp query-str) [query filter] (filterify-query raw-query #{"user"}) - [total hits] (search index-searcher query filter max-hits)] + [total hits] (search index-searcher query filter max-hits "timestamp")] (println "Query:" query) (println "Filter:" filter) (println ">>" total "hits for query:" query-str) @@ -86,21 +88,43 @@ (let [msg (trim (.getMessage ev)) user (.. ev getUser getNick) timestamp (.getTimestamp ev) - channel (.getChannel ev)] + channel (.getChannel ev) + query (trim (subs msg 2))] (if (.startsWith msg "!q") (with-open [is (index-searcher (index-dir bot))] - (let [[total results] (search-chat-log is (trim (subs msg 2)) *max-hits* *analyzer*)] + (let [[total results] + (search-chat-log is query *msg-max-hits* *analyzer*)] (if (zero? total) - (send-message bot channel "No results found") + (send-message bot channel (format "No results found for \"%s\"" query)) (do (send-message bot channel - (str total " results found. Top " (count results) " results:")) + (format "%s results found for \"%s\". Top %s results:" + total query (count results))) (doseq [result results] (send-message bot channel result)))))) (when (and (not (.startsWith msg "!")) (not (*ignored-users* user))) (swap! *chat-log* conj [timestamp user msg]))))) +(defmethod event-listener :private-message [^PircBotX bot ^Event ev] + (let [msg (trim (.getMessage ev)) + user (.. ev getUser getNick) + timestamp (.getTimestamp ev) + query (trim (subs msg 2))] + (when (.startsWith msg "!q") + (with-open [is (index-searcher (index-dir bot))] + (let [[total results] + (search-chat-log is query *prv-msg-max-hits* *analyzer*)] + (if (zero? total) + (send-prv-message bot user (format "No results found for \"%s\"" query)) + (do + (send-prv-message + bot user + (format "%s results found for \"%s\". Top %s results:" + total query (count results))) + (doseq [result results] + (send-prv-message bot user result))))))))) + (defn run-bot [bot-name server channel] (let [bot (make-bot bot-name)] (connect-bot bot server channel) diff --git a/src/irc_search_bot/lucene.clj b/src/irc_search_bot/lucene.clj index dd9d404..4c49286 100644 --- a/src/irc_search_bot/lucene.clj +++ b/src/irc_search_bot/lucene.clj @@ -45,7 +45,8 @@ (QueryWrapperFilter. filter-query))]) [query, nil])) -(defn search [^IndexSearcher index-searcher ^Query query ^Filter filter ^Integer max-hits] +(defn search + [^IndexSearcher index-searcher ^Query query ^Filter filter ^Integer max-hits ^String timestamp-field] (let [top-docs (.search index-searcher @@ -54,7 +55,7 @@ max-hits (Sort. (into-array - [SortField/FIELD_SCORE (SortField. "timestamp" SortField/LONG true)])))] + [SortField/FIELD_SCORE (SortField. timestamp-field SortField/LONG true)])))] (vector (.totalHits top-docs) (->>