forked from andrewtryder/MegaHAL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
197 lines (171 loc) · 6.87 KB
/
plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# -*- coding: utf-8 -*-
###
# Copyright (c) 2013, spline
# All rights reserved.
#
#
###
# my libs
import time
import re
import random
import os
import chardet
# extra supybot
import supybot.ircmsgs as ircmsgs
import supybot.conf as conf
import supybot.schedule as schedule
# stock supybot
import supybot.utils as utils
from supybot.commands import *
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
from supybot.i18n import PluginInternationalization, internationalizeDocstring
try:
from cobe.brain import Brain
except ImportError:
raise callbacks.Error, 'You need to install cobe. pip install cobe.'
_ = PluginInternationalization('MegaHAL')
@internationalizeDocstring
class MegaHAL(callbacks.Plugin):
"""Add the help for "@plugin help MegaHAL" here
This should describe *how* to use this plugin."""
threaded = True
callAfter = ['MoobotFactoids', 'Factoids', 'Infobot']
callBefore = ['Dunno']
def __init__(self, irc):
self.__parent = super(MegaHAL, self)
self.__parent.__init__(irc)
self._brainfile = conf.supybot.directories.data.dirize('MegaHAL.brain')
self.last_replied = ircutils.IrcDict()
def die(self):
self.__parent.die()
def _updatesentinel(self, channel):
now = time.time()
try:
lastreplied = self.last_replied[channel]
except KeyError:
self.last_replied[channel] = now
self.log.debug("I have not spoke in {0} yet.".format(channel))
return True
if lastreplied:
if now > (lastreplied + self.registryValue('waitTimeBetweenSpeaking', channel)):
self.log.debug("I spoke more than {0} ago in {1}".format(lastreplied, channel))
self.last_replied[channel] = now
return True
else:
self.log.debug("I spoke less than {0} ago in {1}".format(lastreplied, channel))
return False
def _decode_irc(self, raw, preferred_encs = ["UTF-8", "CP1252", "ISO-8859-1"]):
"""Taken from here: http://bit.ly/1681r2i """
changed = False
for enc in preferred_encs:
try:
res = raw.decode(enc)
changed = True
break
except:
pass
if not changed:
try:
enc = chardet.detect(raw)['encoding']
res = raw.decode(enc)
except:
res = raw.decode(enc, 'ignore')
return res
def _cleantext(self, text):
"""Clean-up text for input into corpus."""
text = self._decode_irc(text)
text = ircutils.stripFormatting(text)
text = text.strip()
text = re.sub("[^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890@.!?;:/%\$§\-_ ]", " ", text)
text = utils.str.normalizeWhitespace(text)
return text
def _learn(self, irc, channel, text, probability):
text = self._cleantext(text)
if text:
if len(text) > 0 and not text.isspace():
b = Brain(self._brainfile)
b.learn(text)
# determine probability to respond.
if random.randint(0, 100) < probability:
# if we've randomly determined to talk, check the update time.
sentinel = self._updatesentinel(channel)
if sentinel:
self._reply(irc, channel, text)
def _reply(self, irc, channel, text):
self.log.info("Trying to respond in %s" % channel)
b = Brain(self._brainfile)
response = b.reply(text).encode('utf-8')
# delay the response here so we look real?
if self.registryValue('responseDelay', channel):
self.log.info("Delayed response in %s" % channel)
delayseconds = time.time() + random.randint(2, 5)
schedule.addEvent((irc.queueMsg(ircmsgs.privmsg(channel, response))), delayseconds)
else:
irc.queueMsg(ircmsgs.privmsg(channel, response))
def doPrivmsg(self, irc, msg):
channel = msg.args[0].lower()
text = msg.args[1].strip()
# ignore ctcp, action and only messages in a channel.
# if txt.startswith(conf.supybot.reply.whenAddressedBy.chars()):
if ircmsgs.isCtcp(msg) or ircmsgs.isAction(msg) or not irc.isChannel(channel):
return
# on to the text. check if we're ignoring the text matching regex here.
if re.match(self.registryValue('ignoreRegex'), text):
return
# should we strip urls from text?
if self.registryValue('stripUrls'):
text = re.sub(r'(http[^\s]*)', '', text)
# determine probability if addressed or not.
if callbacks.addressed(irc.nick, msg):
text = re.sub('(' + irc.nick + '*?\W+)', '', text, re.IGNORECASE)
probability = self.registryValue('probabilityWhenAddressed', channel)
else:
probability = self.registryValue('probability', channel)
# should we strip nicks from the text?
if self.registryValue('stripNicks'):
removenicks = '|'.join(item + '\W.*?\s' for item in irc.stats.channels[channel].users)
text = re.sub(r'' + removenicks + '', '', text)
# finally, pass to our learn function.
self._learn(irc, channel, text, probability)
def _prettysize(self, size):
suffixes = [("B",2**10), ("K",2**20), ("M",2**30), ("G",2**40), ("T",2**50)]
for suf, lim in suffixes:
if size > lim:
continue
else:
return round(size/float(lim/2**10),2).__str__()+suf
def corpussize(self, irc, msg, args):
"""
Display the size of my brain on disk.
"""
if os.path.exists(self._brainfile):
size = float(os.path.getsize(self._brainfile))
irc.reply("My brainfile is: {0}".format(self._prettysize(size)))
else:
irc.reply("ERROR: I am missing a brainfile.")
corpussize = wrap(corpussize)
def corpuslearn(self, irc, msg, args, text):
"""<text>
Manually teach the corpus <text>
"""
text = self._cleantext(text)
if text and len(text) > 1 and not text.isspace():
irc.reply("I am learning: {0}".format(text))
b = Brain(self._brainfile)
b.learn(text)
else:
irc.reply("After sanitizing, I did not have any text to learn.")
corpuslearn = wrap(corpuslearn, [('checkCapability', 'admin'), ('text')])
def corpusreply(self, irc, msg, args, text):
"""<text>
Manually have the brain reply to <text>
"""
b = Brain(self._brainfile)
response = b.reply(text).encode('utf-8')
irc.reply(response)
corpusreply = wrap(corpusreply, [('checkCapability', 'admin'), ('text')])
Class = MegaHAL
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=250: