๐Ÿ’ฌ
B.TECH III YEAR โ€ข II SEMESTER โ€ข R22

Natural Language Processing - Lab Manual

Natural Language Processing Laboratory โ€ข Program Solution

๐Ÿ“˜ R22 Regulation ๐ŸŽ“ III Year ๐Ÿ“š II Semester ๐Ÿ’ฌ NLP Using Python ๐Ÿงช Lab Program
Selected Program

Write a Python program to implement Porter stemmer algorithm for stemming.

โ† All NLP Programs

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.

The compiler is embedded using OneCompiler. Your Python program runs inside the embedded Python environment; Study Glance does not execute Python code directly on your web server. If the program asks for input, type the value in the STDIN box before clicking Run. Programs that use the NLTK library need the package and its data (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.
โ† Back to NLP Lab Programs ๐Ÿ“š All Lab Manuals