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

August 19, 2023

Aspect-based sentiment analysis - NLP

Aspect-based sentiment analysis is about identifying different aspects in a given topic. TextBlob will help us to calculate the polarity of the sentiments


!pip install TextBlob
import nltk
nltk.download('brown')
nltk.download('punkt')
#Aspect-based sentiment analysis is about identifying different aspects in a given topic. TextBlob will help us to calculate the polarity of the sentiments.
from textblob import TextBlob
text = 'The sound quality of this phone is incredible. But the battery life is quite poor.'
blob = TextBlob(text)
# Noun phrases in the text
aspects = blob.noun_phrases
polarity = []
for sentence in blob.sentences:
polarity.append(sentence.sentiment.polarity)
# Mapped aspects and their sentiment polarity
aspect_sentiment = dict(zip(aspects, polarity))
print(aspect_sentiment)
#A polarity of 0 is considered neutral, above 0 is positive, and below 0 is negative.
#https://neurosys.com/blog/intro-to-coreference-resolution-in-nlp
#Co-reference resolution is about detecting all expressions that refer to the same entity in a text.
#We can use Hugging Face's transformers library. Please note that it requires PyTorch to be installed.
from transformers import pipeline
nlp = pipeline('feature-extraction')
text = 'Jim is a programmer. He loves coding.'
result = nlp(text)
print(result)
#In the 'text' variable, "Jim" and "He" are referring to the same entity.





  • context exclusion
  • intent extraction
  • co-reference resolution
  • sentiment analysis

Keep Exploring!!!

No comments: