"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

February 12, 2019

Day #210 - NLP Coding Snippets

Samples on Entity Extraction, Keywords extraction, Sentiment Analysis for evaluating sentences.

#pip install spacy
#python -m spacy download en
#pip install multi-rake
from multi_rake import Rake
import spacy
nlp = spacy.load('en')
from textblob import TextBlob
def computekeywords(sentence):
print(sentence)
doc = nlp(sentence)
rake = Rake()
print('using spacy')
for ent in doc.ents:
print(ent.text, ent.label_)
keywords = rake.apply(sentence)
print('Keywords using Rake')
print(keywords)
print('Sentiment of the sentence')
analysis = TextBlob(sentence)
if(analysis.sentiment[0])>0:
intent = 'Positive'
elif(analysis.sentiment[0])<0:
intent = 'Negative'
else:
intent = 'Neutral'
print(intent)
sentence = "I need car insurance"
computekeywords(sentence)
sentence2 = "I lost my credit card"
computekeywords(sentence2)
#I need car insurance
#using spacy
#Keywords using Rake
#[('car insurance', 4.0)]
#Sentiment of the sentence
#Neutral
#I lost my credit card
#using spacy
#Keywords using Rake
#[('credit card', 4.0), ('lost', 1.0)]
#Sentiment of the sentence
#Neutral
view raw NLP_Keywords.py hosted with ❤ by GitHub
Happy Mastering DL!!!!

No comments: