0% found this document useful (0 votes)
6 views23 pages

Natural Language Processing Techniques

Uploaded by

Omkar Kamtekar
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)
6 views23 pages

Natural Language Processing Techniques

Uploaded by

Omkar Kamtekar
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

NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

INDEX

Sr. Page
Title Date Sign
No No

a. Convert the text into tokens


b. Find the word frequency
1 c. Demonstrate a bigram language model 2
d. Demonstrate a trigram language model
e. Generate regular expression for a given text

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

a. Find the synonym of a word using WordNet


b. Find the antonym of a word
c. Implement semantic role labeling to identify
3 11
named entities
d. Resolve the ambiguity
e. Translate the text using First-order logic

a. Implement RNN for sequence labeling


b. Implement POS tagging using LSTM
4 14
c. Implement Named Entity Recognizer
d. Word sense disambiguation by LSTM/GRU

a. Develop a Movie review system


5 18
b. Create a chatbot for HITS.

Karmaveer Bhaurao Patil College Vashi 1|Page


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

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. Convert the text into tokens

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',]

Karmaveer Bhaurao Patil College Vashi 2|Page


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

3. Tokenize User Input


Code:
import nltk
[Link]('punkt_tab')
a=input("Enter a sentence:- ")
result=nltk.word_tokenize(a)
print(result)

Output:
Enter a sentence:- This is a python program
['This', 'is', 'a', 'python', 'program']

[Link] User Input using function


Code:
from nltk import word_tokenize
txt=input("Enter a sentence:- ")
def tokenize(str1):
print(word_tokenize(str1))
tokenize(txt)

Output:
Enter a sentence:- this is a python code
['this', 'is', 'a', 'python', 'code']

2. Find the word frequency

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', '.']

Karmaveer Bhaurao Patil College Vashi 3|Page


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

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

3. Demonstrate a bigram language model

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)]

Karmaveer Bhaurao Patil College Vashi 4|Page


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

[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']

4. Demonstrate a trigram language model

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')]

5. Generate regular expression for a given text

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:

Karmaveer Bhaurao Patil College Vashi 5|Page


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

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

Karmaveer Bhaurao Patil College Vashi 6|Page


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

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]

Karmaveer Bhaurao Patil College Vashi 7|Page


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

for original,stemmed in zip(words,stemmed_words):


print(f"{original}->{stemmed}")

Output:
running->run
files->file
jumping->jump
quickly->quickli

3. Identify parts-of Speech using Penn Treebank tag set.


Code:
import nltk
[Link]('averaged_perceptron_tagger_eng')
sentence="the cats are chasing mice"
words=nltk.word_tokenize(sentence)
pos_tags=nltk.pos_tag(words)
print(pos_tags)

Output:

4. Implement HMM for POS tagging


Code:
import nltk
import random
from [Link] import hmm
[Link]('punkt')
[Link]('treebank')
[Link]('punkt_tab')

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]()

Karmaveer Bhaurao Patil College Vashi 8|Page


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

hmm_tagger = [Link](training_sentences)

accuracy = hmm_tagger.evaluate(testing_sentences)
print("HMM POS Tagger Accuracy:", accuracy)

new_sentence = "This is a test sentence"


new_words = nltk.word_tokenize(new_sentence)
predicted_tags = hmm_tagger.tag(new_words)

print("Predicted POS tags for the new sentence:")


print(predicted_tags)

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

Karmaveer Bhaurao Patil College Vashi 9|Page


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

from [Link] import LsaSummarizer


text=""" Text summarizer is the process of generating short,fluent and most importantly
accurate summary of respectevly longer text document""" # Corrected typo

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:

Karmaveer Bhaurao Patil College Vashi 10 | P a g e


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

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

1. Find the synonym of a word using WordNet


Code:
import nltk
[Link]('wordnet',quiet=True)
from [Link] import wordnet
word="Happy"
synonyms=[]
for syn in [Link](word):
for lemma in [Link]():
[Link]([Link]())
synonyms=list(set(synonyms))
print("Synonyms for",word+";")
print(synonyms)

Output:
1Synonyms for Happy;
['well-chosen', 'felicitous', 'happy', 'glad']

2. Find the antonym of a word


Code:
import nltk
[Link]('wordnet',quiet=True)
from [Link] import wordnet
word="Good"
antonyms=[]
for syn in [Link](word):
for lemma in [Link]():
for antonym in [Link]():
[Link]([Link]())
antonyms=list(set(antonyms))
print("Antonyms for",word+";")
print(antonyms)

Output:
Antonyms for Good;
['evil', 'bad', 'evilness', 'ill', 'badness']

Karmaveer Bhaurao Patil College Vashi 11 | P a g e


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

3. Implement semantic role labeling to identify named entities


Code:
import nltk
import spacy
[Link]("averaged_perceptron_tagger")
[Link]('words') # Corrected 'word' to 'words'
nlp=[Link]("en_core_web_sm")
text="apple lnc. was founded by steve jobs and steven worinak in capertion, california"
doc=nlp(text)
entities=[([Link],ent.label_) for ent in [Link]]
for entity in entities:
print(f"Entity:{entity[0]},Label:{entity[1]}")

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

4. Resolve the ambiguity


Code:
import nltk
import spacy
nlp=[Link]("en_core_web_sm")
text="the chicken is ready too eat "
doc=nlp(text)
for token in doc:
print(f"Token:{[Link]},POS:{token.pos_},Sense:{token.lemma_}")

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

Karmaveer Bhaurao Patil College Vashi 12 | P a g e


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

5. Translate the text using First-order logic


Code:
from pyDatalog import pyDatalog
pyDatalog.create_terms('X, human, mortal')
+human('John')
+human('Alice')
mortal(X) <= human(X)
print("Known mortals:")
print(mortal(X))
humans = ['John', 'Alice']
if all(mortal(x) == [(x,)] for x in humans):
print("All humans are mortal")
else:
print("Not all humans are mortal")

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?
[]

Who is a student of Socrates?


X
-----
plato

Karmaveer Bhaurao Patil College Vashi 13 | P a g e


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

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

1. Implement RNN for sequence labeling


import torch
import [Link] as nn
import [Link] as optim
import numpy as np

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']]

sequence_indices=[[vocab[word] for word in sequence]for sequence in sequences]

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)

Karmaveer Bhaurao Patil College Vashi 14 | P a g e


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

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-

2. Implement POS tagging using LSTM


Code:
import spacy
import tensorflow as tf
import numpy as np # Import numpy for reshaping
from tensorflow import keras
from sklearn.model_selection import train_test_split
from [Link] import LabelEncoder
nlp=[Link]("en_core_web_sm")

text="The quick brown fox jumps over the lazy dog"

doc=nlp(text)

tokens=[[Link] for token in doc]


pos_tags=[token.pos_ for token in doc]

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)

Karmaveer Bhaurao Patil College Vashi 15 | P a g e


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

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

3. Implement Named Entity Recognizer

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}')

Karmaveer Bhaurao Patil College Vashi 16 | P a g e


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

Output:
Entity:Apple,Label:PERSON
Entity:Inc.,Label:ORGANIZATION
Entity:American,Label:GPE
Entity:Cupertino,Label:GPE
Entity:California,Label:GPE

4. Word sense disambiguation by LSTM/GRU


Code:
import nltk
from [Link] import lesk
from [Link] import word_tokenize
[Link]('all')
def get_semantic(seq,key_word):
temp=word_tokenize(seq)
temp=lesk(temp,key_word)
return [Link]()
keyword='book'
seq1='I love reading books on coding.'
seq2='The table was already booked by someone else.'

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

Karmaveer Bhaurao Patil College Vashi 17 | P a g e


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

Practical No 05

Aim:
1. Develop a Movie review system
2. Create a chatbot for HITS.

1. Develop a Movie review system


Code:
import numpy as np
import pandas as pd
import re
import nltk
from [Link] import stopwords
from [Link] import word_tokenize
from [Link] import SnowballStemmer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB,MultinomialNB,BernoulliNB
from [Link] import accuracy_score
import joblib

[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")

print(f"Dataset output counts:\n{[Link].value_counts()}\n")

[Link]('positive', 1, inplace=True)
[Link]('negative', 0, inplace=True)
print(f"Dataset head after encoding :\n{[Link](10)}\n")

"""Clean dataset reviews as following:


1. Remove HTML tags
2. Remove special characters
3. Convert everything to lowercase
4. Remove stopwords
5. Stemming
"""
def clean(text):
cleaned = [Link](r'<.*?>')
return [Link](cleaned,'',text)

[Link] = [Link](clean)
print(f"Review sample after removing HTML tags : \n{[Link][0]}\n")

def is_special(text):
rem = ''

Karmaveer Bhaurao Patil College Vashi 18 | P a g e


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

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")

"""Create model to fit it to the data"""

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")

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=9)


print(f"Train shapes X : {X_train.shape}, y : {y_train.shape}\n")
print(f"Test shapes X : {X_test.shape}, y : {y_test.shape}\n")

gnb, mnb, bnb = GaussianNB(), MultinomialNB(alpha=1.0, fit_prior=True),


BernoulliNB(alpha=1.0, fit_prior=True)
[Link](X_train, y_train)
[Link](X_train, y_train)
[Link](X_train, y_train)

[Link](gnb, "Models/MRSA_gnb.pkl")
[Link](mnb, "Models/MRSA_mnb.pkl")
[Link](bnb, "Models/MRSA_bnb.pkl")

Karmaveer Bhaurao Patil College Vashi 19 | P a g e


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

ypg = [Link](X_test)
ypm = [Link](X_test)
ypb = [Link](X_test)

"""Evaluate model performance"""


print(f"Gaussian accuracy = {round(accuracy_score(y_test, ypg), 2) * 100} %")
print(f"Multinomial accuracy = {round(accuracy_score(y_test, ypm), 2) * 100} %")
print(f"Bernoulli accuracy = {round(accuracy_score(y_test, ypb), 2) * 100} %")

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

2. Create a chatbot for HITS.


Code:
import json
import requests
import time
import os
from [Link] import urlencode

API_KEY = "AIzaSyCvgvOrT-b0qGot9JwUytQBf47qIwI0GAI"
API_URL = "[Link]
05-20:generateContent"

"""Define the chatbot's role, memory, and utility functions."""

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 = []

def call_gemini_api(prompt, system_instruction, history, tools=None):


"""
Calls the Gemini API with exponential backoff for text generation.

Args:
prompt (str): The user's latest query.
system_instruction (str): The model's persona definition.
history (list): List of previous messages for context.

Karmaveer Bhaurao Patil College Vashi 20 | P a g e


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

tools (list, optional): Tools for grounding (e.g., Google Search).

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

for attempt in range(max_retries):


try:

url = f"{API_URL}?key={API_KEY}"

response = [Link](
url,
headers={'Content-Type': 'application/json'},
data=[Link](payload)
)
response.raise_for_status()
result = [Link]()

candidate = [Link]('candidates', [{}])[0]


if candidate and [Link]('content') and candidate['content'].get('parts'):
return candidate['content']['parts'][0]['text']

return "Error: Could not extract response text from API."

except [Link] as e:

if response.status_code in [429, 500, 503] and attempt < max_retries - 1:


[Link](delay)
delay *= 2
continue
return f"HTTP Error: {e}"

Karmaveer Bhaurao Patil College Vashi 21 | P a g e


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

except Exception as e:
return f"An unexpected error occurred: {e}"

return "Error: Max retries reached. The API is unresponsive."

def update_history(role, text):


"""
Adds a message to the global chat history.
"""
chat_history.append({'role': role, 'text': text})
if len(chat_history) > 10:
chat_history.pop(0)

"""Main interactive loop for the chatbot."""

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 user_input.lower() in ['exit', 'quit']:


print("\nHITS Bot: Thank you for chatting! Have a great day.")
break

if not user_input.strip():
continue
update_history("user", user_input)

tools_config = [{"google_search": {}}]

response_text = call_gemini_api(
prompt=user_input,
system_instruction=SYSTEM_PROMPT,
history=chat_history,
tools=tools_config
)

update_history("model", response_text)

print(f"HITS Bot: {response_text}\n")

except EOFError:
print("\nExiting chat.")
break

Karmaveer Bhaurao Patil College Vashi 22 | P a g e


NATURAL LANGUAGE PROCESSING

Roll NO: 25166001

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:

Karmaveer Bhaurao Patil College Vashi 23 | P a g e

You might also like