Group Information Retrieval Practicals

Q1: Write a program to preprocess a text document by tokenizing it, removing stop words, and stemming the remaining words.

Text Preprocessing: Stop-word Removal and Stemming

Solution and implementation for Q1 from Information Retrieval (ir).

1_text_preprocessing.py Download
"""Remove stop words and stem words in a text document."""

import nltk
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize


nltk.download("punkt", quiet=True)
nltk.download("stopwords", quiet=True)

document = "Machine learning cannot be mastered in two months; it needs years of practice and study."
tokens = word_tokenize(document.lower())
filtered = [word for word in tokens if word.isalpha() and word not in set(stopwords.words("english"))]
stemmer = PorterStemmer()
stemmed = [stemmer.stem(word) for word in filtered]

print("Tokens:", tokens)
print("After stop-word removal:", filtered)
print("After stemming:", stemmed)

Other Questions in Information Retrieval

See All Available Questions
Download