Tencent Cloud Nlp SDK for Python

raw JSON →
3.1.80 verified Sat May 09 auth: no python

Official Tencent Cloud SDK for Natural Language Processing (NLP) service. Provides Python client classes for text classification, sentiment analysis, keyword extraction, and more. Current version 3.1.80, released monthly.

pip install tencentcloud-sdk-python-nlp
error ModuleNotFoundError: No module named 'tencentcloud'
cause Common package not installed
fix
pip install tencentcloud-sdk-python-common
error ImportError: cannot import name 'SentimentAnalysisRequest' from 'tencentcloud.nlp.models'
cause Wrong import path; models are under versioned subpackage
fix
from tencentcloud.nlp.v20190408 import models
gotcha The SDK is split into per-service packages. You must install both tencentcloud-sdk-python-common and the specific package (e.g., tencentcloud-sdk-python-nlp). Installing only the NLP package does not pull in common automatically.
fix pip install tencentcloud-sdk-python-common tencentcloud-sdk-python-nlp
breaking In v3, the model classes and client classes have been moved under versioned paths (v20190408). Old v2-style imports (e.g., from tencentcloud.nlp.models import SentimentAnalysisRequest) no longer work.
fix Use from tencentcloud.nlp.v20190408 import models
gotcha Many API request objects require explicitly setting all fields. Forgetting to set required fields (like Text in SentimentAnalysisRequest) will cause a MissingParameter error.
fix Always check the official API documentation for required fields and set them before calling the method.

Initialize client and perform sentiment analysis

from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.nlp.v20190408 import nlp_client, models

try:
    cred = credential.Credential(
        secret_id=os.environ.get('TENCENTCLOUD_SECRET_ID'),
        secret_key=os.environ.get('TENCENTCLOUD_SECRET_KEY')
    )
    client = nlp_client.NlpClient(cred, "ap-guangzhou")
    req = models.SentimentAnalysisRequest()
    req.Text = "今天天气真好"
    resp = client.SentimentAnalysis(req)
    print(resp.to_json_string())
except TencentCloudSDKException as e:
    print(e)