Humor is the quality of being amusing. It is a special form of expression that can liven up a room or ease an awkward moment. The humor classification task analyzes text to predict its type of humor, such as homophonic, semantic, or reversal.
This service is provided by the Natural Language Processing (NLP) Self-Learning Platform. You can use this service by calling the API directly.
Service activation and resource plan purchase
Before you use this service, you must activate it. After the service is activated, you can purchase a resource plan.
Activate the service: Activation page
Purchase a resource plan: Purchase page
Service invocation and testing
For more information about how to call the model, see Model invocation.
For more information about software development kit (SDK) examples, see SDK examples.
Test
Examples of different humor types:
Example 1: Homophonic humor: After the college entrance exam, I knew I had not done well. I told my father that my scores were not ideal. My father sighed and said, "If it doesn't work out, you can just repeat the year." My grandfather, who was sitting nearby, became angry and said, "If you can't pass, you can't pass! Why would you take poison?! So spineless!" The humor is based on a pun because the Chinese words for "repeat the year" (复读, fùdú) and "take poison" (服毒, fúdú) sound identical.
Example 2: Semantic humor: A father is walking with his son on the street when a love song starts playing from a nearby speaker. The son starts to sing along. The father stops him, "Don't sing that. That song isn't healthy." The son asks curiously, "Why? Will singing it give me a cold?"
Example 3: Reversal humor: Mr. Zhou came home after shaving off his beard. His neighbor's little girl saw him and said, "Uncle Zhou! Looking at your face, you don't look like an old man at all." Mr. Zhou was delighted and asked, "Are you saying I look like a young man?" The girl replied, "No, I'm saying your face looks just like an old woman's!"
Configure access credentials using environment variables
Notes:
An AccessKey for an Alibaba Cloud account grants access permissions to all APIs, which poses a high security risk. We recommend that you create and use a Resource Access Management (RAM) user for API access or daily O&M. To create a RAM user, log on to the RAM console.
Do not save your AccessKey ID and AccessKey secret in your code because this can lead to key leakage. Instead, configure environment variables to save and access your AccessKey pair.
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 pair, see Step 2: Obtain an AccessKey pair.
Configure on a Windows system
Create a file to define the environment variables. Add the
NLP_AK_ENVandNLP_SK_ENVenvironment variables, and then assign your AccessKey ID and AccessKey secret as their respective values.Restart the Windows system.
Java code example
// An Alibaba Cloud account AccessKey has access permissions to all APIs. This poses a high security threat. Create and use a RAM user for API access or daily O&M. To create a RAM user, log on to the RAM console.
// This example shows how to save the AccessKey ID and AccessKey secret in environment variables. You can also save them in a configuration file as needed.
// Do not save your AccessKey ID and AccessKey secret in your code. This can lead to key leakage.
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 = "After the college entrance exam, I knew I did not do well. I told my father my scores were not ideal. My father sighed and said, 'If it does not work out, just repeat the year.' My grandfather, sitting nearby, got angry and said, 'If you cannot pass, you cannot pass! Why would you take poison?! So spineless!!!'";
RunPreTrainServiceRequest request = new RunPreTrainServiceRequest();
request.setServiceName("Humor-Analysis");
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 to all APIs. This poses a high security threat. Create and use a RAM user for API access or daily O&M. To create a RAM user, log on to the RAM console.
// This example shows how to save the AccessKey ID and AccessKey secret in environment variables. You can also save them in a configuration file as needed.
// Do not save your AccessKey ID and AccessKey secret in your code. This can lead to key leakage.
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 = "After the college entrance exam, I knew I did not do well. I told my father my scores were not ideal. My father sighed and said, 'If it does not work out, just repeat the year.' My grandfather, sitting nearby, got angry and said, 'If you cannot pass, you cannot pass! Why would you take poison?! So spineless!!!'"
# Initialize a request and set parameters
request = RunPreTrainServiceRequest.RunPreTrainServiceRequest()
request.set_ServiceName('Humor-Analysis')
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 content example
{
"RequestId": "F96B5C70-680C-5DF1-8305-F420A9A86CA6",
"PredictResult": "{\"type\":[{\"score\":0.9955,\"key\":\"homophonic\"},{\"score\":0.0043,\"key\":\"semantic\"},{\"score\":0.0003,\"key\":\"reversal\"}]}"
}Request parameters
Parameter | Description |
content | The text sample to analyze. |
Response parameters
Parameter | Description |
key | The predefined humor type, such as homophonic, semantic, or reversal. |
score | The confidence score. |