def _gen_markov_model(self, text): lines = text.split("\n") # Get an array of lines of text # from the corpus # TODO: figure out how to clean this up def handle_begin(queue): begin_state = tuple(queue.contents[:2]) next_state = tuple(queue.contents[1:]) self._graph.insert_word(begin_state) self._graph.insert_word(next_state) self._graph.upsert_vert(".", begin_state) self._graph.upsert_vert(begin_state, next_state) def handle_end(queue): prev_state = tuple(queue.contents[:-1]) next_state = tuple(queue.contents[1:]) self._graph.insert_word(next_state) self._graph.insert_word(prev_state) self._graph.upsert_vert(prev_state, next_state) self._graph.upsert_vert(next_state, "?") def handle_rest(queue): prev_state = tuple(queue.contents[:-1]) next_state = tuple(queue.contents[1:]) self._graph.insert_word(prev_state) self._graph.insert_word(next_state) self._graph.upsert_vert(prev_state, next_state) for line in lines: queue = Queue(self.order + 1) tokens = self._tokenize_line(line) for index, token in enumerate(tokens): queue.enqueue(token) if len(queue.contents) >= self.order + 1: # handles the start of sentences if index == self.order: handle_begin(queue) # handles edge case where len of sentence == order + 1 if token is tokens[-1]: handle_end(queue) # handles the end of sentences elif token is tokens[-1]: handle_end(queue) # handles everything else else: handle_rest(queue)
def start_stream(): comments = sub.stream.comments() for comment in comments: for bot in bots: if bot.validate_comment(comment): queue = pickle.load(open(bot.file, 'rb')) if queue: queue.enqueue(comment.id) else: queue = Queue() queue.enqueue(comment.id) pickle.dump(queue, open(bot.file, 'wb')) timestr = str(time.localtime()[3]) + ":" + str(time.localtime()[4]) print("> %s - Added comment to queue! Queue length: %s" % (timestr, len(queue)))
def start_stream(): comments = sub.stream.comments(pause_after=-1) for comment in comments: if comment == None: continue if garlicbot.validate_comment(comment): queue = pickle.load(open(garlicbot.file, 'rb')) if queue: queue.enqueue(comment.id) else: queue = Queue() queue.enqueue(comment.id) pickle.dump(queue, open(garlicbot.file, 'wb')) timestr = str(time.localtime()[3]) + ":" + str(time.localtime()[4]) print("> %s - Added comment to queue! Queue length: %s" % (timestr, len(queue)))
def start_stream(): messages = rocket.channels_history('GENERAL', count=100).json( ) # Alternativt henter 100 siste meldinger på #bot-warz - tznw8qNDK94F3nf3u for comment in messages['messages']: for bot in bots: if bot.validate_comment(comment): queue = pickle.load(open(bot.file, 'rb')) if queue: queue.enqueue(comment['_id']) else: queue = Queue() queue.enqueue(comment['_id']) pickle.dump(queue, open(bot.file, 'wb')) timestr = str(time.localtime()[3]) + ":" + str( time.localtime()[4]) print("> %s - Added comment to queue! Queue length: %s" % (timestr, len(queue)))