Basic NLP Progs-Lab1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

BASIC NLP PROGS-1

# PROGRAM1:
#a) Create a basic NLP program to find words,phrases,names
#and concepts using "spacy.blank" to create the English nlp container.#b) Process the text and instantiate a Doc
object in the variable doc. #c) Select the first token of the Doc and print its text.
#TEXT1: Be Indian and Buy Indian

import spacy
# a)
nlp=spacy.load("en_core_web_sm")import
en_core_web_sm
nlp=en_core_web_sm.load()
from spacy.lang.en import Englishnlp=English()
nlp=spacy.blank("en")
# b)
text1="Be Indian and Buy Indian"
new=text1.split(".")
print(new)
text="Be Indian and Buy Indian"doc=nlp(text1)
print(doc.text)
# c.)
first_token=doc[1]
print(first_token)

# PROGRAM2:
#Write a Python spacy program to split the
#text sentence/paragraph into a list of wordsusing TOKENIZATION
#Code:
Text="Dr.strange loves pav bhaji of mumbai .Hyderabadis likes Biryani"print("\nOriginal Sentence
:") print(Text)
print("===================")
nlp=spacy.load("en_core_web_sm")doc=nlp(Text)
for sent in doc.sents:
print(sent.text,"\n")for sent in
doc.sents:
print(sent.text)
for words in sent:
print(words)

# PROGRAM3:
##a) Create a basic NLP program to find words,phrases,names and concepts#using "spacy.blank" to create the
English nlp container.
##b) Process the text and instantiate a Doc object in the variable doc.##c)Select the first token of
the Doc and print its text2.
##d) print table of token_text , pos and dependency sparsing
##e) print the entitytext , entitylabel & explanation of entitylabel
# a .) and b.)
nlp=spacy.load("en_core_web_sm")import
en_core_web_sm
nlp=en_core_web_sm.load()
from spacy.lang.en import Englishnlp=English()
nlp=spacy.blank("en")
text2="It’s official:TCS becomes first Indian company to breach $100 billion market"doc=nlp(text2)
print(doc.text)
# c.)
first_token=doc[1]
print(first_token)
# d.)
nlp=spacy.load("en_core_web_sm")
text2="It’s official:TCS becomes first Indian company to breach $100 billion market"doc=nlp(text2)

for token in doc:


token_text=token.text
token_pos=token.pos_
token_dep=token.dep_
print(f"{token_text:<12}{token_pos:<10}{token_dep:<10}")

# e.)
nlp=spacy.load("en_core_web_sm")
text2="It’s official:TCS becomes first Indian company to breach $100 billion market"doc=nlp(text2)
for entity in doc.ents:
print(entity.text+'-'+entity.label_+'-'+str(spacy.explain(entity.label_)))

#Program4:
#Get all the proper nouns from a given TEXT3 in a list and also count how many of them.#Proper Noun means a noun that names a
particular person, place, or thing.
#Hint: Use the spacy pos

nlp=spacy.load("en_core_web_sm")
text3= '''Ravi and Raju are the best friends from school days.'''doc=nlp(text3)
print(doc.text)list=[]
for word in doc:
if word.pos_ == "PROPN":
list.append(word)
print(list)
print("Count: ",len(list))

#Program5:
#Get all companies names from a given TEXT4 and also the count of them.
text4 = '''The Top 5 companies in USA are Tesla, Walmart, Amazon,Microsoft, Google and the top 5
companies in
India are Infosys, Reliance, HDFC Bank, HindustanUnilever and Bharti
Airtel'''
print(text4)
nlp=spacy.load("en_core_web_sm")doc=nlp(text4)
list=[]
for ent in doc.ents:
if ent.label_ == "ORG":
list.append(ent)
print(list)
print("Count: ",len(list))

You might also like