Installation
You can install the package using the .NET command-line interface (CLI).
dotnet add package aliyun-net-sdk-alinlp
For more information about installation and usage, see aliyun-openapi-net-sdk · GitHub.
Configure access credentials using environment variables
Notes:
An AccessKey for an Alibaba Cloud account has access permissions for all APIs, which poses a high security threat. We recommend that you create and use a Resource Access Management (RAM) user for API calls or daily operations and maintenance. To create a RAM user, log on to the RAM console.
To prevent key leaks, do not hardcode the AccessKey ID and AccessKey secret into your code. Instead, configure environment variables to store and access your AccessKey pair.
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 information about how to obtain an AccessKey ID and an AccessKey secret, see Step 2: Obtain an AccessKey for your account.
Configuration for Windows
Create a file for environment variables. In the file, add the
NLP_AK_ENVandNLP_SK_ENVenvironment variables and set their values to your AccessKey ID and AccessKey secret.Restart Windows.
Example call
The following sample code shows how to call the Chinese word segmentation algorithm.
using System;
using System.Collections.Generic;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core.Auth;
using Aliyun.Acs.alinlp.Model.V20200629;
namespace AlibabaCloud.SDK.Sample
{
class Program
{
static void Main(string[] args)
{
/**
* An Alibaba Cloud account AccessKey has access permissions for all APIs, which poses a high security threat. Create and use a RAM user for API calls or daily O&M. To create a RAM user, log on to the RAM console.
* 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 leaks, do not hardcode the AccessKey ID and AccessKey secret into your code.
*/
string accessKeyId = Environment.GetEnvironmentVariable("NLP_AK_ENV");
string accessKeySecret = Environment.GetEnvironmentVariable("NLP_SK_ENV");
AlibabaCloudCredentialsProvider provider = new AccessKeyCredentialProvider(accessKeyId, accessKeySecret);
/* use STS Token
AlibabaCloudCredentialsProvider provider = new StsCredentialProvider("<your-access-key-id>", "<your-access-key-secret>", "<your-sts-token>");
*/
IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou");
// Create a client to send requests.
DefaultAcsClient client = new DefaultAcsClient(profile, provider);
// Create a request.
var request = new GetWsChGeneralRequest();
request.ServiceCode = "alinlp";
request.TokenizerId = "GENERAL_CHN";
request.Text = "Smart body fat scale precision Bluetooth scale weight measurement human body fat scale home use health electronic scale";
request.OutType = "1";
try
{
// Send the request.
var response = client.GetAcsResponse(request);
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
catch (ServerException e)
{
Console.WriteLine(e);
}
catch (ClientException e)
{
Console.WriteLine(e);
}
}
}
}
Note: In GetWsChGeneralRequest, GetWsChGeneral is the action name of the algorithm. Replace GetWsChGeneral with the action name of the algorithm that you want to use and update the corresponding parameters. You can find the action name in the API reference document. It is the sample value for the Action parameter in the Request parameters section.
Troubleshoot call exceptions
If an exception occurs during a call, see Troubleshoot call exceptions (error code summary). This document contains a table where you can find the description for your error code. The description explains the cause of the error and provides a solution.