def load_words(session, directory): folder = os.path.join(directory, 'words') for filename in os.listdir(folder): if filename.endswith('.csv'): with open(os.path.join(folder, filename), newline='') as csv_file: reader = csv.DictReader(csv_file, delimiter=',', quotechar='"') for word_csv in reader: word = Word.create(session, **word_csv) session.add(word) session.commit()
def token_value(self, token): energy = 10 word = None if token not in self.words: try: word = Word.get(word=token) except Word.DoesNotExist: word = Word.create(word = token, last_used = datetime.now(), times = 1) else: scale = self.energy_scale(self.words[token].last_used, datetime.now()) energy *= scale self.words[token].times += 1 logger.debug("[{}] Scale: {} | Energy: {}".format(token, scale, energy)) # After all that, save the word. word.save() if energy < 1: energy = 1 return energy