Sentiment Analysis (English)

Updated at:

The English sentiment analysis service predicts the sentiment of short texts from sources such as social media for E-commerce. The service classifies sentiment as positive, neutral, or negative.

Note

This service is provided by the NLP Self-Learning Platform. You can use this service by calling the API directly.

Service activation and resource plan purchase

Before you use the service, you must activate it. After the service is activated, you can purchase a resource plan.

Service invocation and testing

For more information about how to invoke the model, see Model invocation.

For more information about software development kit (SDK) examples, see SDK examples.

Debug

You can run this API operation directly in OpenAPI Explorer, which eliminates the need to calculate signatures. After a successful call, OpenAPI Explorer automatically generates sample SDK code.

Configure access credentials (AccessKey ID and AccessKey secret) using environment variables

  1. Notes:

    1. An AccessKey pair for an Alibaba Cloud account has permissions for 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 routine O&M. To create a RAM user, log on to the RAM console.

    2. Do not hard-code your AccessKey ID and AccessKey secret in your code because this can cause your keys to leak. Instead, configure them as environment variables.

  2. Configuration for Linux and macOS

    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 an AccessKey secret, see Step 2: Obtain an AccessKey pair.

  3. Windows system configuration

    1. Create a file for environment variables. Add the NLP_AK_ENV and NLP_SK_ENV environment variables, and then set their values to your AccessKey ID and AccessKey secret.

    2. Restart your Windows system.

Java code example

/**
 * An AccessKey pair for an Alibaba Cloud account has permissions on all API operations. This poses a high security threat.
 * We strongly recommend that you create and use a RAM user for API access or routine 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.
 * Do not hard-code your AccessKey ID and AccessKey secret in your code. This can cause your keys to leak.
 */
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);
Map<String, Object> map = new HashMap<>();
List<String> messages = ArrayList<>();
messages.add("Feels more like a joke");
map.put("messages", messages);
RunPreTrainServiceRequest request = new RunPreTrainServiceRequest();
request.setServiceName("NLP-En-Sentiment-Analysis");
request.setPredictContent(JSON.toJSONString(map));
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 AccessKey pair for an Alibaba Cloud account has permissions on all API operations. This poses a high security threat.
# We strongly recommend that you create and use a RAM user for API access or routine 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.
# Do not hard-code your AccessKey ID and AccessKey secret in your code. This can cause your keys to leak.
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"
);
messages=["Feels more like a joke"]
content ={"messages": messages}
# Initialize a request and set parameters
request = RunPreTrainServiceRequest.RunPreTrainServiceRequest()
request.set_ServiceName('NLP-En-Sentiment-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['predictions'])

Example of PredictContent

{
  "messages": ["Feels more like a joke"],
}

Example of PredictResult

{
  "predictions":["negative"]
}

Request parameters

Parameter

Description

messages

A list of texts to predict.

Response parameters

Parameter

Description

predictions

A list of sentiment values corresponding to the texts.