當前位置: 首頁>>代碼示例>>Python>>正文


Python Markov.speak方法代碼示例

本文整理匯總了Python中markov.Markov.speak方法的典型用法代碼示例。如果您正苦於以下問題:Python Markov.speak方法的具體用法?Python Markov.speak怎麽用?Python Markov.speak使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在markov.Markov的用法示例。


在下文中一共展示了Markov.speak方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: get_message_text

# 需要導入模塊: from markov import Markov [as 別名]
# 或者: from markov.Markov import speak [as 別名]
            if extension != ".json": continue
            result["#" + channel_name] = os.path.join(dirpath, history_file)
        return result
    return {}

def get_message_text(message):
    """Returns the text value of `message` if it is a valid text message, or `None` otherwise"""
    if message.get("type") == "message" and isinstance(message.get("ts"), str):
        if isinstance(message.get("text"), str) and isinstance(message.get("user"), str): # normal message
            return server_text_to_sendable_text(message["text"])
        if message.get("subtype") == "message_changed" and isinstance(message.get("message"), dict) and isinstance(message["message"].get("user"), str) and isinstance(message["message"].get("text"), str): # edited message
            return server_text_to_sendable_text(message["message"]["text"])
    return None

markov = Markov(2) # Markov model with 2 word look-behind

entries = open("kjv.txt", "r").read().split("\n")
matcher = re.compile(Markov.WORD_PATTERN, re.IGNORECASE)
for message in (matcher.findall(m) for m in entries):
    markov.train([m.lower() for m in message], 3)

for channel_name, history_file in get_history_files().items():
    with open(history_file, "r") as f:
        for entry in f:
            text = get_message_text(json.loads(entry))
            if text is not None:
                markov.train(Markov.tokenize_text(sendable_text_to_text(text)))

for x in range(10000):
    print(Markov.format_words(markov.speak()))
開發者ID:wlstrickland,項目名稱:botty-bot-bot-bot,代碼行數:32,代碼來源:generate_sample.py


注:本文中的markov.Markov.speak方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。