Write a Python program to implement Porter stemmer algorithm for stemming.
Solution :
DESCRIPTION:
The Porter Stemming Algorithm is a rule-based stemming technique used in text preprocessing to reduce words to their root or base form. It works by iteratively removing common suffixes such as -ing, -ed, -s, and -ies based on a set of linguistic rules. This helps in reducing vocabulary size and improves the efficiency of information retrieval and text mining systems.
PROGRAM: ( porter_stemmer_alg.py )
def porter_stem(word):
word = word.lower()
# Step 1a
if word.endswith("sses"):
word = word[:-2]
elif word.endswith("ies"):
word = word[:-2]
elif word.endswith("ss"):
pass
elif word.endswith("s"):
word = word[:-1]
# Step 1b
if word.endswith("ing"):
word = word[:-3]
elif word.endswith("ed"):
word = word[:-2]
# Step 1c
if word.endswith("y"):
word = word[:-1] + "i"
return word
# input words
words = ["horsess", "flies", "agreed", "running", "happy", "studies"]
for w in words:
print(w, "->", porter_stem(w))
OUTPUT:
$ python porter_stemmer_alg.py
horsess -> horsess
flies -> fli
agreed -> agre
running -> runn
happy -> happi
studies -> studi
๐ป Run This Python Program
Write a Python program to implement Porter stemmer algorithm for stemming.
The selected Python program is loaded
automatically into the editor.
Edit the program, provide input through
STDIN if required
and click Run.
nltk.download()).
If NLTK is not available in the embedded compiler, use
Copy Program and run the program on your
computer (pip install nltk) or in Google Colab.