コード例 #1
0
ファイル: Cobe.py プロジェクト: alyptik/dotfiles
    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)
コード例 #2
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)
コード例 #3
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))
コード例 #4
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
コード例 #5
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
コード例 #6
0
ファイル: helga_mimic.py プロジェクト: nixjdm/helga-mimic
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
コード例 #7
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))
コード例 #8
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']))
コード例 #9
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())
コード例 #10
0
ファイル: plugin.py プロジェクト: clukawski/nish
 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)
コード例 #11
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")
コード例 #12
0
ファイル: test_brain.py プロジェクト: Gustavo6046/cobe
    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")
コード例 #13
0
ファイル: skynet.py プロジェクト: skittie/snippets
    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)
コード例 #14
0
    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)
コード例 #15
0
ファイル: test_brain.py プロジェクト: Gustavo6046/cobe
    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))])
コード例 #16
0
ファイル: test_brain.py プロジェクト: Gustavo6046/cobe
    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))
コード例 #17
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']))
コード例 #18
0
ファイル: test_brain.py プロジェクト: Gustavo6046/cobe
    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")))
コード例 #19
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)
コード例 #20
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))
コード例 #21
0
ファイル: test_brain.py プロジェクト: Gustavo6046/cobe
    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))
コード例 #22
0
ファイル: helga_mimic.py プロジェクト: nixjdm/helga-mimic
    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))
コード例 #23
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
コード例 #24
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
コード例 #25
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))
コード例 #26
0
ファイル: main.py プロジェクト: CrazyPython/sanbot2
 def learn(self, *args, **kwargs):
     return Brain(self.corpus).learn(*args, **kwargs)
コード例 #27
0
ファイル: main.py プロジェクト: CrazyPython/sanbot2
 def reply(self, *args, **kwargs):
     return Brain(self.corpus).reply(*args, **kwargs)
コード例 #28
0
ファイル: markov.py プロジェクト: sim1/slack-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)
コード例 #29
0
ファイル: test_brain.py プロジェクト: wodim/cobe
 def setUp(self):
     self.brain = Brain(":memory:")
コード例 #30
0
ファイル: cobe.py プロジェクト: pjaraherrera/telegram-bot
import sys
sys.path.append("./cobe/")
from cobe.brain import Brain

print "Learning from all"

text = []

for line in sys.stdin:
    text.append(line)

print "Starting"

b = Brain("cobe.brain")
map(lambda x: b.learn(x), text)

print "Done"