NLP Lab File
NLP Lab File
UNIVERSITY
SE-316
NATURAL LANGUAGE PROCESSING
Submitted by
Prashant Tiwari
Roll Number :- 2K20/IT/103
Batch :- IT-B
CODE :
import nltk
nltk.download('stopwords')
nltk.download('punkt')
OUTPUT :
EXPERIMENT - 2
AIM : Import spacy and load the language model
CODE :
import spacy
nlp_eng = spacy.load('en_core_web_sm')
nlp_multi = spacy.load('xx_ent_wiki_sm')
OUTPUT :
EXPERIMENT - 3
AIM : WAP in python to tokenize a given text
CODE :
from nltk import word_tokenize
text = "Last week, the University of Cambridge shared its own research
that shows if everyone wears a mask outside home,dreaded ‘second wave’
of the pandemic can be avoided."
text = word_tokenize(text)
for t in text:
print(t)
OUTPUT :
EXPERIMENT - 4
AIM : WAP in python to get the sentences of a text document.
CODE :
file = open('04.txt')
Input_text = file.read()
ans = Input_text.split('.')
for an in ans:
print(an,'\n')
OUTPUT :
EXPERIMENT - 5
AIM : WAP in python to tokenize text with stopwords as
delimiters.
CODE :
text = "Walter was feeling anxious. He was diagnosed today. He probably
is the best person I know."
OUTPUT :
EXPERIMENT - 6
AIM : WAP in python to add custom stop words in spaCy.
CODE :
import spacy
nlp = spacy.load('en_core_web_sm')
doc = nlp("Jonas was a JUNK great guy NIL Adam was evil NIL Martha JUNK
was more of a fool")
for token in doc:
if not token.is_stop:
print(token.text, end=" ")
OUTPUT :
EXPERIMENT - 7
AIM : WAP to remove punctuations, perform stemming,
lemmatize given text and extract usernames from emails
CODE :
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
string = "Jonas!!! great \\guy <> Adam --evil [Martha] ;;fool() ."
ans = ""
for char in string:
if char not in punctuations:
ans+=char
print(ans)
text_list = word_tokenize(text)
usernames = []
for i in range(len(text_list)):
if text_list[i] == "@":
usernames.append(text_list[i-1])
print(usernames)
OUTPUT :
EXPERIMENT - 8
AIM : WAP to do spell correction, extract all nouns, pronouns
and verbs in a given text
CODE :
from textblob import TextBlob
text="He is a gret person. He beleives in bod"
textb = TextBlob(text)
correct_text = textb.correct()
print(correct_text)
import nltk
from nltk import word_tokenize, pos_tag
text="James works at Microsoft. She lives in manchester and likes to
play the flute"
tokens = word_tokenize(text)
parts_of_speech = nltk.pos_tag(tokens)
nouns = list(filter(lambda x: x[1] == "NN" or x[1] == "NNP",
parts_of_speech))
for noun in nouns:
print(noun[0])
text = "I may bake a cake for my birthday. The talk will introduce
reader about Use of baking"
words = word_tokenize(text)
verb_phrases = []
for i in range(len(words)):
if i > 0 and pos_tag(words)[i][1] == 'VB':
verb_phrase = words[i-1] + ' ' + words[i]
verb_phrases.append(verb_phrase)
for i in verb_phrases:
print (i)
OUTPUT :
EXPERIMENT - 9
AIM : WAP to find similarity between two words and classify a
text as positive/negative sentiment
CODE :
import spacy
nlp = spacy.load('en_core_web_md')
words = "amazing terrible excellent"
tokens = nlp(words)
OUTPUT :