docs: def build_markov(cursor, cmdchar, ctrlchan, speaker=None, initial_run=False, debug=False):
      """Builds a markov dictionary."""
      if initial_run:
          cursor.query(Babble_last).delete()
      lastrow = cursor.query(Babble_last).first()
      if not lastrow:
          lastrow = Babble_last(last=0)
          cursor.add(lastrow)
      t = time.time()  # for debug
      messages = get_messages(cursor, cmdchar, ctrlchan, speaker, lastrow.last)
      # FIXME: count can be too low if speaker is not None
      curr = messages[-1].id if messages else None
      markov = generate_markov(cursor, 1, messages, initial_run)
      markov2 = generate_markov(cursor, 2, messages, initial_run)
      if debug:
          print('Generated markov in %f' % (time.time() - t))
          t = time.time()
      data, count_data = build_rows(cursor, 1, markov, initial_run)
      data2, count_data2 = build_rows(cursor, 2, markov2, initial_run)
      if debug:
          print('Rows built in %f' % (time.time() - t))
      if initial_run:
          t = time.time()  # for debug
          delete_tables(cursor)
          if debug:
              print('Tables deleted in %f' % (time.time() - t))
      t = time.time()  # for debug
      if initial_run and cursor.bind.dialect.name == 'postgresql':
          postgres_hack(cursor, 1, data)
          postgres_hack(cursor, 2, data2)
      else:
          data = [{'source': x[0], 'target': x[1], 'key': x[2], 'word': x[3], 'freq': x[4]} for x in data]
          cursor.bulk_insert_mappings(Babble, data)
          data2 = [{'source': x[0], 'target': x[1], 'key': x[2], 'word': x[3], 'freq': x[4]} for x in data2]
          cursor.bulk_insert_mappings(Babble2, data2)
      cursor.bulk_insert_mappings(Babble_count, count_data)
      cursor.bulk_insert_mappings(Babble_count, count_data2)
      if debug:
          print('Inserted rows in %f' % (time.time() - t))
      if curr is not None:
          lastrow.last = curr
      if initial_run:
          if debug:
              t = time.time()
          key_index = Index('ix_babble_key', Babble.key)
          key_index2 = Index('ix_babble2_key', Babble2.key)
          key_index.create(cursor.connection())
          key_index2.create(cursor.connection())
          if debug:
              print('Created index in %f' % (time.time() - t))
      t = time.time()  # for debug
      cursor.commit()
      if debug:
          print('Commited in %f' % (time.time() - t))