This project is part of the INFO6145 Data Science and Machine Learning course at Fanshawe College. The goal is to create an N-gram extractor that processes text files, generates 2-grams, 3-grams, and 4-grams, and retrieves their definitions using the WordNet lexical database.
- Python 3.x
- NLTK library
- WordNet data files (
NounsData.txtandNounsIndex.txt) - Test files (
test_text1.txttotest_text8.txt) in theS24 test_filesfolder
- Clone the repository.
- Ensure
NounsData.txtandNounsIndex.txtare in the same directory as the script. - Place the test files in the
S24 test_filesfolder.
Run the script to process the test files and generate n-grams with their definitions.
python ngram_extractor.pyReads the input text file and returns the content as a string.
- Parameters:
file_path(str): Path to the input text file.
- Returns:
str: Content of the input file.
Tokenizes the input text into sentences.
- Parameters:
text(str): The input text as a string.
- Returns:
list: List of sentences.
Tokenizes a sentence into words.
- Parameters:
sentence(str): A single sentence as a string.
- Returns:
list: List of words.
Generates n-grams from the list of words.
- Parameters:
words(list): A list of words.n(int): The number of words in each n-gram.
- Returns:
list: List of n-grams.
Loads WordNet index and data files into dictionaries.
- Parameters:
index_file(str): Path to the WordNet index file.data_file(str): Path to the WordNet data file.
- Returns:
tuple: Two dictionaries, one for the index and one for the data.
Looks up the definition of a token using the WordNet data.
- Parameters:
token(str): The n-gram token.index_dict(dict): Dictionary containing WordNet index data.data_dict(dict): Dictionary containing WordNet data definitions.
- Returns:
str: Definition of the token if found, elseNone.
Prints n-grams along with their definitions.
- Parameters:
ngrams(dict): Dictionary containing n-grams.definitions(dict): Dictionary containing definitions for the n-grams.
This project is licensed under the MIT License. See the LICENSE file for details.
- NLTK: Natural Language Toolkit. (n.d.). Retrieved from https://www.nltk.org/
- WordNet: Princeton University "About WordNet." WordNet. Princeton University. 2010.