I am new to Python but slowly using available resources to make good progress from a coding perspective. However, I can't find help for a specific use case that I am trying to solve. I have survey comments that I want to analyze using sentiment analysis. I have been able to get Python NLTK and spaCy to process the text for sentiment analysis, but the comments I have are complicated, typically containing two independent thoughts.
For example, say I am analyzing text comments about pets and I have this sentence: "Dogs make better pets than Alligators". What I want to do is get a positive sentiment for Dogs and a negative (or neutral) for Alligators.
I am using spacy to identify sentences with specific phrases (I think of these as topics). I am using SIA from NLTK to do the actual analysis.
For the sentence "Dogs make better pets than Alligators." the code is correctly processing it twice since it matches the phrase "Dogs" and "Alligtors". But I need to have SIA process it once with "Dogs" as the topic and once with "Alligators" as the topic and I can't figure out how to get SIA to do that.
import nltk
import spacy
from nltk.sentiment import SentimentIntensityAnalyzer
sia = SentimentIntensityAnalyzer()
nlp = spacy.load("en_core_web_sm")
text = """Dogs and cats make great pets.
Alligators make poor pets.
Dogs make better pets than Alligators."""
from spacy.matcher import PhraseMatcher
phrase_matcher = PhraseMatcher(nlp.vocab)
phrases = ['Dogs', 'Alligators']
patterns = [nlp(text) for text in phrases]
phrase_matcher.add('Pets', None, *patterns)
doc = nlp(text)
for sent in doc.sents:
for match_id, start, end in phrase_matcher(nlp(sent.text)):
if nlp.vocab.strings[match_id] in ["Pets"]:
res = sia.polarity_scores(sent.text)
print(res, " score for sentence: ", sent.text)