コード例 #1
0
    def __init__(self, brain_db, chain_length):
        self.brain_db = brain_db
        self.chain_length = chain_length

        # Holds markov chain data. key(tuple(words[chain_length])) -> {word_choice1: count, word_choice2: count}
        self.sqlite_dict = DatabaseDictionary(self.brain_db,
                                              MARKOV_VALUE_PROPS,
                                              MarkovDictionaryValue)
コード例 #2
0
ファイル: markovbrain.py プロジェクト: Gothealsforu/dagbot
 def load_brain(self):
     print 'Brain loading...'
     as_process(markov_dictionary_from_file,
                (self.temp_db_file, self.brain_file, self.chain_length
                 ))  # Shields main process from intermediate memory used
     self.markov = DatabaseDictionary(self.temp_db_file, MARKOV_VALUE_PROPS)
     print 'Brain loaded.'
     print 'Markov dictionary has %i keys' % (len(self.markov), )
コード例 #3
0
 def sync_with_file(self, brain_file):
     if len(self.sqlite_dict) > 0:
         return
     self.sqlite_dict.close()
     as_process(markov_dictionary_from_file,
                (self.brain_db, brain_file, self.chain_length))
     self.sqlite_dict = DatabaseDictionary(self.brain_db,
                                           MARKOV_VALUE_PROPS,
                                           MarkovDictionaryValue)
コード例 #4
0
ファイル: markovcommon.py プロジェクト: Gothealsforu/dagbot
def markov_dictionary_from_file(temp_db_file, brain_file, chain_length):
    print 'Creating markov chains from %s' % brain_file

    line_count = count_lines(brain_file)

    temp_dict = {}
    for c in xrange(1, chain_length + 1):
        print 'Creating markov chains of length %i' % c
        with open(brain_file, 'r') as f:
            progress_bar = ProgressBar(line_count)
            for line in f:
                add_to_markov_dictionary(temp_dict, c,
                                         line.strip().decode('utf-8').lower())
                progress_bar.update()

    print 'Populating database dictionary with markov chains'
    db_dict = DatabaseDictionary(temp_db_file, MARKOV_VALUE_PROPS)
    db_dict.begin()
    db_dict.replace(temp_dict)
    db_dict.commit()
    db_dict.close()