Created
November 20, 2017 11:32
-
-
Save TonyMooori/3f14f26d2a02130a38dce23204626fa1 to your computer and use it in GitHub Desktop.
マルコフ連鎖で文字列を生成するプログラム
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| lines = [] | |
| # 直前3文字から次の文字を予測する | |
| n = 3 | |
| # 1行に一文が並んだ文章 | |
| with open("gingatetsudono_yoru.txt","r") as f: | |
| for line in f.readlines(): | |
| if len(line) < n+1: | |
| continue | |
| lines.append(line) | |
| dic = {} | |
| for line in lines: | |
| for i in range(len(line)-n): | |
| key = line[i:i+n] | |
| c = line[i+n] | |
| if not key in dic.keys(): | |
| dic[key] = [] | |
| # 直前n文字を取ってきてはキーにしてリストに追加 | |
| dic[key].append(c) | |
| # 文章生成 | |
| def generate(dic,lines): | |
| # どれかの文の冒頭n文字を取ってくる(出発点) | |
| w = random.choice(lines)[:n] | |
| key = w | |
| while True: | |
| # 改行がでるまで直前n文字から次の文字をランダムに決定する | |
| c = random.choice(dic[key]) | |
| key = key[1:]+c | |
| w = w + c | |
| if c == '\n': | |
| break | |
| return w | |
| for i in range(100): | |
| print("-"*20) | |
| print(generate(dic,lines)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment