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 def get_tencent_emotion_analysis(text, secret_id, secret_key): try: cred = credential.Credential(secret_id, secret_key) http_profile = HttpProfile() http_profile.endpoint = "nlp.tencentcloudapi.com" client_profile = ClientProfile() client_profile.httpProfile = http_profile client = nlp_client.NlpClient(cred, "ap-guangzhou", client_profile) req = models.SentimentAnalysisRequest() params = { "Text": text, "Flag": 2, "Mode": "2class" } req.from_json_string(json.dumps(params)) resp = client.SentimentAnalysis(req) emotion_dict = json.loads(resp.to_json_string()) return emotion_dict except TencentCloudSDKException as err: print(err) get_tencent_emotion_analysis('我很喜欢你!', 'AKIDlLfiU0FDuKUNyIL7bpmSrypPikUxGL9g', 'II1TeYAVZMs75mBJBl46qJVQ4RXLs9Au')