0
with open("text.txt", "r") as file:
contents = file.read().replace('\n',' ')
words = contents.split(' ')
wordsDict = {}
for i in range(len(words) - 1):
wordsDict[words[i]] = words[i + 1]
def assemble():
start = words[random.randint(0, len(words))]
print(start.capitalize())
assemble()
現在、マルコフチェーンエスクプロジェクトを作成中です。私はこのコードを実行したとき、私は次のように見て、辞書のために期待していた:リストのインデックスi + 1に辞書の値を設定すると、最後に値が出現した後に項目が設定されます。
(読みTEXT.TXT場合:犬は、ラットの家に猫を追いかけながら、猫がネズミを追いかけた)
{'the': 'cat', 'cat': 'chased', 'chased': 'the', 'the': 'rat', 'rat': 'while', 'while': 'the', 'the': 'dog', 'dog': 'chased', 'chased': 'the', 'the': 'cat', 'cat': 'into', 'into': 'the', 'the': 'rat', 'rat': 'house'}
を
代わりに、あなたはパターンを感知しない場合、私は
{'the': 'rat', 'cat': 'into', 'chased': 'the', 'rat': 'house', 'while': 'the', 'dog': 'chased', 'into': 'the'}
を取得し、それは価値が配列にちょうど次の項目ではないことだ、それは単語の最後のoccuranceの後に次のアイテムです。この場合、我々の最初のキー&値のペアは 'the': 'rat'です。なぜなら、theの最後の出現にはラットが続くからです。
なぜこのようなことが起こるのか、どのように修正するのかはわかりません。
ヒント:あなたのマルコフ連鎖は、Xのすぐ後に続く可能な値Yをモデリングする必要があります。 – donkopotamus