première version
This commit is contained in:
22
markov.py
Normal file
22
markov.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from collections import defaultdict
|
||||
from random import choice, randrange
|
||||
|
||||
suivants = defaultdict(list)
|
||||
|
||||
with open("fra_mixed_2009_10K-sentences.txt", "r", encoding="utf-8") as fichier:
|
||||
for phrase in fichier:
|
||||
antepenultieme, penultieme = "", ""
|
||||
for word in phrase.split():
|
||||
suivants[(antepenultieme, penultieme)].append(word)
|
||||
antepenultieme, penultieme = penultieme, word
|
||||
|
||||
phrases = []
|
||||
for _ in range(randrange(1, 4)):
|
||||
antepenultieme, penultieme = "", ""
|
||||
phrase = []
|
||||
while mots_possibles := suivants[(antepenultieme, penultieme)]:
|
||||
mot_suivants = choice(mots_possibles)
|
||||
phrase.append(mot_suivants)
|
||||
antepenultieme, penultieme = penultieme, mot_suivants
|
||||
phrases.append(" ".join(phrase))
|
||||
print("\n".join(phrases))
|
||||
Reference in New Issue
Block a user