A machine learning project that generates random sentences using n-gram language models trained on the Brown Corpus.
This project implements an n-gram language model to generate syntactically plausible sentences. It processes the Brown Corpus from NLTK, builds n-gram frequency models, and uses them to generate new sentences by selecting words based on probability distributions derived from the training data.
- Flexible n-gram support: Generate sentences using bigrams (n=2), trigrams (n=3).
- Brown Corpus training: Leverages NLTK's Brown Corpus for robust linguistic patterns
- Customizable output: Control the number of sentences generated and maximum sentence length
- Statistical word selection: Uses n-gram frequency counts to select contextually appropriate next words
- Python 3.x
- NLTK (Natural Language Toolkit)
Enter the number of sentences to generate (M): 3
Enter the n-gram model to use (2 for bigram, 3 for trigram): 2
Enter the maximum length of a sentence (maxLen): 15This will generate 3 sentences using a bigram model with a maximum of 15 words each.
- Preprocessing: The Brown Corpus is tokenized and converted to lowercase, with non-alphanumeric characters removed
- N-gram Model Building: Frequency counts are collected for all n-grams in the corpus
- Sentence Generation:
- Start with a random n-gram from the training set
- Iteratively select the next word based on which word most frequently follows the current context in the training data
- Continue until the maximum length is reached or no valid continuation exists
NgramSentenceGenerator/
├── main.py # Main script with all functions
└── README.md # Project documentation
preprocess_corpus(): Loads and preprocesses the Brown Corpusbuild_ngram_model(sentences, n): Creates frequency counts for all n-gramsgenerate_sentence(ngram_model, vocabulary, n, max_len): Generates a single sentencemain(M, N, maxLen): Orchestrates the sentence generation process
- Generated sentences may not always be grammatically perfect or semantically meaningful
- Uses greedy word selection (always picks the most frequent next word) rather than probabilistic sampling
- Performance may vary depending on n-gram size and corpus coverage
- Implement probabilistic sampling instead of greedy selection
- Add support for custom training corpora
- Cache n-gram models for faster repeated runs
- Add filtering for more natural-sounding output
- Implement smoothing techniques for better generalization