import random import nltk

Download the Brown corpus if not already downloaded

nltk.download('brown')

def generate_sentence(): # Load the Brown corpus and extract a random word words = nltk.corpus.brown.words() word = random.choice(words)

# Determine the word's part of speech
pos = nltk.pos_tag([word])[0][1]
if pos.startswith('N'):
    noun = word
    verb = random.choice([w for w, t in nltk.corpus.brown.tagged_words() if t.startswith('V')])
    adjective = random.choice([w for w, t in nltk.corpus.brown.tagged_words() if t.startswith('J')])
elif pos.startswith('V'):
    verb = word
    noun = random.choice([w for w, t in nltk.corpus.brown.tagged_words() if t.startswith('N')])
    adjective = random.choice([w for w, t in nltk.corpus.brown.tagged_words() if t.startswith('J')])
elif pos.startswith('J'):
    adjective = word
    noun = random.choice([w for w, t in nltk.corpus.brown.tagged_words() if t.startswith('N')])
    verb = random.choice([w for w, t in nltk.corpus.brown.tagged_words() if t.startswith('V')])

# Construct the sentence
adverb = random.choice([w for w, t in nltk.corpus.brown.tagged_words() if t.startswith('RB')])
preposition = random.choice([w for w, t in nltk.corpus.brown.tagged_words() if t.startswith('IN')])
article = random.choice(['a', 'an', 'the'])
object_noun = random.choice([w for w, t in nltk.corpus.brown.tagged_words() if t.startswith('N')])

sentence = f"{article.capitalize()} {adjective} {noun} {verb} {adverb} {preposition} {article} {object_noun}."
return sentence

if name == 'main': for i in range(10): print(generate_sentence())