๐Ÿ’ฌ
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 Python Program for

โ† All NLP Programs

Solution :

DESCRIPTION:

Word Analysis :
Word analysis (morphological analysis) breaks a word into its smallest meaningful parts: the prefix (un-, re-, dis-), the root (the base word) and the suffix (-ing, -ed, -ness). It also measures properties of the word such as its length and the number of vowels and consonants. The program below uses lists of common prefixes and suffixes and finds the longest affix that matches, leaving at least three letters for the root.

Word Generation :
Word generation is the reverse process: new word forms are produced from a root by applying rules. The program applies simple inflection rules to a list of regular verbs to generate the third person singular, the past tense and the continuous (-ing) form, for example study → studies, studied, studying and stop → stops, stopped, stopping.

a) Word Analysis

PROGRAM: ( word_analysis.py )

 
# Word Analysis: find the prefix, root and suffix of a word
prefixes = ["un", "re", "dis", "pre", "mis", "in", "im"]
suffixes = ["ness", "ful", "less", "able", "ment", "ing", "ed", "er", "ly", "es", "s"]
vowels = "aeiou"

word = input("Enter a word: ").lower()

# longest matching prefix (the root must keep at least 3 letters)
prefix = ""
for p in prefixes:
    if word.startswith(p) and len(word) - len(p) >= 3 and len(p) > len(prefix):
        prefix = p

# longest matching suffix (the root must keep at least 3 letters)
suffix = ""
for s in suffixes:
    if word.endswith(s) and len(word) - len(prefix) - len(s) >= 3 and len(s) > len(suffix):
        suffix = s

root = word[len(prefix):len(word) - len(suffix)]

vowel_count = sum(1 for ch in word if ch in vowels)
consonant_count = sum(1 for ch in word if ch.isalpha() and ch not in vowels)

print("\nWord analysis of '" + word + "'")
print("Length     :", len(word))
print("Vowels     :", vowel_count)
print("Consonants :", consonant_count)
print("Prefix     :", prefix if prefix else "-")
print("Root       :", root)
print("Suffix     :", suffix if suffix else "-")

OUTPUT:

 
$ python word_analysis.py
Enter a word: unfaithful

Word analysis of 'unfaithful'
Length     : 10
Vowels     : 4
Consonants : 6
Prefix     : un
Root       : faith
Suffix     : ful

$ python word_analysis.py
Enter a word: disagreement

Word analysis of 'disagreement'
Length     : 12
Vowels     : 5
Consonants : 7
Prefix     : dis
Root       : agree
Suffix     : ment

b) Word Generation

PROGRAM: ( word_generation.py )

 
# Word Generation: generate word forms from a root using rules
vowels = "aeiou"

def is_cvc(w):
    # short word ending in consonant-vowel-consonant (stop -> stopped)
    return (3 <= len(w) <= 4 and w[-1] not in vowels + "wxy"
            and w[-2] in vowels and w[-3] not in vowels)

def third_person(w):
    if w.endswith(("s", "x", "z", "ch", "sh")):
        return w + "es"
    if w.endswith("y") and w[-2] not in vowels:
        return w[:-1] + "ies"
    return w + "s"

def past_tense(w):
    if w.endswith("e"):
        return w + "d"
    if w.endswith("y") and w[-2] not in vowels:
        return w[:-1] + "ied"
    if is_cvc(w):
        return w + w[-1] + "ed"
    return w + "ed"

def continuous(w):
    if w.endswith("e") and not w.endswith("ee"):
        return w[:-1] + "ing"
    if is_cvc(w):
        return w + w[-1] + "ing"
    return w + "ing"

roots = ["play", "study", "watch", "stop", "love", "try"]

print("{:<8} {:<12} {:<10} {:<12}".format("Root", "3rd person", "Past", "Continuous"))
print("-" * 44)
for r in roots:
    print("{:<8} {:<12} {:<10} {:<12}".format(r, third_person(r), past_tense(r), continuous(r)))

OUTPUT:

 
$ python word_generation.py
Root     3rd person   Past       Continuous  
--------------------------------------------
play     plays        played     playing     
study    studies      studied    studying    
watch    watches      watched    watching    
stop     stops        stopped    stopping    
love     loves        loved      loving      
try      tries        tried      trying      


๐Ÿ’ป Run This Python Program

Write Python Program for
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