Sarcasm detection
Irony and sarcasm are rhetorical devices common in social media.
Irony is the use of words to convey a meaning that is the opposite of their literal meaning, often with a negative, sarcastic, or mocking intent. It is a figure of speech that carries strong emotional overtones. Sarcasm uses techniques such as metaphor and exaggeration to expose, criticize, or mock people or things. Sarcasm is a type of irony that involves emotions, such as aggression. For simplicity, this document refers to both as 'sarcasm' and makes no further distinction between them.
This service is provided by the NLP Self-Learning Platform and is available through direct API calls.
Service activation and resource plan purchase
Before you begin, ensure that you have activated the service. After activation, you can purchase a resource plan.
Activate service: Activation page
Purchase resource plan: Purchase page
Service invocation and testing
For more information about model invocation, see Model invocation.
For more information about software development kit (SDK) examples, see SDK examples.
Testing
Configure access credentials using environment variables
Note:
An Alibaba Cloud account AccessKey has access permissions for all APIs, which poses a high security threat. We strongly recommend that you create and use a Resource Access Management (RAM) user for API access or daily operations and maintenance (O&M). You can log on to the Resource Access Management (RAM) console to create a RAM user.
To prevent key leakage, do not hardcode your AccessKey ID and AccessKey secret in your code. Instead, configure them as environment variables.
Configure on Linux and macOS systems
export NLP_AK_ENV=<access_key_id> export NLP_SK_ENV=<access_key_secret>Replace <access_key_id> with your AccessKey ID and <access_key_secret> with your AccessKey secret. For more information about how to obtain an AccessKey ID and AccessKey secret, see Step 2: Obtain an AccessKey.
Configure on a Windows system
Create a new file to define the environment variables. Add the
NLP_AK_ENVandNLP_SK_ENVenvironment variables, and set their values to your AccessKey ID and AccessKey secret.Restart the Windows system.
Java code example
// An Alibaba Cloud account AccessKey has access permissions for all APIs, which poses a high security threat. We strongly recommend that you create and use a RAM user for API access or daily O&M. Log on to the RAM console to create a RAM user.
// This example shows how to store the AccessKey ID and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
// To prevent key leakage, do not hardcode your AccessKey ID and AccessKey secret in your code.
String accessKeyId = System.getenv("NLP_AK_ENV");
String accessKeySecret = System.getenv("NLP_SK_ENV");
DefaultProfile defaultProfile = DefaultProfile.getProfile("cn-hangzhou",accessKeyId,accessKeySecret);
IAcsClient client = new DefaultAcsClient(defaultProfile);
String content = "{\"content\": \"Came home to a enormous mess and said, 'It's so good to be back.'\"}";
RunPreTrainServiceRequest request = new RunPreTrainServiceRequest();
request.setServiceName("Irony-Computation");
request.setPredictContent(content);
RunPreTrainServiceResponse response = client.getAcsResponse(request);
System.out.println(response.getPredictResult());Python code example
# Install dependencies
pip install aliyun-python-sdk-core
pip install aliyun-python-sdk-nlp-automl# -*- coding: utf8 -*-
import json
import os
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdknlp_automl.request.v20191111 import RunPreTrainServiceRequest
# An Alibaba Cloud account AccessKey has access permissions for all APIs, which poses a high security threat. We strongly recommend that you create and use a RAM user for API access or daily O&M. Log on to the RAM console to create a RAM user.
# This example shows how to store the AccessKey ID and AccessKey secret in environment variables. You can also store them in a configuration file as needed.
# To prevent key leakage, do not hardcode your AccessKey ID and AccessKey secret in your code.
access_key_id = os.environ['NLP_AK_ENV']
access_key_secret = os.environ['NLP_SK_ENV']
# Initialize AcsClient instance
client = AcsClient(
access_key_id,
access_key_secret,
"cn-hangzhou"
);
content = {"content": "Came home to a enormous mess and said, 'It's so good to be back.'"}
# Initialize a request and set parameters
request = RunPreTrainServiceRequest.RunPreTrainServiceRequest()
request.set_ServiceName('Irony-Computation')
request.set_PredictContent(json.dumps(content))
# Print response
response = client.do_action_with_exception(request)
resp_obj = json.loads(response)
predict_result = json.loads(resp_obj['PredictResult'])
print(predict_result)PredictResult code example
{
"RequestId": "B10B7383-1776-5209-9EB6-647DF53F51B5",
"PredictResult": "{\"sarcasm\":[{\"score\":0.989,\"key\":\"yes\"},{\"score\":0.011,\"key\":\"no\"}]}"
}Request parameters
Parameter | Description |
content | The text sample to analyze. |
Response parameters
Parameter | Description |
key | Indicates whether the text is sarcastic. Valid values: yes, no. |
score | The confidence score. |