- Overview
- Features
- Getting Started
- Data Sources
- How It Works
- Example Usage
- Example Output
- Custom Text Analysis
This project implements a language model in Python that analyzes two corpora of text. It generates text in the style of the corpus authors and compares the probabilities and frequencies of unigrams and bigrams within the corpora.
- Text Analysis: Analyze and compare the linguistic patterns of two corpora.
- Text Generation: Generate text that mimics the style of the authors in the corpora.
- Statistical Comparison: Compute and compare unigram and bigram probabilities and frequencies.
-
Clone the Repository:
git clone https://github.com/cdundon123/langmodel_110.git
-
Install Dependencies: Ensure you have Python 3.8 installed. The project uses
matplotlibfor generating visualizations. Ensure it is installed to view the outputs:pip install matplotlib
-
Run the Language Model: Execute the main script to analyze the corpora or generate text:
python language_model/language_func.py.py
-
Run the Tests: Verify the functionality of the language model using:
python -m unittest language_model/tests.py
The data/ directory contains the following text corpora:
- Andersen's Fairy Tales: Cleaned and raw versions of Hans Christian Andersen's works.
- Grimm's Fairy Tales: Cleaned and raw versions of the Brothers Grimm's works.
- Hamilton: A text corpus for additional analysis
- Test Files: Small sample files (
test1.txt,test2.txt) for debugging and testing.
- Text Preprocessing: The raw text is cleaned and tokenized into unigrams and bigrams.
- Probability Calculation: The model calculates unigram and bigram probabilities for each corpus.
- Text Generation: Using the probabilities, the model generates text in the style of the corpus authors.
To generate text in the style of a specific corpus:
python [language_func.py](http://_vscodecontentref_/6) --generate --corpus [andersen_clean.txt](http://_vscodecontentref_/7)To compare unigram and bigram probabilities:
python [language_func.py](http://_vscodecontentref_/8) --compare --corpus1 [andersen_clean.txt](http://_vscodecontentref_/9) --corpus2 [grimm_clean.txt](http://_vscodecontentref_/10)The output of the language model includes both generated text and statistical comparison visualizations:
-
Sample output from the text generation function:
the others shiver below by , who could not drink , and good , " how many had finished both father , and rest , and so the maid : but then it , all the door , and the garden , i never heard this way out .
-
Sample output from the probability comparison function:
The histogram visualizations are generated using the
matplotliblibrary. Ensure it is installed to view the output.
You can also analyze corpora beyond the provided samples. Either add your own .txt file OR upload the text of a new book:
- Find the text of a book you want online and download it into a
.txtfile. - Read the text into a string using:
with open('path_to_your_file.txt', 'r') as file: text = file.read()
- Create the proper book format with
cleanBookData:python cleaned_text = cleanBookData(text)
- Save the book into a new file, then run
loadBookon the new file to generate a corpus. - Call functions like
unigramProborbigramProbto analyze the new corpus!
