Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

N-gram Sentence Generator

A machine learning project that generates random sentences using n-gram language models trained on the Brown Corpus.

Overview

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.

Features

  • 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

Requirements

  • Python 3.x
  • NLTK (Natural Language Toolkit)

Example

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): 15

This will generate 3 sentences using a bigram model with a maximum of 15 words each.

How It Works

  1. Preprocessing: The Brown Corpus is tokenized and converted to lowercase, with non-alphanumeric characters removed
  2. N-gram Model Building: Frequency counts are collected for all n-grams in the corpus
  3. 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

Project Structure

NgramSentenceGenerator/
├── main.py          # Main script with all functions
└── README.md        # Project documentation

Functions

  • preprocess_corpus(): Loads and preprocesses the Brown Corpus
  • build_ngram_model(sentences, n): Creates frequency counts for all n-grams
  • generate_sentence(ngram_model, vocabulary, n, max_len): Generates a single sentence
  • main(M, N, maxLen): Orchestrates the sentence generation process

Limitations

  • 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

Future Improvements

  • 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

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages