Python calls Tencent natural language processing api

Tencent Product Homepage:https://cloud.tencent.com/product

Subscribe to a service

Enter the website: https://console.cloud.tencent.com/nlp , scan WeChat to log in to Tencent Cloud, click 开通to open Tencent NLP service

Official document: https://cloud.tencent.com/document/api/271/35483

Configure SDK

Enter the website: https://console.cloud.tencent.com/cam/capi , click to 新建秘钥generate a new key, remember SecretIdtoSecretKey

Use pipinstallation in python :pip install -i https://mirrors.tencent.com/pypi/simple/ --upgrade tencentcloud-sdk-python

Code

Take sentiment analysis as an example:

import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.nlp.v20190408 import nlp_client, models
try:
    cred = credential.Credential("AKID0lFUC4D53WkhS4PgJ9kHZLRItFNFB9Ai", "WopcQ0LRJvGWBUhhSxTklM4Xkqgd4HF5")
    httpProfile = HttpProfile()
    httpProfile.endpoint = "nlp.tencentcloudapi.com"

    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    client = nlp_client.NlpClient(cred, "ap-guangzhou", clientProfile)

    req = models.SentimentAnalysisRequest()
    params = {
    
    
        "Text": "自然语言处理是计算机科学领域与人工智能领域中的一个重要方向"
    }
    req.from_json_string(json.dumps(params))

    resp = client.SentimentAnalysis(req)
    print(resp.to_json_string())

except TencentCloudSDKException as err:
    print(err)

For more code, please refer to the official code generation sample: https://console.cloud.tencent.com/api/explorer?Product=nlp&Version=2019-04-08&Action=SentimentAnalysis&SignVersion=

Guess you like

Origin blog.csdn.net/weixin_35757704/article/details/114531307
Recommended