Python-分析法语文本.法国情人



如果我想将其用于法语文本,我真的不明白是否需要设置特定的内容。我已经阅读了Azure文档。他们说要用";fr";密码但我真的不知道该放在哪里。你知道吗?

#Azure lib
from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential
azurekey = ""
azureendpoint = ""
def authenticate_client():
ta_credential = AzureKeyCredential(azurekey)
text_analytics_client = TextAnalyticsClient(
endpoint=azureendpoint, 
credential=ta_credential)
return text_analytics_client
clientazure = authenticate_client()
if self.content:
documents = [self.content]

response = clientazure.analyze_sentiment(documents = documents)[0]
try:
self.emotion = "sentiment: {}".format(response.sentiment) + " detail: positive={0:.2f}; neutral={1:.2f}; negative={2:.2f} n".format(response.confidence_scores.positive,response.confidence_scores.neutral,response.confidence_scores.negative,)
except Exception as e:
self.emotion = None

result = clientazure.recognize_entities(documents = documents)[0]
for entity in result.entities:
try:
self.topic = entity.text
except Exception as e:
self.topic = None
try:
self.category = entity.category
except Exception as e:
self.category = None

语言代码可以放在每个文本文档中,也可以放在整个批次中。如果您希望"fr"应用于所有文档,请将其传递到对analyze_sentiment()的调用中:

response = clientazure.analyze_sentiment(documents = documents, language="fr")

如果只希望语言代码应用于单个文档,请在文档级别传入语言代码。假设self.content包含要分析的文本,它看起来像这样:

documents = [{"id": "1", "language": "fr", "text": self.content}]

最新更新