Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views2 pages

Darshnlp 4

This document outlines a Python program that utilizes the NLTK library to analyze words by identifying their prefixes and suffixes. It includes functions to find the root of a word and generate new words by combining different prefixes and suffixes. The program prompts the user for a word and displays the identified prefix, root, suffix, and examples of newly generated words.

Uploaded by

dhakeyashashree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Darshnlp 4

This document outlines a Python program that utilizes the NLTK library to analyze words by identifying their prefixes and suffixes. It includes functions to find the root of a word and generate new words by combining different prefixes and suffixes. The program prompts the user for a word and displays the identified prefix, root, suffix, and examples of newly generated words.

Uploaded by

dhakeyashashree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

EXPERIMENT NO.

INPUT:

import nltk
from nltk.corpus import wordnet
# Download required NLTK data if not already present
nltk.download('wordnet')
nltk.download('omw-1.4')
# Common prefixes and suffixes (can be extended)
prefixes = ['un', 're', 'in', 'im', 'dis', 'pre', 'non', 'over', 'mis', 'sub', 'inter', 'super', 'semi', 'anti', 'auto']
suffixes = ['ing', 'ed', 'ly', 'es', 's', 'ment', 'ness', 'ful', 'less', 'able', 'ible', 'al', 'er', 'or', 'ism', 'ist', 'ize', 'ise']
def find_prefix(word):
for pre in sorted(prefixes, key=len, reverse=True):
if word.startswith(pre):
return pre
return ''
def find_suffix(word):
for suf in sorted(suffixes, key=len, reverse=True):
if word.endswith(suf):
return suf
return ''
def get_root(word, prefix, suffix):
# Remove prefix and suffix to find root
root = word
if prefix:
root = root[len(prefix):]
if suffix:
root = root[:-len(suffix)]
return root
def generate_words(root, add_prefixes, add_suffixes):
generated = []
for pre in add_prefixes:
generated.append(pre + root)
for suf in add_suffixes:
generated.append(root + suf)
# Combine prefix + root + suffix also
for pre in add_prefixes:
for suf in add_suffixes:
generated.append(pre + root + suf)
return generated
def main():
text = input("Enter a word: ").strip().lower()
prefix = find_prefix(text)
suffix = find_suffix(text)
root = get_root(text, prefix, suffix)
print(f"Input word: {text}")
print(f"Prefix: '{prefix}'")
print(f"Root word: '{root}'")
print(f"Suffix: '{suffix}'")
# Generate new words using other prefixes and suffixes (excluding the original)
new_prefixes = [p for p in prefixes if p != prefix]
new_suffixes = [s for s in suffixes if s != suffix]
generated_words = generate_words(root, new_prefixes, new_suffixes)
print("\nGenerated words (some examples):")
for w in generated_words[:20]: # limiting output
print(w)
if __name__ == "__main__":
main()

OUTPUT:

You might also like