import nltk
from nltk import bigrams, FreqDist
from nltk.corpus import udhr
import random
import re

def correct(str_in):
    str_out = re.sub(r'\s+([.,;!?\'"])', r'\1', str_in)
    return str_out

nltk.download('udhr')

# Charger le corpus de texte udhr en français
corpus = udhr.words('French_Francais-Latin1')

# Créer des bigrammes et calculer la fréquence des occurrences
bi_grams = list(bigrams(corpus))
frequency_dist = nltk.FreqDist(bi_grams)

# Générer du texte à l'aide d'une chaîne de Markov
word = random.choice(corpus)
output = ''
for _ in range(100):
    if word != '.' or word != ',' or word != '\'':
        output += ' ' + word
    else:
        output += word
    next_words = [w[1] for w in bi_grams if w[0] == word]
    if not next_words:
        break
    next_word = random.choice(next_words)
    word = next_word
output = correct(output)    
print(output)
