Solving NLP Problems Quickly with IBM Watson NLP

Let’s explore some of the out-of-the-box NLP models provided by IBM Watson NLP

Partha Pratim Neog
Towards Data Science

--

Source: Pixabay

As data scientists, before starting on the developing the models, by trying to find and download open-source models or develop models on your own, wouldn’t it be nice to have all of these handy and available to be implemented in just a few lines of code? That’s exactly what I will show you today in this blog post. With Watson NLP, you get state-of-the art pre-trained models for numerous NLP use-cases that can get you up and running in just a few hours if not less. These models are also re-trainable with custom domain specific knowledge if required.

We’ll take the NLP use-case of sentiment analysis for this example; where we need to do three things.

  1. Extract sentiment of a text.
  2. Extract custom target mentions.
  3. Extract the sentiments of the target words.

We’ll use a review on the trending Game of Thrones prequel, House of Dragons from IMDB for this example. Following is the review.

This happens so often when a sequel or prequel gets made after a highly successful show has made its mark…Once that benchmark is set, it can be a tough if not Impossible act to follow… Such is the case here..There Are exceptions to this rule such as the Battlestar Galactica remake of the old original series wherein the successor Exceeded the original in every metric.. So it happens, but not here with GOT…Right off the bat, the initial opening scenes with the dragon and the young girl who is I guess, this shows version of Daneerys, All these scenes show a CGI that is severely lacking compared to GOT…The color palete looks faded, lacking detail and really, it looks like a computer rendered background that would not fool anyone… yes of coursre, it IS computer rendered but a good CGI will Not draw attention to that fact..It will Make you Believe… Not so here……The Dragon in the opening scene looks Wayy off which is wild since the original GOT had dragons rendered with cinema quality appearance of the highest order…Quite frankly, these dragons here look pathetic, lacking detail, menace, or sheer presence…its absent…. Being that there are 10 dragons in this show, maybe this is the runt of the litter but they Should have done Better.. As for the acting,initially I was offput by the casting and as many commented, I didnt feel an immediate connection with anyone here…Upon wading thru the 1st half of the show, I Will say that elements of the original GOT’s vibe and atmosphere Are there and Do infuse some of the scenes , especially those with the young princess and her father and the dialogues of the father and Damon…Its not totally absent ,it IS there but in the format of a lighter budget show with lower caliber actors and effects and sets…Its Not so bad that you Cant get into it and I’d suggest suspending the sure to come judgements till you are a few shows into the series…Theres hardly any new series that doesnt need a few shows under its belt to hit its stride and hook in the viewers… most new casting feels stiff and wooden as actors are just getting Into their roles and have yet to inhabit them to the point of being Convincing…Yes, this show too is doing the wokeness theme and politically correct diversity dance as is just about Everything these days.. it is what it is and its part of the new world consciousness, it infuses Everything …it can seem too “forced” and not a natural expression of the storylines when writers and casting try to conform to and make the story fit these current trends… that said, if you loved GOT, which i do , then certainly this is worth a watch to see how it evolves…why complain when a favorite series is given further expression? Enjoy it for what it is and those who do not want to see it, turn on Something Else…Simple as that!

For this exercise, we will need the Syntax Analysis model (that handles basic NLP operations like tokenisation, lemmatisation, POS tagging etc), a Rule Based Mentions model that detects where the target keywords were mentioned, and a Target Sentiment Model.

Download and load the models.

import watson_nlp
from watson_nlp.toolkit.sentiment_analysis_utils.output.document_sentimet import predict_document_sentiment
# download and load the Syntax model
syntax_model = watson_nlp.download_and_load(‘syntax_izumo_en_stock’)
# download and load the Sentence sentiment model.
sentiment_model = watson_nlp.download_and_load(‘sentiment_sentence-bert_multi_stock’)
# download and load the Target sentiment model
targeted_sentiment_model = watson_nlp.download_and_load(‘sentiment_targeted-cnn_en_stock’)

Target Sentiment

We’ll first configure the rule based model to extract the target mentioned from the review. We’ll set the targets COLOR, DRAGON, Game of Thrones (GoT), CGI and ACTOR. This can be done by creating a CSV file having two columns Label and Entry. Entry would have the keyword as it appears in the text. This needs to be the base version of a word, as the algorithm does Lemma match as well. For example; if you set it as Mouse, the algorithm will detect mention of Mice as well. Label would have the label under which you want to group the target. For example; the keywords cat and dog can be under the same Label ANIMAL.

import os
import watson_nlp
module_folder = “NLP_Dict_Module_1”
os.makedirs(module_folder, exist_ok=True)
# Create a table dictionary
table_file = ‘features.csv’
with open(os.path.join(module_folder, table_file), ‘w’) as features:
features.write(“\”label\”,\”entry\””)
features.write(“\n”)
features.write(“\”COLOR\”,\”color\””)
features.write(“\n”)
features.write(“\”DRAGON\”,\”dragon\””)
features.write(“\n”)
features.write(“\”Game of Thrones\”,\”GOT\””)
features.write(“\n”)
features.write(“\”CGI\”,\”CGI\””)
features.write(“\n”)
features.write(“\”ACTOR\”,\”actor\””)
features.write(“\n”)
# Load the dictionaries
dictionaries = watson_nlp.toolkit.rule_utils.DictionaryConfig.load_all([{
'name': 'feature mappings',
'source': table_file,
'dict_type': 'table',
'case': 'insensitive',
'lemma': True,
'mappings': {
'columns': ['label', 'entry'],
'entry': 'entry'
}
}])
# Train the rule based model on the above targets
custom_dict_block = watson_nlp.resources.feature_extractor.RBR.train(module_folder,
language='en', dictionaries=dictionaries)

Now that the rule base model to extract the target mentions is trained, we call the sentiment analysis model. Note that the models need to be called in order since the output of one needs to be the input of the other. We start with the syntax model, followed by the mentions model and then finally the target sentiment model which takes both the output of the syntax and mentions models as input.

def get_lemma(target):
“””
Gets the lemma of a target text.
“””
lemmas = [token[‘lemma’] if token[‘lemma’] != “” else token[‘span’][‘text’] for token in syntax_model.run(target, parsers=(‘token’, ‘lemma’)).to_dict()[‘tokens’]]
return “ “.join(lemmas)
def get_text_label(text):
“””
Gets the label of a text from the target feature list csv.
“””
text = get_lemma(text)
try:
label = feature_df[feature_df[‘entry’].str.lower() == text.lower()][‘label’].values[0]
except:
label = None
return label
def extract_mentions(text):
“””
Extracts the spans where the target features have been mentioned in the text.
“””
mentions = defaultdict(list)
for mention in custom_dict_block.run(text):
mentions[get_text_label(mention.text)].append((mention.begin, mention.end))

return mentions.values()
def target_sentiment_of_line(text):
syntax_result = syntax_model.run(text, parsers=(‘token’, ‘lemma’))

targetMentions = extract_mentions(text)
targeted_sent_result = targeted_sentiment_model.run(syntax_result, targetMentions, show_neutral_scores=False)
return targeted_sent_result

We can now pass the text to the target_sentiment_of_line function defined above, and get following results (we get a JSON response, but I have formatted it into a excel file for readability).

Output of target_sentiment_of_line function, formatted in Excel

For each target, we get an aggregated score and also scores for each individual sentence where the target was detected. For example, GOT was detected in four sentences and the overall sentiment if positive. However, the first mention of GOT was detected as negative, and the remaining mentions were positive.

A sentiment score can be between -1 and 1. -1 is the most negative sentiment and 1 is the most positive sentiment, whereas 0 is neutral. A value of -0.4 is less negative than a value of -0.98. Similarly a value of 0.3 is less positive than a value of 0.99

Overall Sentiment

Let’s also get the overall sentiment of the text by calling the sentence sentiment model as seen below.

def sentiment_of_line(text):
# Run the syntax model on the input text
syntax_prediction = syntax_model.run(text)
# Run the sentiment model on the result of syntax
sentiment_result = sentiment_model.run_batch(syntax_prediction.get_sentence_texts(), syntax_prediction.sentences)
# Aggregate the sentiment of all the sentences in the text
document_sentiment = predict_document_sentiment(sentiment_result, sentiment_model.class_idxs, combine_approach="NON_NEUTRAL_MEAN")
return document_sentiment

On passing the text to the function sentiment_of_line, we get the overall sentiment as NEGATIVE and score as -0.424164.

Conclusion

To conclude, the highlight of the Watson NLP for me was the ability to quickly get started with NLP use-cases at work without having to worry about collecting datasets, developing models from scratch. This helps getting up and running quickly. If required, we can easily retrain the models later on with domain specific data.

--

--