当前位置: 首页>>代码示例>>Python>>正文


Python Dataset.random_phrase方法代码示例

本文整理汇总了Python中dataset.Dataset.random_phrase方法的典型用法代码示例。如果您正苦于以下问题:Python Dataset.random_phrase方法的具体用法?Python Dataset.random_phrase怎么用?Python Dataset.random_phrase使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在dataset.Dataset的用法示例。


在下文中一共展示了Dataset.random_phrase方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: AudioBot

# 需要导入模块: from dataset import Dataset [as 别名]
# 或者: from dataset.Dataset import random_phrase [as 别名]
class AudioBot(telebot.TeleBot):
    def __init__(self, token, description_file):
        super(AudioBot, self).__init__(token, threaded=False)
        with open(description_file, encoding="utf-8") as fp:
            description = json.load(fp)
        self.mention_words = set(description['mention_words'])
        self.dataset = Dataset(description_file)

    def audio_list(self, msg):
        logger.info("%s: sending phrase list to user %s" % (msg.text, user_log_string(msg)))
        text = ["Phrase list (use '/say ID' to choose phrase):"]
        for i, title in self.dataset.list_files():
            text.append("*[%2d]*  _%s_" % (i, title))
        self.reply_to(msg, "\n".join(text), parse_mode="Markdown")

    def top_phrases(self, msg):
        logger.info("%s: sending top phrases to user %s" % (msg.text, user_log_string(msg)))
        lines = ["Top phrases (use '/say ID' to choose phrase):"]
        for audio_id, count in self.dataset.top_phrases.most_common(5):
            lines.append(
                "*[%2d]*  _%s_ (%d reproductions)" % (audio_id, self.dataset.get_audio_data(audio_id)['title'], count))
        self.reply_to(msg, "\n".join(lines), parse_mode="Markdown")

    def send_phrase(self, msg, audio_id):
        if 0 <= audio_id < len(self.dataset.data_keys):
            audio, duration = self.dataset.get_phrase(audio_id)
            logger.info("%s: sending phrase #%d to user %s" % (msg.text, audio_id, user_log_string(msg)))
            self.send_audio(msg.chat.id, audio, duration)
        else:
            bot.send_random_phrase(msg)

    def send_random_phrase(self, msg):
        logger.info("%s: sending random phrase to user %s" % (msg.text, user_log_string(msg)))
        audio, duration = self.dataset.random_phrase()
        self.send_audio(msg.chat.id, audio, duration)

    def search_mentions(self, msg):
        msg_plain = sanitize_string(msg.text)
        msg_words = set(msg_plain.split())
        if not self.mention_words.intersection(msg_words):
            return
        logger.info("mention: responding to user %s" % user_log_string(msg))
        audio, duration = self.dataset.random_phrase(msg_words)
        self.send_audio(msg.chat.id, audio, duration)
开发者ID:adrinieto,项目名称:PhraseBot,代码行数:46,代码来源:phrasebot.py


注:本文中的dataset.Dataset.random_phrase方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。