How to use the markovify.Text.from_dict function in markovify

To help you get started, we’ve selected a few markovify examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github lu-ci / apex-sigma-core / sigma / modules / utilities / mathematics / combinechains.py View on Github external
"""
    if len(pld.msg.mentions) >= 2:
        empty_chain = None
        chains = []
        chain_dicts = []
        with ThreadPoolExecutor() as threads:
            for target in pld.msg.mentions:
                target_chain = await cmd.db[cmd.db.db_nam].MarkovChains.find_one({'user_id': target.id})
                if not target_chain:
                    empty_chain = target
                    break
                chain_dicts.append(deserialize(target_chain.get("chain")))
            failed = False
            for chain_dict in chain_dicts:
                try:
                    chain_task = functools.partial(markovify.Text.from_dict, chain_dict)
                    chain = await cmd.bot.loop.run_in_executor(threads, chain_task)
                    chains.append(chain)
                except (ValueError, KeyError, AttributeError):
                    failed = True
            if not empty_chain:
                if not failed:
                    await cmd.bot.cool_down.set_cooldown(cmd.name, pld.msg.author, 20)
                    try:
                        combine_task = functools.partial(markovify.combine, chains)
                        combination = await cmd.bot.loop.run_in_executor(threads, combine_task)
                        sentence_function = functools.partial(combination.make_short_sentence, 500)
                        sentence = await cmd.bot.loop.run_in_executor(threads, sentence_function)
                    except (ValueError, KeyError, AttributeError):
                        sentence = None
                    if not sentence:
                        not_enough_data = '😖 I could not think of anything... I need more chain items!'
github lu-ci / apex-sigma-core / sigma / modules / utilities / mathematics / collector_clockwork.py View on Github external
coll = ev.db[ev.db.db_nam].CollectorQueue
    while True:
        if ev.bot.is_ready():
            now = arrow.utcnow().timestamp
            await coll.delete_many({'stamp': {'$lt': now - 3600}})
            cltr_items = await coll.find({}).to_list(None)
            for cltr_item in cltr_items:
                cl_usr = await ev.bot.get_user(cltr_item.get('user_id'))
                cl_chn = await ev.bot.get_channel(cltr_item.get('channel_id'))
                cl_ath = await ev.bot.get_user(cltr_item.get('author_id'))
                if cl_usr and cl_chn:
                    await coll.delete_one(cltr_item)
                    current_user_collecting = cl_usr.id
                    collection = await ev.db[ev.db.db_nam].MarkovChains.find_one({'user_id': cl_usr.id})
                    collection = collection.get('chain') if collection else None
                    chain = markovify.Text.from_dict(deserialize(collection)) if collection is not None else None
                    messages = []
                    pfx = await ev.db.get_guild_settings(cl_chn.guild.id, 'prefix') or ev.bot.cfg.pref.prefix
                    # noinspection PyBroadException
                    try:
                        async for log in cl_chn.history(limit=100000):
                            cnt = log.content
                            if log.author.id == cl_usr.id and len(log.content) > 8:
                                if not check_for_bot_prefixes(pfx, cnt) and not check_for_bad_content(cnt):
                                    cnt = cleanse_content(log, cnt)
                                    if cnt not in messages and cnt and len(cnt) > 1:
                                        messages.append(cnt)
                    except Exception as e:
                        print(e)
                        pass
                    try:
                        new_chain = markovify.Text(f'{". ".join(messages)}.')