How to conduct targeted sentiment analysis at the target level? (IBM Watson Cognitive)

69 Views Asked by At

I'm trying to assess the sentiment related to specific words that appear in a body of text. Currently I'm using IBM Watson's targeted sentiment analysis (natural_language_understanding.analyze). However, if I have two target words "apple" and "orange", and the sentence "I like apples but I do not like oranges.", the analyzer will give the same score to both targets because the program ultimately uses the targets to identify sentences in the text, and then provides the sentiment of that sentence. Is there an alternative that provides target-level sentiment, rather than sentence-level sentiment, such that a positive numeric score is produced for "apples" and a negative numeric score is produced for "oranges"?

I've looked into using sentiment-targeted_bert_multi_stock from IBM's aspect-oriented sentiment analysis page, however, it seems to only provide a label of sentiment_positive, sentiment_neutral, or sentiment_negative as output.

1

There are 1 best solutions below

0
matano On

Target-level sentiment, or Targeted sentiment analysis (TSA), is available through IBM's Watson Natural Language Processing.

There are existing models that may be reused, based on IBM's foundation models, for example:

import watson_nlp
    
# Load the model using its name
model_name = 'targets-sentiment_transformer-workflow_multilingual_slate.153m.distilled-cpu'
tsa_model = watson_nlp.load(model_name)
    
# Run the model on an input text
text = "I like apples but I do not like oranges."
tsa_predictions = tsa_model.run(text)

# For visualization or further analysis, convert the output to a DataFrame
from watson_nlp.pandas.converters.sentiment import targets_sentiment_to_pandas
target_mentions = targets_sentiment_to_pandas(text, tsa_predictions)

print(target_mentions)

Will output:

#                   span sentiment  confidence
# 0    [7, 13): 'apples'  positive    0.993077
# 1  [32, 39): 'oranges'  negative    0.992382

Within IBM Products

TSA is available through Watson Studio in IBM's WatsonX (see here) and Cloud Pak for Data (see here). The documentation appears under Targets sentiment extraction. The NLP runtimes shipped with in these products support two TSA models out-of-the-box, one suited for an environment with a GPU, and another which is CPU-optimized. These models can be utilized in a notebook-based environment using, for example, the above code sample.