def generator(conditions=None, ordered_conditions=None, maxlength=100): if type(ordered_conditions) is not list: ordered_conditions = [ordered_conditions] with database.connection(): _cond = _conditions(Word.previous_word >> None, conditions, ordered_conditions[0]) word = Word.select().join(Speech).where(_cond).order_by( database.random()).first() if word: pprev_word = None prev_word = word.word payload = word.word i = 1 while i < maxlength: pprev_cond = database.none_or(Word.pprevious_word, pprev_word) prev_cond = database.none_or(Word.previous_word, prev_word) _cond = _conditions(pprev_cond, prev_cond, conditions) try: _cond = _conditions(_cond, ordered_conditions[i]) except IndexError: pass word = Word.select().join(Speech).where(_cond).order_by( database.random()).first() if word: payload += " " + word.word if word.next_word is None: break pprev_word = prev_word prev_word = word.word else: break i += 1 return payload
def action(self, source, target, message): if message[0][0] in ["!", ",", ";", "~", "#", ".", "§"]: return with database.connection(): with database.transaction(): for i, word in enumerate(message): pprev_word = message[i - 2] if i > 1 else None prev_word = message[i - 1] if i > 0 else None next_word = message[i + 1] if i < len(message) - 1 else None nnext_word = message[i + 2] if i < len(message) - 2 else None pprev_cond = database.none_or(Word.pprevious_word, pprev_word) prev_cond = database.none_or(Word.previous_word, prev_word) next_cond = database.none_or(Word.next_word, next_word) nnext_cond = database.none_or(Word.nnext_word, nnext_word) word = Word.select().where((pprev_cond) & (prev_cond) & (next_cond) & (nnext_cond) & (Word.word == word)).first() if not word: word = Word.create(pprevious_word=pprev_word, previous_word=prev_word, next_word=next_word, nnext_word=nnext_word, word=message[i]) word.save() Speech.create(word=word, user=target, chan=source).save() self.check_speak(source, target, message)
def read_words(filename): file = open(filename, 'r') print('creating words from %s' % filename) for line in file: word = Word(word=line, anagram=''.join(sorted(list(line[0:len(line) - 1])))) try: word.save() except IntegrityError as e: pass
def add_file_word(update: Updater, context: CallbackContext, user: User): document = update.effective_message.document if document.mime_type != 'text/plain': return update.effective_user.send_message('send text file !') out = update.effective_message.document.get_file().download_as_bytearray() for word in out.decode('utf-8').split('\n'): word = word.strip() if not word: continue Word.create(user=user, value=word) update.effective_user.send_message( 'Your words added successfully. It will be reminded repetitively ⌛')
def next_word(updater: Updater, context: CallbackContext, user: User): query = updater.callback_query word_id = query.data.split()[1] word = Word.get_or_none(id=word_id) if not word: query.answer(text="word not found") return word.go_next_level() user.answered() query.answer(text="success") query.edit_message_caption(updater.effective_message.caption_markdown + ' ✅', parse_mode="Markdown")
def back_word(updater: Updater, context: CallbackContext, user: User): query = updater.callback_query word_id = query.data.split()[1] word = Word.get_or_none(id=word_id) if not word: query.answer(text="word not found") return word.back_to_first_level() user.answered() query.answer(text="success") query.edit_message_caption(caption="Read the definition again. 💪\n\n" + get_def_markdown(word.value), parse_mode="Markdown")
def add_word(update: Updater, context: CallbackContext, user: User): word = Word.create(user=user, value=update.effective_message.text) # word.go_next_level() # url = f"[definition](https://www.oxfordlearnersdictionaries.com/us/definition/english/{word.value.lower()})" # caption = "Read this word or delete this word if its known.\n\n" + url # replay_markup = [ # [InlineKeyboardButton("Next", callback_data="next " + str(word.id)),InlineKeyboardButton("Delete", callback_data="delete " + str(word.id))] # ] # word.set_reminded() # update.effective_user.send_photo(photo=generate_image(word.value),caption=caption, reply_markup=InlineKeyboardMarkup(replay_markup),parse_mode="Markdown") update.effective_user.send_message( 'Your word added successfully. It will be reminded repetitively ⌛') return ConversationHandler.END
def _test_value(text, typeV, user): counted_words = _text_count_words(text) words = counted_words.keys() weight = 0 # hope I correctly get max portion size i, portion, wcount = 0, 30, len(words) while i < wcount: wportion = words[i:i + portion] i += portion ws = Word.gql("WHERE stem IN :1", wportion).fetch(portion) for word in ws: aKind = word.grabKind(typeV, user) if aKind.total == 0: continue word_weight = float(aKind.spam) / aKind.total word_count = counted_words[word.stem] validity = float(word_count) / wcount weight += validity * word_weight weight *= 100 return weight
def make(self): with open("data/categories.csv") as inCategories: for line in inCategories: objLine = line.split(",") category = Category(objLine[1].strip(" "), objLine[2].strip("\n").strip(" ")) db.session.add(category) db.session.commit() with open("data/words.csv") as inWords: for line in inWords: objLine = line.strip("\n").strip("\r").split("\t") if objLine[2].strip(" ") != "": print objLine[2].strip("\n").strip(" ") category = db.session.query(Category).filter( Category.id == int(objLine[2].strip("\n").strip( " "))).first() # print objLine[4] word = Word(objLine[0].strip(" "), objLine[1].strip(" "), objLine[3].strip(" "), objLine[4].strip(" ")) category.words.append(word) db.session.commit()
def _update_text(text, typeV, user, clean=False): counted_words = _text_count_words(text) # save data writeList = [] for w in counted_words.keys(): count = counted_words[w] spam_count = count if clean == True: spam_count = 0 word = Word.get_by_id(w) if not word: word = Word(id=w) word.stem = w word.put() scanType = word.grabKind(typeV, user) scanType.total += count scanType.spam += spam_count writeList.append(scanType) ndb.put_multi(writeList) return len(counted_words.keys())
f = open(args.file) for line in f: line = line.strip() if line: char = line[0] try: char_obj = session.query(Char).filter(Char.char == char).one() changed += 1 except NoResultFound: char_obj = Char(char=char) session.add(char_obj) added += 1 char_obj.pairs = char + char.upper() + line[1:] session.commit() print '{} char added, {} chars changed'.format(added, changed) else: added, existed = 0, 0 f = open(args.file) for line in f: line = line.strip() if line and not has_stop_char(line): word = line.lower() try: session.query(Word).filter(Word.word == word).one() existed += 1 except NoResultFound: session.add(Word(word=word, length=len(word), language=lang)) session.commit() added += 1 print '{} words added, {} existed already'.format(added, existed)