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

Create Sample list of at least 10 words POS tagging and find the POS for any given word.

โ† All NLP Programs

Solution :

DESCRIPTION:

Part-Of-Speech (POS) tagging is the process of assigning a grammatical category (noun, verb, adjective, adverb, preposition, etc.) to every word of a text. It is one of the first steps of many NLP applications such as parsing, information extraction and machine translation.

The program creates a sample list of 12 words with their POS tags (Penn Treebank tag set) and then finds the POS of any word entered by the user. A word that is not in the list is reported as unknown. Commonly used tags: NN noun, NNS plural noun, VB* verbs, JJ adjective, RB adverb, PRP pronoun, DT determiner, IN preposition, CC conjunction, CD number.

PROGRAM: ( pos_tagging.py )

 
# Sample list of words with their Part-Of-Speech (POS) tags (Penn Treebank tag set)
pos_list = {
    "the":       "DT   (Determiner)",
    "boy":       "NN   (Noun, singular)",
    "books":     "NNS  (Noun, plural)",
    "he":        "PRP  (Personal pronoun)",
    "runs":      "VBZ  (Verb, 3rd person singular present)",
    "ran":       "VBD  (Verb, past tense)",
    "running":   "VBG  (Verb, gerund / present participle)",
    "beautiful": "JJ   (Adjective)",
    "quickly":   "RB   (Adverb)",
    "in":        "IN   (Preposition)",
    "and":       "CC   (Coordinating conjunction)",
    "three":     "CD   (Cardinal number)",
}

print("Sample list of words and their POS tags:\n")
for word, tag in pos_list.items():
    print("{:<10} -> {}".format(word, tag))

# find the POS of any given word
word = input("\nEnter a word: ").lower()

if word in pos_list:
    print("POS of '" + word + "' is", pos_list[word])
else:
    print("The word '" + word + "' is not in the sample list")

# tag a sentence made from the words of the list
sentence = "the boy runs quickly"
print("\nSentence:", sentence)
for w in sentence.split():
    print(w, "->", pos_list[w].split()[0])

OUTPUT:

 
$ python pos_tagging.py
Sample list of words and their POS tags:

the        -> DT   (Determiner)
boy        -> NN   (Noun, singular)
books      -> NNS  (Noun, plural)
he         -> PRP  (Personal pronoun)
runs       -> VBZ  (Verb, 3rd person singular present)
ran        -> VBD  (Verb, past tense)
running    -> VBG  (Verb, gerund / present participle)
beautiful  -> JJ   (Adjective)
quickly    -> RB   (Adverb)
in         -> IN   (Preposition)
and        -> CC   (Coordinating conjunction)
three      -> CD   (Cardinal number)

Enter a word: quickly
POS of 'quickly' is RB   (Adverb)

Sentence: the boy runs quickly
the -> DT
boy -> NN
runs -> VBZ
quickly -> RB

$ python pos_tagging.py
Enter a word: table
The word 'table' is not in the sample list


๐Ÿ’ป Run This Python Program

Create Sample list of at least 10 words POS tagging and find the POS for any given word.
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