Natural Language Processing Techniques
Natural Language Processing Techniques
INDEX
Sr. Page
Title Date Sign
No No
a. Perform Lemmatization
b. Perform Stemming
c. Identify parts-of Speech using Penn Treebank tag
2 7
set.
d. Implement HMM for POS tagging
e. Build a Chunker
Practical No 01
Aim:
1. Convert the text into tokens
2. Find the word frequency
3. Demonstrate a bigram language model
4. Demonstrate a trigram language model
5. Generate regular expression for a given text
6. Text Normalization
1. Tokenize sentence
Code:
import nltk
[Link]('punkt_tab')
a='I am going to kathmandu'
result=nltk.word_tokenize(a)
print(result)
Output:
['I', 'am', 'going', 'to', 'kathmandu']
2. Tokenize Paragraph
Code:
import nltk
[Link]('punkt_tab')
a="""Once, there was a hare who was best friends with a tortoise. The hare was very
proud of how fast he could run, so one day, he challenged the tortoise to a race. The
tortoise agreed, even though everyone thought he was way too slow to win. The race
began, and the hare raced so fast that he was far ahead of the tortoise."""
result=nltk.word_tokenize(a)
print(result)
Output:
['Once', ',', 'there', 'was', 'a', 'hare', 'who', 'was', 'best', 'friends', 'with', 'a', 'tortoise', '.', 'The',
'hare', 'was', 'very', 'proud', 'of', 'how', 'fast', 'he', 'could', 'run', ',', 'so', 'one', 'day', ',', 'he',
'challenged', 'the', 'tortoise', 'to', 'a', 'race', '.', 'The', 'tortoise', 'agreed', ',', 'even',]
Output:
Enter a sentence:- This is a python program
['This', 'is', 'a', 'python', 'program']
Output:
Enter a sentence:- this is a python code
['this', 'is', 'a', 'python', 'code']
Code:
import nltk
[Link]('punkt_tab')
t="""Once, there was a hare who was best friends with a tortoise. The hare was very
proud of how fast he could run, so one day, he challenged the tortoise to a race. The
tortoise agreed, even though everyone thought he was way too slow to win. The race
began, and the hare raced so fast that he was far ahead of the tortoise."""
t1=nltk.word_tokenize(t)
print(t1)
Output:
['Once', ',', 'there', 'was', 'a', 'hare', 'who', 'was', 'best', 'friends', 'with', 'a', 'tortoise', '.', 'The',
'hare', 'was', 'very', 'proud', 'of', 'how', 'fast', 'he', 'could', 'run', ',', 'so', 'one', 'day', ',', 'he',
'challenged', 'the', 'tortoise', 'to', 'a', 'race', '.', 'The', 'tortoise', 'agreed', ',', 'even', 'though',
'everyone', 'thought', 'he', 'was', 'way', 'too', 'slow', 'to', 'win', '.', 'The', 'race', 'began', ',',
'and', 'the', 'hare', 'raced', 'so', 'fast', 'that', 'he', 'was', 'far', 'ahead', 'of', 'the', 'tortoise', '.']
Code:
count=[]
for i in t1:
if i not in count:
[Link](i)
for j in range(0,len(count)):
print(count[j],[Link](count[j]))
Output:
Once 1
,5
there 1
was 5
a3
hare 3
who 1
best 1
friends 1
with 1
tortoise 4
.4
The 3
very 1
proud 1
of 2
how 1
fast 2
he 4
could 1
run 1
so 2
one 1
day 1
Code:
from nltk import word_tokenize
import nltk
[Link]('punkt_tab') #for jupyter notebook use "[Link]('punkt')"
sentence="She will be showing a demo of the company's new alarm system. a demo
version of the software I saw a demo on how to use the computer program"
gram=2
token=word_tokenize(sentence)
bigram=[]
for i in range(len(token)-(gram-1)):
temp=[token[j] for j in range(i,i+gram)]
[Link](" ".join(temp))
print(bigram)
Output:
['She will', 'will be', 'be showing', 'showing a', 'a demo', 'demo of', 'of the', 'the company',
"company 's", "'s new", 'new alarm', 'alarm system', 'system .', '. a', 'a demo', 'demo version',
'version of', 'of the', 'the software', 'software I', 'I saw', 'saw a', 'a demo', 'demo on', 'on how',
'how to', 'to use', 'use the', 'the computer', 'computer program']
Code:
import nltk
[Link]('punkt_tab')
from nltk import ngrams
from [Link] import word_tokenize
sentence="She will be showing a demo of the company's new alarm system. a demo
version of the software I saw a demo on how to use the computer program"
tokens=word_tokenize(sentence)
bigrams=list(ngrams(tokens,2))
trigrams=list(ngrams(tokens,3))
print("Bigrams: ",bigrams)
print("Trigrams: ",trigrams)
Output:
Bigrams: [('She', 'will'), ('will', 'be'), ('be', 'showing'), ('showing', 'a'), ('a', 'demo'), ('demo',
'of'), ('of', 'the'), ('the', 'company'), ('company', "'s"), ("'s", 'new'), ('new', 'alarm'),)]
Trigrams: [('She', 'will', 'be'), ('will', 'be', 'showing'), ('be', 'showing', 'a'), ('showing', 'a',
'demo'), ('a', 'demo', 'of'), ('demo', 'of', 'the'), ('of', 'the', 'company'), ('the', 'company', "'s"),
('company', "'s", 'new'), ("'s", 'new', 'alarm')]
Code:
import re
text="Please contact support@[Link] or sales+@[Link]."
email_pattern=r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b'
regex=[Link](email_pattern)
matches=[Link](text)
for match in matches:
print(match)
Output:
support@[Link]
sales+@[Link]
6. Text Normalization
Code:
import re
import unicodedata
def abc(text):
normalized_text=[Link]()
normalized_text=[Link](r'[^\w\s]','',normalized_text)
normalized_text=[Link]('NFKD',normalized_text).encode('ASCII','ignore').
decode('utf-8')
normalized_text="".join(normalized_text.split())
return normalized_text
input_text=input("Enter text to normalize:- ")
normalized_result=abc(input_text)
print("Normalize text: ",normalized_result)
Output:
Enter text to normalize:- This is a "python" code.
Normalize text: thisisapythoncode
Practical No 02
Aim:
1. Perform Lemmatization
2. Perform Stemming
3. Identify parts-of Speech using Penn Treebank tag set.
4. Implement HMM for POS tagging
5. Build a Chunker
6. Summerization
1. Perform Lemmatization
Code:
import nltk
[Link]('punkt_tab')
[Link]('omw-1.4')
from [Link] import WordNetLemmatizer
[Link]('wordnet')
Output:
Code:
lemmatizer = WordNetLemmatizer()
sen="The boys and girls were presented in classes."
words=nltk.word_tokenize(sen)
lemmatized_word=[[Link](word)for word in words]
lemmatized_sen=' '.join(lemmatized_word)
print(lemmatized_sen)
Output:
2. Perform Stemming
Code:
import nltk
from [Link] import PorterStemmer
stemmer = PorterStemmer()
words=["running","files","jumping","quickly"]
stemmed_words=[[Link](word)for word in words]
Output:
running->run
files->file
jumping->jump
quickly->quickli
Output:
corpus = [Link]
sentences = [Link]()
tagged_sentences = corpus.tagged_sents()
[Link](123)
split_ratio = 0.8
split_index = int(len(tagged_sentences) * split_ratio)
training_sentences = tagged_sentences[:split_index]
testing_sentences = tagged_sentences[split_index:]
trainer = [Link]()
hmm_tagger = [Link](training_sentences)
accuracy = hmm_tagger.evaluate(testing_sentences)
print("HMM POS Tagger Accuracy:", accuracy)
Output:
5. Build a Chunker
Code:
import nltk
[Link]('punkt_tab')
[Link]('averaged_perceptron_tagger_eng')
L="The quick brown fox jumps over the lazy dog"
words=nltk.word_tokenize(L)
pos_tags=nltk.pos_tag(words)
grammer=r"""NP:{<DT|JJ|NN.*>+}"""
chunk_parcer=[Link](grammer)
chunks_sentence=chunk_parcer.parse(pos_tags)
for subtree in chunks_sentence.subtrees():
if [Link]()=='NP':
print(' '.join(word for word,tag in [Link]()))
Output:
6. Summerization
Code:
%pip install sumy
import sumy
from [Link] import PlaintextParser
from [Link] import Tokenizer
parcer=PlaintextParser.from_string(text,Tokenizer("english"))
summarizer=LsaSummarizer()
sentences_count=int(input("enter the value"))
summary=summarizer([Link],sentences_count)
for sentence in summary:
print(sentence)
Output:
Practical No 03
Aim:
1. Find the synonym of a word using WordNet
2. Find the antonym of a word
3. Implement semantic role labeling to identify named entities
4. Resolve the ambiguity
5. Translate the text using First-order logic
Output:
1Synonyms for Happy;
['well-chosen', 'felicitous', 'happy', 'glad']
Output:
Antonyms for Good;
['evil', 'bad', 'evilness', 'ill', 'badness']
Output:
[nltk_data] Downloading package averaged_perceptron_tagger to
[nltk_data] /root/nltk_data...
[nltk_data] Package averaged_perceptron_tagger is already up-to-
[nltk_data] date!
[nltk_data] Downloading package words to /root/nltk_data...
[nltk_data] Unzipping corpora/[Link].
Entity:steve jobs,Label:PERSON
Entity:california,Label:GPE
Output:
Token:the,POS:DET,Sense:the
Token:chicken,POS:NOUN,Sense:chicken
Token:is,POS:AUX,Sense:be
Token:ready,POS:ADJ,Sense:ready
Token:too,POS:ADV,Sense:too
Token:eat,POS:VERB,Sense:eat
Output:
Not all humans are mortal
Code:
from pyDatalog import pyDatalog
[Link]()
pyDatalog.create_terms('X, Y, teaches, students_of, younger_than')
+teaches('plato', 'aristotle')
+teaches('socrates', 'plato')
students_of(Y, X) <= teaches(X, Y)
younger_than(X, Y) <= students_of(Y, X)
print("Is Aristotle younger than Plato?")
print(younger_than('aristotle', 'plato'))
print("\nWho is a student of Socrates?")
print(students_of(X, 'socrates'))
Output:
Is Aristotle younger than Plato?
[]
Practical No 04
Aim:
1. Implement RNN for sequence labeling
2. Implement POS tagging using LSTM
3. Implement Named Entity Recognizer
4. Word sense disambiguation by LSTM/GRU
vocab={'I':0,'love':1,'natural':2,'language':3,'processing':4,'like':5,'deep':6,'learning':7}
sequences=[['I','love','natural','language','processing']]
labels=[['PRON','VERB','ADJ','NOUN','NOUN']]
label_vocab={'PRON':0,'VERB':1,'ADJ':2,'NOUN':3}
label_indices=[[label_vocab[label] for label in label_sequence]for label_sequence in labels]
class RNN([Link]):
def __init__(self,input_size,hidden_size,output_size):
super(RNN,self).__init__()
self.hidden_size=hidden_size
[Link]=[Link](input_size,hidden_size)
[Link]=[Link](hidden_size,hidden_size)
[Link]=[Link](hidden_size,output_size)
def forward(self,x):
embedded=[Link](x)
output,_=[Link](embedded)
output=[Link](output)
return output
input_size=len(vocab)
hidden_size=64
output_size=len(label_vocab)
model=RNN(input_size,hidden_size,output_size)
criterion=[Link]()
optimizer=[Link]([Link](),lr=0.001)
num_epochs=100
for epoch in range(num_epochs):
optimizer.zero_grad()
inputs=[Link](sequence_indices).long()
labels=[Link](label_indices).view(-1).long()
outputs=model(inputs)
outputs=[Link](-1,output_size)
loss=criterion(outputs,labels)
[Link]()
[Link]()
print(f'Epoch[{epoch+1}/{num_epochs}],loss:{[Link]()}')
with torch.no_grad():
test_sequence=[['I','like','deep','learning']] test_sequence_indices=[[vocab[word] for word in
sequence]for sequence in test_sequence]
inputs=[Link](test_sequence_indices).long()
outputs=model(inputs)
predicted_labels=[Link](outputs,dim=2)
predicted_labels=[[list(label_vocab.keys())[list(label_vocab.values()).index(label)]for label in
sequence]for sequence in predicted_labels]
print(f'Predicted Labels:{predicted_labels}')
Output-
doc=nlp(text)
label_encoder=LabelEncoder()
pos_labels=label_encoder.fit_transform(pos_tags)
X_train,X_test,Y_train,Y_test=train_test_split(tokens,pos_labels,test_size=0.2,random_sta
te=42)
tokenizer=[Link]()
[Link](X_train)
X_train=tokenizer(X_train)
X_test=tokenizer(X_test)
Y_train = Y_train.reshape(-1, 1)
Y_test = Y_test.reshape(-1, 1)
model=[Link]([[Link](input_dim=len(tokenizer.get_vocabular
y()),output_dim=128,mask_zero=True),
[Link](128,return_sequences=True),
[Link](len(label_encoder.classes_),activation='softmax')])
[Link](optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accurac
y'])
[Link](X_train,Y_train,epochs=5,validation_split=0.2)
loss,accuracy=[Link](X_test,Y_test)
print(f'loss:{loss},accuracy:{accuracy}')
Output:
Entity:Apple,Label:PERSON
Entity:Inc.,Label:ORGANIZATION
Entity:American,Label:GPE
Entity:Cupertino,Label:GPE
Entity:California,Label:GPE
Code:
import nltk
[Link]('punkt')
[Link]('averaged_perceptron_tagger_eng')
[Link]('maxent_ne_chunker')
[Link]('words')
[Link]('maxent_ne_chunker_tab')
text="Apple Inc. is an American multinational technology company headquartered in
Cupertino,California."
tokens=nltk.word_tokenize(text)
pos_tags=nltk.pos_tag(tokens)
named_entities=[Link].ne_chunk(pos_tags)
entities=[]
for subtree in named_entities:
if isinstance(subtree,[Link]):
entity=" ".join([word for word, tag in [Link]()])
label=[Link]()
[Link]((entity,label))
for entity,label in entities:
print(f'Entity:{entity},Label:{label}')
Output:
Entity:Apple,Label:PERSON
Entity:Inc.,Label:ORGANIZATION
Entity:American,Label:GPE
Entity:Cupertino,Label:GPE
Entity:California,Label:GPE
keyword1='jam'
seq3='My mother prepares very yummy jam.'
seq4='signal jammers are the reason for no signal.'
print(get_semantic(seq1,keyword))
print(get_semantic(seq2,keyword))
print(get_semantic(seq3,keyword1))
print(get_semantic(seq4,keyword1))
Output:
a number of sheets (ticket or stamps etc.) bound together on one edge
arrange for and reserve (something for someone else) in advance
press tightly together or cram
deliberate radiation or reflection of electromagnetic energy for the
purpose of disrupting enemy use of electronic devices or systems
Practical No 05
Aim:
1. Develop a Movie review system
2. Create a chatbot for HITS.
[Link]('stopwords')
[Link]('punkt')
"""Prepare the dataset before training"""
dataset = pd.read_csv('Dataset/[Link]')
print(f"Dataset shape : {[Link]}\n")
print(f"Dataset head : \n{[Link]()}\n")
[Link]('positive', 1, inplace=True)
[Link]('negative', 0, inplace=True)
print(f"Dataset head after encoding :\n{[Link](10)}\n")
[Link] = [Link](clean)
print(f"Review sample after removing HTML tags : \n{[Link][0]}\n")
def is_special(text):
rem = ''
for i in text:
if [Link]():
rem = rem + i
else:
rem = rem + ' '
return rem
[Link] = [Link](is_special)
print(f"Review sample after removing special characters : \n{[Link][0]}\n")
def to_lower(text):
return [Link]()
[Link] = [Link](to_lower)
print(f"Review sample after converting everything to lowercase : \n{[Link][0]}\n")
def rem_stopwords(text):
stop_words = set([Link]('english'))
words = word_tokenize(text)
return [w for w in words if w not in stop_words]
[Link] = [Link](rem_stopwords)
print(f"Review sample after removing stopwords : \n{[Link][0]}\n")
def stem_text(text):
ss = SnowballStemmer('english')
return " ".join([[Link](w) for w in text])
[Link] = [Link](stem_text)
print(f"Review sample after stemming the words : \n{[Link][0]}\n")
X = [Link]([Link][:,0].values)
y = [Link]([Link])
cv = CountVectorizer(max_features = 2000)
X = cv.fit_transform([Link]).toarray()
print(f"--- Bag of words ---\n")
print(f"f'BOW X shape : {[Link]}\n")
print(f"f'BOW Y shape : {[Link]}\n")
[Link](gnb, "Models/MRSA_gnb.pkl")
[Link](mnb, "Models/MRSA_mnb.pkl")
[Link](bnb, "Models/MRSA_bnb.pkl")
ypg = [Link](X_test)
ypm = [Link](X_test)
ypb = [Link](X_test)
Output:
Dataset shape : (12, 2)
Dataset head :
review sentiment
0 A truly wonderful and touching film. I absolut... positive
1 This movie was a total waste of time. The plot... negative
2 The cinematography was stunning, but the dialo... positive
3 I walked out halfway through. The most boring ... negative
4 Highly recommended! A masterpiece of modern ci... positive
API_KEY = "AIzaSyCvgvOrT-b0qGot9JwUytQBf47qIwI0GAI"
API_URL = "[Link]
05-20:generateContent"
SYSTEM_PROMPT = (
"You are 'HITS Bot', an official, friendly, and highly informative chatbot "
"for the Hindustan Institute of Technology and Science (HITS) in Chennai, India. "
"Your primary goal is to provide accurate and helpful information about the "
"university, including admissions, courses, campus life, faculty, and recent news. "
"Keep your answers concise, professional, and encouraging. Always maintain "
"the persona of a representative of HITS."
)
chat_history = []
Args:
prompt (str): The user's latest query.
system_instruction (str): The model's persona definition.
history (list): List of previous messages for context.
Returns:
str: The generated response text, or an error message.
"""
full_contents = []
for message in history:
full_contents.append({
"role": message['role'],
"parts": [{"text": message['text']}]
})
full_contents.append({
"role": "user",
"parts": [{"text": prompt}]
})
payload = {
"contents": full_contents,
"systemInstruction": {"parts": [{"text": system_instruction}]}
}
if tools:
payload['tools'] = tools
max_retries = 3
delay = 1
url = f"{API_URL}?key={API_KEY}"
response = [Link](
url,
headers={'Content-Type': 'application/json'},
data=[Link](payload)
)
response.raise_for_status()
result = [Link]()
except [Link] as e:
except Exception as e:
return f"An unexpected error occurred: {e}"
def run_chatbot():
"""
Initializes and runs the interactive HITS Chatbot.
"""
print("----------------------------------------------------------------------")
print("Welcome to HITS Bot! I am here to answer your questions about the")
print("Hindustan Institute of Technology and Science.")
print("Type 'exit' or 'quit' to end the session.")
print("----------------------------------------------------------------------")
while True:
try:
user_input = input("You: ")
if not user_input.strip():
continue
update_history("user", user_input)
response_text = call_gemini_api(
prompt=user_input,
system_instruction=SYSTEM_PROMPT,
history=chat_history,
tools=tools_config
)
update_history("model", response_text)
except EOFError:
print("\nExiting chat.")
break
except KeyboardInterrupt:
print("\nExiting chat.")
break
except Exception as e:
print(f"\nAn unexpected runtime error occurred: {e}")
break
if __name__ == "__main__":
run_chatbot()
Output: