NLP FinAL
NLP FinAL
Practical Journal
Seat Number [ ]
CERTIFICATE
This is to certify that Miss. Aishwarya Nandkishore Sonawane of M.Sc. (CS) with
Seat No. 23
has complete 8 Practical of Paper-( Course Name- Natural Language
Processing) under mysupervision in this College during the Fourth Semester of academic
year 2022-2023.
Date:
INDEX
Program:
from playsound import playsound
language="en"
myobj=gTTS(text=mytext,lang=language,slow=False)
myobj.save("myfile.mp3")
playsound("myfile.mp3")
Output:
welcomeNLP.mp3 audio file is getting created and it plays the file with playsound() method,
while running the program
Practical 1(B)
Program:
import speech_recognition as sr
r=sr.Recognizer()
audio_data=r.record(source)
text=r.recognize_google(audio_data)
print(text)
Output:
Practical 2(A)
Program:
import nltk
print (filelist.fileids())
print (filelist.root)
'''display other information about each text, by looping over all the values of fileid
corresponding to the filelist file identifiers listed earlier and then computing statistics
print ('AvgWordLen\tAvgSentenceLen\tno.ofTimesEachWordAppearsOnAvg\tFileName')
num_chars = len(filelist.raw(fileid))
num_words = len(filelist.words(fileid))
num_sents = len(filelist.sents(fileid))
Program:
import nltk
nltk.download('punkt')
nltk.download('words')
sents = tokenize.sent_tokenize(para)
print("\nsentence tokenization\n===================\n",sents)
# word tokenization
print("\nword tokenization\n===================\n")
words = tokenize.word_tokenize(sents[index])
print(words)
Output:
Practical 2(C)
Aim: Map Words to Properties Using Python Dictionaries
Program:
thisdict= {
"brand":"Mercedes",
"model": "G-Class",
"year":1964
print(thisdict)
print(thisdict["brand"])
print(len(thisdict))
print(type(thisdict))
Output:
Practical 3(A)
Aim: Study Default Tagger
Program:
import nltk
exptagger=DefaultTagger('NN')
testsentences=treebank.tagged_sents()[1000:]
print(exptagger.evaluate(testsentences))
import nltk
exptagger=DefaultTagger
exptagger=DefaultTagger('NN')
print(exptagger.tag_sents([['Hey',','],['How','are','you','?']]))
Output:
Practical 3(B)
Aim: Study Unigram Tagger
Program:
# Loading Libraries
# Using data
train_sents = treebank.tagged_sents()[:10]
# Initializing
tagger = UnigramTagger(train_sents)
print(treebank.sents()[0])
print('\n',tagger.tag(treebank.sents()[0]))
tagger.tag(treebank.sents()[0])
print('\n',tagger.tag(treebank.sents()[0]))
Output:
Practical 4(A)
Aim: Study of Wordnet Dictionary with methods as synsets, definitions, examples, antonyms
Program:
'''WordNet provides synsets which is the collection of synonym words also called
“lemmas”'''
import nltk
print(wordnet.synsets("computer"))
print(wordnet.synset("computer.n.01").definition())
#examples
print("Examples:", wordnet.synset("computer.n.01").examples())
#get Antonyms
print(wordnet.lemma('buy.v.01.buy').antonyms())
Output:
Practical 4(B)
Aim: Write a program using python to find synonym and antonym of word "active" using Wordnet.
Program:
from nltk.corpus import wordnet
print( wordnet.synsets("active"))
print(wordnet.lemma('active.a.01.active').antonyms())
Output:
Practical 4(C)
Aim: Compare two nouns
Program:
import nltk
syn1 = wordnet.synsets('football')
syn2 = wordnet.synsets('soccer')
# A word may have multiple synsets, so need to compare each synset of word1 with synset of word2
for s1 in syn1:
for s2 in syn2:
print()
Output:
Practical 5(A)
Program:
text = """ This tool is an a beta stage. Alexa developers can use Get Metrics API to seamlessly analyse
metric. It also supports custom skill model, prebuilt Flash Briefing model, and the Smart Home Skill API.
You can use this tool for creation of monitors, alarms, and dashboards that spotlight changes. The
release of these three tools will enable developers to create visual rich skills for Alexa devices with
screens. Amazon describes these tools as the collection of tech and tools for creating visually rich and
interactive voice experiences. """
data = text.split('.')
for i in data:
print (i)
Output:
Practical 5(B)
Program:
import nltk
tokens = tk.tokenize(str)
print(tokens)
Output:
Practical 5(C)
Program:
import keras
tokens = text_to_word_sequence(str)
print(tokens)
Output:
Practical 6(A)
Program:
import spacy
nlp = spacy.load("en_core_web_sm")
"Google in 2007, few people outside of the company took him "
"seriously. “I can tell you very senior CEOs of major American "
"car companies would shake my hand and turn away because I wasn’t "
"worth talking to,” said Thrun, in an interview with Recode earlier "
"this week.")
doc = nlp(text)
# Analyse syntax
Output:
Practical 6(B)
Aim: Named Entity recognition with diagram using NLTK corpus – treebank.
Program:
import nltk
nltk.download('treebank')
treebank_chunk.tagged_sents()[0]
treebank_chunk.chunked_sents()[0]
treebank_chunk.chunked_sents()[0].draw()
Output:
Practical 7(A)
Aim: Define grammar using nltk. Analyze a sentence using the same
Program:
import nltk
grammar1 = nltk.CFG.fromstring("""
S -> VP
VP -> VP NP
NP -> Det NP
NP -> 'flight'
VP -> 'Book'
""")
all_tokens = tokenize.word_tokenize(sentence)
print(all_tokens)
parser = nltk.ChartParser(grammar1)
print(tree)
tree.draw()
Output:
Practical 7(B)
Aim: Implementation of Deductive Chart Parsing using context free grammar and a given
sentence.
Program:
import nltk
grammar1 = nltk.CFG.fromstring("""
S -> NP VP
PP -> P NP
VP -> V NP | VP PP
V -> 'saw'
P -> 'in'
""")
all_tokens = tokenize.word_tokenize(sentence)
print(all_tokens)
parser = nltk.ChartParser(grammar1)
print(tree)
tree.draw()
Output:
Practical 8
Program:
print('PorterStemmer')
import nltk
word_stemmer = PorterStemmer()
print(word_stemmer.stem('writing'))
print('LancasterStemmer')
import nltk
Lanc_stemmer = LancasterStemmer()
print(Lanc_stemmer.stem('writing'))
print('RegexpStemmer')
import nltk
print(Reg_stemmer.stem('writing'))
print('SnowballStemmer')
import nltk
print(english_stemmer.stem ('writing'))
print('WordNetLemmatizer')
lemmatizer = WordNetLemmatizer()
print("word :\tlemma")
Output: