Codeq NLP API Tutorial

In this tutorial we describe how you can call some of the text classifiers of our NLP API; specifically, we showcase modules that are focused on understanding the intrinsic semantics and pragmatics of texts.

This is a follow up tutorial; previous content can be found here:

The complete list of modules in the Codeq NLP API can be found in our documentation:

Define a Pipeline

The first thing to do is to create an instance of the Codeq Client using your API credentials and define a pipeline containing some text classifiers:

from codeq_nlp_api import CodeqClientclient = CodeqClient(user_id="USER_ID", user_key="USER_KEY")pipe = [
"sentiment", "emotion", "sarcasm"
]

Analyze a text

The client is used to analyze a text and retrieve a Document object with a list of Sentences, where the output of the classifiers is stored, i.e., each sentence will contain the prediceted labels for sentiment, emotion and sarcasm.

To print a quick overview of the results, you can use the method document.pretty_print():

text = "The hotel location is perfect. But the rooms are in great need of an upgrade."document = client.analyze(text, pipeline=pipe)print(document.pretty_print())

In the following sections we will describe:

  • the keyword (KEY) used to call each classifier,

Sentiment Classifier

This annotator analyzes sentences and tries to determine whether they convey a positive, negative or neutral sentiment.

  • KEY: sentiment

OUTPUT LABELS:

  • Positive
text = "The hotel location is perfect. Located directly across from the metro so it is easy to get everywhere. But the rooms are in great need of an upgrade. I had a king room with no view, and poor lighting. The color scheme in the room was drab and kind of depressing."

document = client.analyze(text, pipeline=pipe)
for sentence in document.sentences:
raw_sentence = sentence.raw_sentence
sentiments = sentence.sentiments
print("\n%s - %s" % (sentiments, raw_sentence))
# OUTPUT:
#
# ['Positive'] - The hotel location is perfect.
#
# ['Positive'] - Located directly across from the metro so it is easy to get everywhere.
#
# ['Negative'] - But the rooms are in great need of an upgrade.
#
# ['Negative'] - I had a king room with no view, and poor lighting.
#
# ['Negative'] - The color scheme in the room was drab and kind of depressing.

Emotion Classifier

The emotion annotator analyzes sentences and predicts if they include one or more emotions or no emotion at all. In contrast to the Sentiment Classifier, this annotator provides a wider granularity when analyzing the feelings expressed in the input text.

In this annotator, sentences with emotional content that don’t achieve high enough probability of a given class are assigned “Unknown emotion”. Sentences perceived as not bearing any emotion are assigned the label “No Emotion”.

More details about this annotator can be found here:

  • KEY: emotion

OUTPUT LABELS:

  • Anger
text = "The hotel location is perfect. Located directly across from the metro so it is easy to get everywhere. But the rooms are in great need of an upgrade. I had a king room with no view, and poor lighting. The color scheme in the room was drab and kind of depressing."

document = client.analyze(text, pipeline=pipe)
for sentence in document.sentences:
raw_sentence = sentence.raw_sentence
emotions = sentence.emotions
print("\n%s - %s" % (emotions, raw_sentence))
# OUTPUT:
#
# ['Joy/Like'] - The hotel location is perfect.
#
# ['Unknown emotion'] - Located directly across from the metro so it is easy to get everywhere.
#
# ['Disgust/Dislike'] - But the rooms are in great need of an upgrade.
#
# ['Disgust/Dislike'] - I had a king room with no view, and poor lighting.
#
# ['Disgust/Dislike', 'Sadness'] - The color scheme in the room was drab and kind of depressing.

Sarcasm Classifier

The goal of this annotator is to predict if a sentence is sarcastic or not. Sarcasm is an indirect act of speech with a high inherent ambiguity and its detection has become an important task in Natural Language Understanding. The Sarcasm Classifier can be considered as a complementary analysis for both the Sentiment and Emotion classifiers.

In the following post you can discover more details about this annotator:

  • KEY: sarcasm

OUTPUT LABELS:

  • Sarcastic
text = "I just love it when people make plans for me without actually including me in this process"document = client.analyze(text, pipeline=pipe)for sentence in document.sentences:
raw_sentence = sentence.raw_sentence
sarcasm = sentence.sarcasm
print("\n%s - %s" % (sarcasm, raw_sentence))
# OUTPUT:
#
# Sarcastic - I just love it when people make plans for me without actually including me in this process

Wrap up

Here we described three annotators that can be used to extract insights about the semantics behind language utterances. The code below summarizes how to access the output labels of each annotator.

  • Take a look at our documentation to learn more about the NLP tools we provide.

Senior Computational Linguist at Codeq