Example #1
0
class Markov:
    def __init__(self, brain_id):
        self.brain_id = brain_id
        brain_path = f'data/cobe/{brain_id}'
        os.makedirs('data/cobe/', exist_ok=True)

        self.brain = Brain(brain_path)

    def filter(self, message):
        message = re.sub('\<@[A-Z0-9a-z]{9}\>', '', message)  # remove mentions
        message = re.sub('\s{2,}', ' ', message)  #remove double spaces
        message = re.sub('\<[^\<]+\>', '', message)  #remove shit like links
        message = message.strip()  # remove unneeded spaces
        valid = False
        if len(message) > 5:
            valid = True
        return [valid, message]

    def learn(self, message):
        valid, message = self.filter(message)
        if not valid:
            return
        self.brain.learn(message)

    def speak(self, message):
        response = self.brain.reply(message)
        valid, response = self.filter(response)
        if not valid:
            return None
        return response
Example #2
0
def bot_say(seed='', think_time=THINK_TIME):
    """

    Generate response from cobe, seeding with the message.

    The we do some processing on the out, like removing nicks (both
    active and known), or replacing nick mentions with OP or preset
    list.

    1. remove nicks (both active and known), replace with either OP or
    something from a preset list TODO

    2. remove odd number quotes (the first)

    3. TODO
    """

    response = Brain('brain.ai').reply(
        seed.replace(NICK, ''),
        loop_ms=think_time,
    )

    balance_chars = ['"', '\'']
    remove_chars = ['[', ']', '{', '}', '(', ')']

    for char in remove_chars:
        response = response.replace(char, '')

    for char in balance_chars:
        if response.count(char) % 2:
            response = response.replace(char, '', 1)

    return response
Example #3
0
def create_tweet(catalyst=''):
	b = Brain(os.path.join(os.path.dirname(__file__), 'cobe.brain'))

	# get a reply from brain, encode as UTF-8
	i = 0

	while True:
		tweet = b.reply(catalyst).encode('utf-8', 'replace')
		if(config.filter_url):
			tweet = remove_url(tweet)
		if(config.filter_hashtag):
			tweet = remove_hashtag(tweet)
		if(config.filter_handle):
			tweet = remove_handle(tweet)
		tweet = smart_truncate(tweet)
		#make sure we're not tweeting something close to something else in the txt files
		#or we can just give up after 100 tries
		if check_tweet(tweet) or i >= 100:
			break
		i += 1
		
	#put the tweet in the db
	db_manager.insert_tweet(tweet)

	return tweet
def create_tweet(catalyst='', save_to_history=True):
	b = Brain(os.path.join(os.path.dirname(__file__), 'cobe.brain'))

	# get a reply from brain, encode as UTF-8
	i = 0

	while True:
		tweet = b.reply(catalyst).replace("&gt;", ">").replace("&lt;", "<").replace("&amp;", "&").replace("\"", "").replace("“", "").replace("”", "")
		if config.filter_url:
			tweet = remove_url(tweet)
		if config.filter_hashtag:
			tweet = remove_hashtag(tweet)
		if(config.filter_handle):
			tweet = remove_handle(tweet)
		tweet = smart_truncate(tweet)
		#make sure we're not tweeting something close to something else in the txt files
		#or we can just give up after 300 tries
		if check_tweet(tweet):
			# tweet matches criteria
			if save_to_history==True:
				db_manager.insert_tweet(tweet)
				print "[debug] Saved tweet to twert history database"
			return tweet
		elif i >= 300:
			print "[debug] Failure to create unique tweet. >300 tries"
			return unicode('')

		i += 1
Example #5
0
    def _reply(self, irc, msg, channel, text):
        """Send a response to text"""
        
        cobeBrain = Brain(self._getBrainDirectoryForChannel(channel))
        response = cobeBrain.reply(text).encode('utf-8')
        response = self._strip_nick(irc, msg, response)
        
        for i in range(response.lower().count(self.magicnick.lower())):
            # If first word is nick, switch with the callers nick.
            if self.magicnick in response:
                response = response.replace(self.magicnick, random.choice(list(irc.state.channels[msg.args[0]].users)))
            if self.magicnick.lower() in response:
                response = response.replace(self.magicnick.lower(), random.choice(list(irc.state.channels[msg.args[0]].users)))

        
        cobeBrain.learn(response) # Let's have the bot learn the wacky things it says
        
        self.log.info("Attempting to respond in {0} with message: {1}".format(channel, response))
        
        # delay the response here so we look real?
        if self.registryValue('responseDelay', channel):
            self.log.info("Delayed the 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))
Example #6
0
 def __init__(self, config=CONFIG):
   self.ready = False
   self.psapi = pushshift_api
   self.rapi = reddit_api
   self.config = CONFIG
   self.brain = Brain(self.config.get("cobe_main_db"))
   self.size = 0
Example #7
0
def learn_text(text, brain_name):
    brain = Brain(brain_name)
    if not os.path.isfile(brain_name):
        print("- Training...")
        for sent in text:
            brain.learn(sent)
    return brain
Example #8
0
    def _reply(self, irc, msg, channel, text):
        """Send a response to text"""

        cobeBrain = Brain(self._getBrainDirectoryForChannel(channel))
        response = cobeBrain.reply(text).encode('utf-8')
        response = self._strip_nick(irc, msg, response)

        for i in range(response.lower().count(self.magicnick.lower())):
            # If first word is nick, switch with the callers nick.
            if self.magicnick in response:
                response = response.replace(
                    self.magicnick,
                    random.choice(list(irc.state.channels[msg.args[0]].users)))
            if self.magicnick.lower() in response:
                response = response.replace(
                    self.magicnick.lower(),
                    random.choice(list(irc.state.channels[msg.args[0]].users)))

        cobeBrain.learn(
            response)  # Let's have the bot learn the wacky things it says

        self.log.info("Attempting to respond in {0} with message: {1}".format(
            channel, response))

        # delay the response here so we look real?
        if self.registryValue('responseDelay', channel):
            self.log.info("Delayed the 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))
Example #9
0
def handle(msg):
    print msg
    content_type, chat_type, chat_id = telepot.glance(msg)
    if content_type == 'text':
        brain = Brain(config.get('Brain', 'path') + str(chat_id) + ".brain")
        brain.learn(msg['text'])
        if 'braulio' in msg['text'].lower():
            bot.sendMessage(chat_id, brain.reply(msg['text']))
Example #10
0
def handle(msg):
    print msg
    content_type, chat_type, chat_id = telepot.glance(msg)
    if content_type == 'text':
        brain = Brain(config.get('Brain', 'path') + str(chat_id) + ".brain")
        brain.learn(msg['text'])
        if 'braulio' in msg['text'].lower():
            bot.sendMessage(chat_id,brain.reply(msg['text']))
Example #11
0
    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)
Example #12
0
def main():
    b = Brain("brains/bible.brain")
    #with open("learning/bible.txt", "r") as bibleFile:
    #    bible = bibleFile.readlines()

    #for line in bible:
    #    b.learn(line)

    print b.reply("Hello cobe")
Example #13
0
 def invalidCommand(self, irc, msg, tokens):
     channel = msg.args[0]
     text = msg.args[1]
     text = self._cleanText(text)
     #cobeBrain = Brain(self.brainDirectories[channel])
     cobeBrain = Brain(self._getBrainDirectoryForChannel(channel))
     response = cobeBrain.reply(text).encode('utf-8')
     response = self._strip_nick(irc, msg, response)
     irc.reply(response, prefixNick=False)
Example #14
0
def main():
    b = Brain("brains/bible.brain")
    #with open("learning/bible.txt", "r") as bibleFile:
    #    bible = bibleFile.readlines()

    #for line in bible:
    #    b.learn(line)

    print b.reply("Hello cobe")
Example #15
0
    def testInit(self):
        Brain.init(TEST_BRAIN_FILE)
        self.failUnless(os.path.exists(TEST_BRAIN_FILE),
                        "missing brain file after init")

        brain = Brain(TEST_BRAIN_FILE)
        self.failUnless(brain.order, "missing brain order after init")
        self.failUnless(brain._end_token_id,
                        "missing brain _end_token_id after init")
Example #16
0
def handle(msg):
    content_type, chat_type, chat_id = telepot.glance(msg)
    if content_type == 'text':
        brain = Brain(config.get('Brain', 'path') + str(chat_id) + ".brain")
        brain.learn(msg['text'])
        if 'reply_to_message' in msg and msg['reply_to_message']['from']['username'] == "Braulio_bot":
            bot.sendMessage(chat_id,brain.reply(msg['text']),reply_to_message_id=msg['message_id'])
        elif 'braulio' in msg['text'].lower():
            bot.sendMessage(chat_id,brain.reply(msg['text']).replace("Braulio",msg['from']['first_name']))
Example #17
0
    def testInit(self):
        Brain.init(TEST_BRAIN_FILE)
        self.assertTrue(os.path.exists(TEST_BRAIN_FILE),
                        "missing brain file after init")

        brain = Brain(TEST_BRAIN_FILE)
        self.assertTrue(brain.order, "missing brain order after init")
        self.assertTrue(brain._end_token_id,
                        "missing brain _end_token_id after init")
Example #18
0
def create_tweet(catalyst=''):
    b = Brain(os.path.join(os.path.dirname(__file__), 'cobe.brain'))

    # get a reply from brain, encode as UTF-8
    i = 0

    while True:
        tweet = b.reply(catalyst).encode('utf-8', 'replace')
        if(config['filter_urls']):
            tweet = remove_url(tweet)
        tweet = smart_truncate(tweet)


        # check if last words of tweet are less than 4 and remove them
        last_words_twert = tweet.split(' ')
        while len(last_words_twert[-1]) < 4:
            print "[debug] Removing last word:"+last_words_twert[-1]
            del(last_words_twert[-1])
        tweet = ' '.join(last_words_twert)



        #make sure we're not tweeting something close to something else in the txt files
        #or we can just give up after 100 tries
        if check_tweet(tweet) or i >= 100:
            break
        i += 1
    
    tweet = HTMLParser().unescape(tweet)
    tweet = tweet.upper()
    # clean up miscellaneous characters INCLUDING NUMBERS?
    for ch in ['(',')','1','2','3','4','5','6','7','8','9','0','.',', ,','-,','-;','-.',',,',' ;' ]:
        if ch in tweet:
            tweet=tweet.replace(ch,"")
        if ' TH ' in tweet:
            tweet = tweet.replace(' TH ',' ')
        if ' ND ' in tweet:
            tweet = tweet.replace(' ND ',' ')
        if ' RD ' in tweet:
            tweet = tweet.replace(' RD ',' ')
        if 'THE OF' in tweet:
            tweet = tweet.replace('THE OF ',' ')
        if "  " in tweet:
            tweet = tweet.replace("  "," ")
        if " - " in tweet:
            tweet = tweet.replace(" - "," ")
        if " , " in tweet:
            tweet = tweet.replace(" , ",", ")
    tweet = tweet.rstrip(" ,;=-")
    tweet = tweet.lstrip(" ,;=-?{}[]/_=+")

        
    #put the tweet in the db
    db_manager.insert_tweet(tweet)

    return tweet
Example #19
0
class BrainTest(unittest.TestCase):
    def setUp(self):
        self.brain = Brain(":memory:")

    def test_train(self):
        # It's hard to test training from here; make sure the model
        # gets expected probabilities.
        model = self.brain.model

        self.brain.train(u"this is a test")
        self.assertAlmostEqual(1.0, model.prob(u"a", u"this is".split()))

        self.brain.train(u"this is another test")
        self.assertAlmostEqual(0.5, model.prob(u"a", u"this is".split()))

    def test_train_many(self):
        self.brain.train_many([u"this is a test"])

        model = self.brain.model
        self.assertAlmostEqual(1.0, model.prob(u"a", u"this is".split()))

    def test_reply(self):
        training = [u"this is a test", u"this is another test"]

        for train in training:
            self.brain.train(train)

        self.assert_(self.brain.reply(u"testing") in set(training))
Example #20
0
    def _learn_corpus(self, corpus_file, brain_name, questions=False):
        if not os.path.isfile(brain_name):
            brain = Brain(brain_name)
            print("- Training...")
            corpus = read_file(corpus_file)
            corpus = clean_text(corpus, get_questions=questions)

            for sent in corpus:
                brain.learn(sent)

        return Brain(brain_name)
Example #21
0
 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))
Example #22
0
 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)
Example #23
0
    def testWrongVersion(self):
        Brain.init(TEST_BRAIN_FILE)

        # manually change the brain version to 1
        brain = Brain(TEST_BRAIN_FILE)
        brain.graph.set_info_text("version", "1")
        brain.graph.commit()
        brain.graph.close()

        try:
            Brain(TEST_BRAIN_FILE)
        except CobeError, e:
            self.assertTrue("cannot read a version" in str(e))
Example #24
0
    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.")
Example #25
0
def fun(message):
    print message.text
    brain = Brain("/db/" + str(message.chat.id)[1:] + ".br")
    # Telegram understands UTF-8, so encode text for unicode compatibility
    brain.learn(message.text)
    if "tagueul" in message.text.lower() or "tg" in message.text.lower() or "ta gueule" in message.text.lower():
        bot.reply_to(message, "Non, toi ta gueule.")
    elif (randint(1, 100) < percent):
        if violence : 
          bot.reply_to(message, brain.reply(message.text.upper(), 3000))
        else :
          bot.reply_to(message, brain.reply(message.text, 3000))
    return 'ok'
Example #26
0
    def connectionMade(self):
        self.nickname = self.factory.nick
        self.password = self.factory.password
        self.replyrate = self.factory.replyrate
        self.ignored = self.factory.ignored
        self.trusted = self.factory.trusted
        self.talking = self.factory.talking

        irc.IRCClient.connectionMade(self)
        print("Connected")

        print("Using brain %s" % self.factory.brain)
        self.brain = Brain(self.factory.brain)
Example #27
0
    def testWrongVersion(self):
        Brain.init(TEST_BRAIN_FILE)

        # manually change the brain version to 1
        brain = Brain(TEST_BRAIN_FILE)
        brain.graph.set_info_text("version", "1")
        brain.graph.commit()
        brain.graph.close()

        try:
            Brain(TEST_BRAIN_FILE)
        except CobeError, e:
            self.assert_("cannot read a version" in str(e))
    def workerThread(self):
        self.brain = Brain(self.fileLocation)
        #wait a few minutes to load enough information
        time.sleep(self.initialWaitTime)
        while True:
            time.sleep(self.chattiness)
            if len(self.brainCache) == 0:
                '''
                try:
                    self.sendChatMessageLock.release()
                except Exception, e:
                    z = e
                    print 'release exception: ' + str(z)
                    '''
                print 'there was nothing to reply to'
                pass
            else:
                print 'adding new info from cache: ' + str(self.brainCache)
                for msg in self.brainCache:  #add all current messages to the brain
                    self.brain.learn(msg)
                usedMessage = max(self.brainCache, key=lambda msg: len(msg))
                print 'replying to message: ' + usedMessage
                message = (usedMessage +
                           '.')[:-1]  #get a copy of the longest message
                del self.brainCache[:]  #clear cache after storing the messages in the brain
                self.brainCache = list()
                gc.collect()
                #release lock after making copy
                '''
                try:
                    self.sendChatMessageLock.release()
                except Exception, e:
                    z = e
                    print 'release exception: ' + str(z)
                 '''

                sentence = ''
                try:
                    sentence = self.generate_sentence(
                        message)  #make new message
                    print 'generated sentence: ' + sentence
                except IndexError, e:
                    a = 1
                if len(sentence.split(' ')) > 2 and sentence.find(
                        '@'
                ) < 0:  #send new message if it meets certain criteria
                    self.cleanString(sentence)
                    #print 'chatter says: ' + sentence
                    command = 'PRIVMSG #otherbrand ' + " :" + sentence + '\r\n'
                    self.commandQueueRef.sendCommandToQueue(
                        command, self.initialWaitTime)
Example #29
0
    def _learn(self, irc, msg, channel, text, probability):
        """Internal method for learning phrases."""

        if os.path.exists(self._getBrainDirectoryForChannel(channel)):
            # Does this channel have a directory for the brain file stored and does this file exist?

            text = self._cleanText(text)

            if text and len(text) > 1 and not text.isspace():

                self.log.debug("Learning: {0}".format(text))
                cobeBrain = Brain(self._getBrainDirectoryForChannel(channel))
                cobeBrain.learn(text)

                if random.randint(0, 10000) <= probability:
                    self._reply(irc, msg, channel, text)

        else:  # Nope, let's make it!

            commands.getoutput('{0} {1}'.format(self._doCommand(channel),
                                                'init'))

            text = self._cleanText(text)

            if text and len(text) > 1 and not text.isspace():

                self.log.debug("Learning: {0}".format(text))
                cobeBrain = Brain(self._getBrainDirectoryForChannel(channel))
                cobeBrain.learn(text)

                if random.randint(0, 10000) <= probability:
                    self._reply(irc, msg, channel, text)
Example #30
0
    def testExpandGraph(self):
        Brain.init(TEST_BRAIN_FILE, order=2)
        brain = Brain(TEST_BRAIN_FILE)

        tokens = ["this", Brain.SPACE_TOKEN_ID, "is", Brain.SPACE_TOKEN_ID,
                  "a", Brain.SPACE_TOKEN_ID, "test"]

        self.assertEquals(list(brain._to_graph(brain._to_edges(tokens))),
                          [((1, 1), False, (1, "this")),
                           ((1, "this"), True, ("this", "is")),
                           (("this", "is"), True, ("is", "a")),
                           (("is", "a"), True, ("a", "test")),
                           (("a", "test"), False, ("test", 1)),
                           (("test", 1), False, (1, 1))])
Example #31
0
    def testInfoPickle(self):
        order = 2
        Brain.init(TEST_BRAIN_FILE, order=order)

        brain = Brain(TEST_BRAIN_FILE)

        db = brain.graph
        key = "pickle_test"
        obj = {"dummy": "object", "to": "pickle"}

        db.set_info_text(key, pickle.dumps(obj))

        get_info_text = lambda: pickle.loads(
            db.get_info_text(key, text_factory=str))
Example #32
0
File: server.py Project: happz/viki
class Chatter(object):
  def __init__(self):
    self.brain = Brain('cobe.brain')

    with open('seed.txt', 'r') as f:
      text = [l.strip() for l in f.read().replace('\n', ' ').replace('.', '\n').replace('?', '\n').replace('\xa0', ' - ').split('\n') if l.strip()]

      for line in text:
        self.brain.learn(line)

  def reply(self, message):
    return self.brain.reply(message)

  def learn(self, line):
    self.brain.learn(line)
Example #33
0
 def _reply(self, irc, channel, text):
     """Send a response to text"""
     
     cobeBrain = Brain(self._getBrainDirectoryForChannel(channel))
     response = cobeBrain.reply(text).encode('utf-8')
     
     cobeBrain.learn(response) # Let's have the bot learn the wacky things it says
     
     self.log.info("Attempting to respond in {0} with message: {1}".format(channel, response))
     
     # delay the response here so we look real?
     if self.registryValue('responseDelay', channel):
         self.log.info("Delayed the 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))
Example #34
0
File: server.py Project: happz/viki
  def __init__(self):
    self.brain = Brain('cobe.brain')

    with open('seed.txt', 'r') as f:
      text = [l.strip() for l in f.read().replace('\n', ' ').replace('.', '\n').replace('?', '\n').replace('\xa0', ' - ').split('\n') if l.strip()]

      for line in text:
        self.brain.learn(line)
Example #35
0
def load_brain(ctx):
    ctx.storage.brains[ctx.config.brain_file] = Brain(ctx.config.brain_file,
                                                      check_same_thread=False)

    scorer = ctx.storage.brains[ctx.config.brain_file].scorer
    scorer.score = scorergroup_score.__get__(scorer, ScorerGroup)
    scorer.add_scorer(1.0, LengthScorer())
    scorer.add_scorer(1.0, BalancedScorer())
Example #36
0
    def testInfoPickle(self):
        order = 2
        Brain.init(TEST_BRAIN_FILE, order=order)

        brain = Brain(TEST_BRAIN_FILE)

        db = brain.graph
        key = "pickle_test"
        obj = self

        db.set_info_text(key, pickle.dumps(obj))

        # pickle cannot load from a unicode object
        get_info_text = lambda: pickle.loads(db.get_info_text(key))
        self.assertRaises(TypeError, get_info_text)

        get_info_text = lambda: pickle.loads(
            db.get_info_text(key, text_factory=str))
Example #37
0
    def testInfoPickle(self):
        order = 2
        Brain.init(TEST_BRAIN_FILE, order=order)

        brain = Brain(TEST_BRAIN_FILE)

        db = brain.graph
        key = "pickle_test"
        obj = {"dummy": "object", "to": "pickle"}

        db.set_info_text(key, pickle.dumps(obj))

        # pickle cannot load from a unicode object
        get_info_text = lambda: pickle.loads(db.get_info_text(key))
        self.assertRaises(TypeError, get_info_text)

        get_info_text = lambda: pickle.loads(
            db.get_info_text(key, text_factory=str))
Example #38
0
    def testInfoText(self):
        order = 2
        Brain.init(TEST_BRAIN_FILE, order=order)

        brain = Brain(TEST_BRAIN_FILE)

        db = brain.graph
        key = "test_text"

        self.assertEqual(None, db.get_info_text(key))

        db.set_info_text(key, "test_value")
        self.assertEqual("test_value", db.get_info_text(key))

        db.set_info_text(key, "test_value2")
        self.assertEqual("test_value2", db.get_info_text(key))

        db.set_info_text(key, None)
        self.assertEqual(None, db.get_info_text(key))
Example #39
0
    def testInfoText(self):
        order = 2
        Brain.init(TEST_BRAIN_FILE, order=order)

        brain = Brain(TEST_BRAIN_FILE)

        db = brain.graph
        key = "test_text"

        self.assertEqual(None, db.get_info_text(key))

        db.set_info_text(key, "test_value")
        self.assertEqual("test_value", db.get_info_text(key))

        db.set_info_text(key, "test_value2")
        self.assertEqual("test_value2", db.get_info_text(key))

        db.set_info_text(key, None)
        self.assertEqual(None, db.get_info_text(key))
Example #40
0
def learn(archivepath, brain, **kwargs):
    # start brain. Batch saves us from lots of I/O
    brain = Brain(brain)
    brain.set_stemmer(kwargs.get('language', 'english'))

    brain.start_batch_learning()

    tweets = tweet_generator(archivepath, **kwargs)
    count = 0

    for text in tweets:
        count = count + 1
        brain.learn(text)

    brain.stop_batch_learning()

    return count
Example #41
0
    def _reply(self, irc, msg, channel, text):
        """Send a response to text"""

        cobeBrain = Brain(self._getBrainDirectoryForChannel(channel))
        response = cobeBrain.reply(text).encode('utf-8')
        response = self._strip_nick(irc, msg, response)

        cobeBrain.learn(
            response)  # Let's have the bot learn the wacky things it says

        self.log.info("Attempting to respond in {0} with message: {1}".format(
            channel, response))

        # delay the response here so we look real?
        if self.registryValue('responseDelay', channel):
            self.log.info("Delayed the 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))
Example #42
0
def create_tweet(catalyst=''):
    b = Brain(os.path.join(os.path.dirname(__file__), 'cobe.brain'))

    # get a reply from brain, encode as UTF-8
    i = 0

    while True:
        tweet = b.reply(catalyst).encode('utf-8', 'replace')
        if (config.filter_url):
            tweet = remove_url(tweet)
        tweet = smart_truncate(tweet)
        #make sure we're not tweeting something close to something else in the txt files
        #or we can just give up after 100 tries
        if check_tweet(tweet) or i >= 100:
            break
        i += 1

    #put the tweet in the db
    db_manager.insert_tweet(tweet)

    return tweet
Example #43
0
 def _learn(self, irc, channel, text, probability):
     """Internal method for learning phrases."""
     
     if os.path.exists(self._getBrainDirectoryForChannel(channel)):
         # Does this channel have a directory for the brain file stored and does this file exist?
         
         text = self._cleanText(text)
         
         if text and len(text) > 1 and not text.isspace():
     
             self.log.debug("Learning: {0}".format(text))
             cobeBrain = Brain(self._getBrainDirectoryForChannel(channel))
             cobeBrain.learn(text)
             
             if random.randint(0, 10000) <= probability:
                 self._reply(irc, channel, text)
             
     else: # Nope, let's make it!
                     
         commands.getoutput('{0} {1}'.format(self._doCommand(channel), 'init'))
         
         text = self._cleanText(text)
         
         if text and len(text) > 1 and not text.isspace():
     
             self.log.debug("Learning: {0}".format(text))
             cobeBrain = Brain(self._getBrainDirectoryForChannel(channel))
             cobeBrain.learn(text)
             
             if random.randint(0, 10000) <= probability:
                 self._reply(irc, channel, text)
Example #44
0
class Markov(ModuleInterface):
    help = "Markov - Yeah I'm sentient, what of it?"

    def onEnable(self):
        self.brain = Brain(os.path.join("hubbot", "data", "{}.brain".format(self.bot.server)))

    def addToBrain(self, msg):
        if "://" not in msg and len(msg) > 1:
            self.brain.learn(msg)

    def shouldTrigger(self, message):
        """
        @type message: hubbot.message.IRCMessage
        """
        if message.Type in self.acceptedTypes:
            return True
        return False

    def onTrigger(self, message):
        """
        @type message: hubbot.message.IRCMessage
        """
        if message.User.Name == self.bot.nickname:
            return
        elif message.TargetType is TargetTypes.USER and not message.MessageString.startswith(self.bot.commandChar):
            reply = self.brain.reply(message.MessageString, max_len=100)
            return IRCResponse(ResponseType.Say, reply.capitalize(), message.ReplyTo)
        elif self.bot.nickname.lower() in message.MessageString.lower() and len(message.MessageList) > 1:
            messageList = [item.lower() for item in message.MessageList if item.lower() != self.bot.nickname.lower()]
            reply = self.brain.reply(" ".join(messageList), max_len=100)

            nickList = [nick.lower() for nick in self.bot.channels[message.ReplyTo].Users.keys()]
            for word in reply.split():
                if word.lower() in nickList:
                    reply.replace(word, message.User.Name)
            return IRCResponse(ResponseType.Say, reply.capitalize(), message.ReplyTo)
        else:
            messageList = [item.lower() for item in message.MessageList if item.lower() != self.bot.nickname.lower()]
            self.addToBrain(" ".join(messageList))
Example #45
0
    def __init__(self, bot):
        self._brain_file = os.path.join(bot.op.files.data_dir, "cobe.brain")
        self._trace_file = os.path.join(bot.op.files.data_dir, "cobe.trace")

        # rotate logs
        if os.path.exists(self._trace_file):
            now = datetime.datetime.now()
            stamp = now.strftime("%Y-%m-%d.%H%M%S")
            os.rename(self._trace_file, "%s.%s" % (self._trace_file, stamp))

        self._brain = Brain(self._brain_file, instatrace=self._trace_file)

        kibot.BaseModule.BaseModule.__init__(self, bot)
Example #46
0
    def testLearnStems(self):
        Brain.init(TEST_BRAIN_FILE, order=2)

        brain = Brain(TEST_BRAIN_FILE)
        brain.set_stemmer("english")
        stem = brain.stemmer.stem

        brain.learn("this is testing")

        c = brain.graph.cursor()
        stem_count = c.execute("SELECT count(*) FROM token_stems").fetchone()

        self.assertEqual(3, stem_count[0])
        self.assertEqual(brain.graph.get_token_stem_id(stem("test")),
                          brain.graph.get_token_stem_id(stem("testing")))
def create_tweet(catalyst=""):
    b = Brain(os.path.join(os.path.dirname(__file__), "cobe.brain"))

    # get a reply from brain, encode as UTF-8
    i = 0

    while True:
        tweet = b.reply(catalyst).encode("utf-8", "replace")
        if config["filter_urls"]:
            tweet = remove_url(tweet)
        tweet = smart_truncate(tweet)
        tweet = quote_fixer(tweet)
        # make sure we're not tweeting something close to something else in the txt files
        # or we can just give up after 100 tries
        if check_tweet(tweet) or i >= 2000:
            break
        i += 1

    tweet = HTMLParser().unescape(tweet)

    # put the tweet in the db
    db_manager.insert_tweet(tweet)

    return tweet
Example #48
0
    def testLearnStems(self):
        Brain.init(TEST_BRAIN_FILE, order=2)

        brain = Brain(TEST_BRAIN_FILE)
        brain.set_stemmer("english")
        stem = brain.stemmer.stem

        brain.learn("this is testing")

        c = brain.graph.cursor()
        stem_count = c.execute("SELECT count(*) FROM token_stems").fetchone()

        self.assertEqual(3, stem_count[0])
        self.assertEqual(brain.graph.get_token_stem_id(stem("test")),
                          brain.graph.get_token_stem_id(stem("testing")))
Example #49
0
    def reply(self, lang, msg):
        # reply message

        if self.multibrain:
            lang = guess_language_name(msg).lower()
        else:
            lang = 'italian'

        if not lang in self.brains:
            self.brains[lang] = Brain(
                os.path.join(
                    os.path.split(__file__)[0], "brains",
                    "lampone_%s.brain" % lang))
            if lang in self.languages:
                self.brains[lang].set_stemmer(lang)

        return self.brains[lang].reply(msg, loop_ms=self.reply_time)
Example #50
0
    def train_brain(self, channel):
        """
        create a cobe brain file based on the db.

        This file is used by cobe to generate responses.
        """

        logger.debug('starting training')
        logger.debug('ignored: {}'.format(IGNORED))

        # replace the current brain
        try:
            os.remove('brain.ai')
        except:
            pass

        BRAIN = Brain('brain.ai')

        logger.debug('created brain.ai')

        start = time.time()

        BRAIN.start_batch_learning()

        logger_lines = db.logger.find({
            'channel': channel,
            'nick': {
                '$nin': IGNORED
            },
            'message': {
                '$regex': '^(?!\.|\,|\!)'
            },
        })

        logger.debug('log total: {}'.format(logger_lines.count()))

        for line in logger_lines:
            BRAIN.learn(line['message'])

        BRAIN.stop_batch_learning()

        logger.debug('learned stuff. Took {:.2f}s'.format(time.time() - start))
Example #51
0
    def testExpandGraph(self):
        Brain.init(TEST_BRAIN_FILE, order=2)
        brain = Brain(TEST_BRAIN_FILE)

        tokens = ["this", Brain.SPACE_TOKEN_ID, "is", Brain.SPACE_TOKEN_ID,
                  "a", Brain.SPACE_TOKEN_ID, "test"]

        self.assertEqual(list(brain._to_graph(brain._to_edges(tokens))),
                          [((1, 1), False, (1, "this")),
                           ((1, "this"), True, ("this", "is")),
                           (("this", "is"), True, ("is", "a")),
                           (("is", "a"), True, ("a", "test")),
                           (("a", "test"), False, ("test", 1)),
                           (("test", 1), False, (1, 1))])
Example #52
0
 def __init__(self, irc):
     # Call Supybot's scripts
     self.__parent = super(Cobe, self)
     self.__parent.__init__(irc)
     
     # Save state
     saved = (sys.stdout, os.getcwd())
     
     # Create proxy for Cobe
     os.chdir(conf.supybot.directories.data())
     sys.stdout = StringIO()
     
     # Initialize Cobe
     self.brain = Brain("cobe.store")
     
     # Restore state
     sys.stdout, cwd = saved
     os.chdir(cwd)
     
     random.seed()
Example #53
0
    def learn(self, msg):
        # learn message
        if self.multibrain:
            lang = guess_language_name(msg).lower()
        else:
            lang = 'italian'

        try:
            if not lang in self.brains:
                self.brains[lang] = Brain(
                    os.path.join(
                        os.path.split(__file__)[0], "brains",
                        "lampone_%s.brain" % lang))
                if self.brains[lang] in self.languages:
                    brain.set_stemmer(lang)
            self.brains[lang].learn(msg)
        except Exception as e:
            logger.error("ERR - learn - %s" % e)

        return lang
Example #54
0
def handle(msg):
    content_type, chat_type, chat_id = telepot.glance(msg)
    if content_type == 'text':
        brain = Brain(config.get('Brain', 'path') + str(chat_id) + ".brain")
        brain.learn(msg['text'])
        if 'reply_to_message' in msg and msg['reply_to_message']['from'][
                'username'] == "Braulio_bot":
            bot.sendMessage(chat_id,
                            brain.reply(msg['text']),
                            reply_to_message_id=msg['message_id'])
        elif 'braulio' in msg['text'].lower():
            bot.sendMessage(
                chat_id,
                brain.reply(msg['text']).replace("Braulio",
                                                 msg['from']['first_name']))
Example #55
0
    def __init__(self):
        self.path, self.file = os.path.split(_abspath)
        self.brain = Brain(u'{}/brain.sqlite'.format(self.path))

        config_json = u'{}/config.json'.format(self.path)
        rps_json = u'{}/rps.json'.format(self.path)
        keys_json = u'{}/keys.json'.format(self.path)
        self.config = dbaccess.Config(config_json, rps_json, keys_json)

        # Load plugins
        for plug_name in self.config.get(u'plugins', list())[:]:
            public, private = self.handle_load([u'!load', plug_name])
            for m in private:
                log.info(m)

        self.mb = util.CollectionOfNamedLists(u'{}/mb.json'.format(self.path))
        self.tf = util.TitleFetcher()

        args = sys.argv[1:]
        for arg in args:
            if arg.startswith(u'--set-'):
                key, value = arg[6:].split(u'=', 1)
                print u'Setting \'{}\' to \'{}\'.'.format(key, value)
                self.config.set(key, value)

        # Set up ignore if the ignore list is non-empty.
        ignore = self.config.get(u'msg:ignore', u'')
        self.reignore = None
        if ignore:
            self.reignore = re.compile(ignore, re.IGNORECASE)

        server = self.config.get(u'irc:server')
        nick = self.config.get(u'irc:nick')
        name = self.config.get(u'irc:name')
        SingleServerIRCBot.__init__(self, [(server, 6667)], nick, name)
        self.connection.buffer_class.errors = u'replace'
Example #56
0
def chatbot(botname, remotebot, out=sys.stdout):
	mem = Brain(botname + ".brain")
	mem.learn("This is the only thing I know!")

	msg = "Hello!"
	out.write("BEGIN LOG: %s\n" % time.ctime())
	try:
		while True:
			out.write("Local: %s\n" % msg)
			msg = remotebot.think(msg)
			out.write("Remote: %s\n" % msg)
			if LEARN == True:
				mem.learn(msg)
			msg = mem.reply(msg)
			out.flush()

	except (KeyboardInterrupt, SystemExit, EOFError):
		print "Saving..."
		out.write("END LOG: %s\n" % time.ctime())
		out.close()
Example #57
0
 def learn(self, *args, **kwargs):
     return Brain(self.corpus).learn(*args, **kwargs)
Example #58
0
 def reply(self, *args, **kwargs):
     return Brain(self.corpus).reply(*args, **kwargs)
Example #59
0
    def __init__(self, brain_id):
        self.brain_id = brain_id
        brain_path = f'data/cobe/{brain_id}'
        os.makedirs('data/cobe/', exist_ok=True)

        self.brain = Brain(brain_path)
Example #60
0
from os import path
from cobe.brain import Brain

b = Brain("%s/shakespeare.sqlite" % path.split(path.abspath(__file__))[0])

b.start_batch_learning()

with open("shakespeare.txt") as f:
    for l in f:
        b.learn(l)

b.stop_batch_learning()