Sentiment analysis (Russian)

更新时间:
复制 MD 格式

The Sentiment Analysis (Spanish) prediction service provides sentiment predictions for short texts from social media in E-commerce scenarios, classifying the sentiment as positive, neutral, or negative.

Note

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

Service activation and resource plan purchase

Before you start, ensure that the service is activated. After you activate the service, you must purchase a resource plan.

Service invocation and debugging

For more information about how to call the model, see Model Invocation.

For more information about SDK examples, see SDK Examples.

Debug

Run this API operation in OpenAPI Explorer to avoid calculating signatures. After a successful call, OpenAPI Explorer automatically generates SDK code examples.

Configure access credentials using environment variables

  1. Notes:

    1. The AccessKey pair of an Alibaba Cloud account has permissions to access all APIs, which poses a security risk. For improved security, we recommend that you create and use a Resource Access Management (RAM) user for API access or daily operations and maintenance (O&M). To create a RAM user, log on to the RAM console.

    2. Do not hardcode your AccessKey ID and AccessKey secret in your code. This can lead to credential leaks. Instead, configure environment variables to store and access your credentials.

  2. Configure on 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. Configure on Windows

    1. Create the NLP_AK_ENV and NLP_SK_ENV environment variables. Set the value of NLP_AK_ENV to your AccessKey ID and the value of NLP_SK_ENV to your AccessKey secret.

    2. Restart Windows.

Java code example

/**
 * An AccessKey pair of an Alibaba Cloud account has permissions to access all APIs. This is a high security risk.
 * Create and use a Resource Access Management (RAM) user for API access or daily O&M. This is strongly recommended.
 * 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 save your AccessKey ID and AccessKey secret in your code. This can lead to credential leaks.
 */
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 = new ArrayList<>();
messages.add("Игровая консоль BoyHom RS37 (Dendy 8-бит) - Распаковка");
map.put("messages", messages);
RunPreTrainServiceRequest request = new RunPreTrainServiceRequest();
request.setServiceName("NLP-Ru-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 of an Alibaba Cloud account has permissions to access all APIs. This is a high security risk.
# Create and use a Resource Access Management (RAM) user for API access or daily O&M. This is strongly recommended.
# 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 save your AccessKey ID and AccessKey secret in your code. This can lead to credential leaks.
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=["Игровая консоль BoyHom RS37 (Dendy 8-бит) - Распаковка"]
content ={"messages": messages}
# Initialize a request and set parameters
request = RunPreTrainServiceRequest.RunPreTrainServiceRequest()
request.set_ServiceName('NLP-Ru-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'])

PredictContent example

{
  "messages": ["Игровая консоль BoyHom RS37 (Dendy 8-бит) - Распаковка"],
}

PredictResult example

{
  "predictions":["neutral"]
}

Request parameters

Parameter

Description

messages

List of texts to predict

Response parameters

Parameter

Description

predictions

A list of sentiment values that correspond to the text.