You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
2.0 KiB
Python
51 lines
2.0 KiB
Python
import irc.bot
|
|
from random import choice
|
|
import whoosh
|
|
from whoosh import qparser
|
|
import whoosh.index
|
|
|
|
|
|
class BotsWaller (irc.bot.SingleServerIRCBot):
|
|
def __init__(self, indexdir, channel, nickname, server, port=6667):
|
|
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
|
|
self.channel = channel
|
|
self.indexdir = indexdir
|
|
self.ix = whoosh.index.open_dir(self.indexdir)
|
|
self.parser = whoosh.qparser.QueryParser("text", schema=self.ix.schema, group=qparser.OrGroup)
|
|
|
|
def on_welcome(self, c, e):
|
|
c.join(self.channel)
|
|
print ("join")
|
|
|
|
def on_privmsg(self, c, e):
|
|
pass
|
|
|
|
def on_pubmsg(self, c, e):
|
|
# print e.arguments, e.target, e.source, e.arguments, e.type
|
|
msg = e.arguments[0]
|
|
with self.ix.searcher() as searcher:
|
|
query = self.parser.parse(msg)
|
|
results = searcher.search(query, terms=True)
|
|
results.fragmenter = whoosh.highlight.WholeFragmenter()
|
|
results.formatter = whoosh.highlight.UppercaseFormatter()
|
|
# could eventually use results[x].score as "confidence" to respond
|
|
if len(results) > 0:
|
|
results = list(results)
|
|
r = choice(results)
|
|
c.privmsg(self.channel, r.highlights("text"))
|
|
|
|
if __name__ == "__main__":
|
|
import sys, argparse
|
|
|
|
parser = argparse.ArgumentParser(description='Fats Waller Wikipedia Bot')
|
|
parser.add_argument('--index', default='index', help='path to whoosh index')
|
|
parser.add_argument('--server', default='irc.freenode.net', help='server hostname')
|
|
parser.add_argument('--port', default=6667, type=int, help='server port')
|
|
parser.add_argument('--channel', default='#botopera', help='channel to join')
|
|
parser.add_argument('--nickname', default='BOTSwaller', help='bot nickname')
|
|
|
|
args = parser.parse_args()
|
|
bot = BotsWaller(args.index, args.channel, args.nickname, args.server, args.port)
|
|
bot.start()
|
|
|