你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

适用于 Python 的 Azure 文本分析 客户端库 - 版本 5.3.0

适用于语言的 Azure 认知服务是一项基于云的服务,它提供自然语言处理 (NLP) 用于理解和分析文本的功能,并包含以下main功能:

  • 情绪分析
  • 命名实体识别
  • 语言检测
  • 关键短语提取
  • 实体链接
  • 多重分析
  • 个人身份信息 (PII) 检测
  • 运行状况文本分析
  • 自定义命名实体识别
  • 自定义文本分类
  • 提取文本摘要
  • 抽象文本摘要

源代码 | 包 (PyPI) | 包 (Conda) | API 参考文档 | 产品文档 | 样品

入门

先决条件

创建认知服务或语言服务资源

语言服务支持 多服务和单服务访问。 如果计划通过一个终结点/密钥访问多个认知服务,请创建认知服务资源。 对于“仅语言服务访问”,请创建语言服务资源。 可以按照本文档中的步骤,使用 Azure 门户Azure CLI 创建资源。

使用客户端库与服务的交互从 客户端开始。 若要创建客户端对象,需要对资源使用认知服务或语言服务 endpoint ,以及 credential 允许访问:

from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient

credential = AzureKeyCredential("<api_key>")
text_analytics_client = TextAnalyticsClient(endpoint="https://<resource-name>.cognitiveservices.azure.com/", credential=credential)

请注意,对于某些认知服务资源,终结点可能与上述代码片段不同。 例如,https://<region>.api.cognitive.microsoft.com/

安装包

使用 pip 安装适用于 Python 的 Azure 文本分析 客户端库:

pip install azure-ai-textanalytics
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient
endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]

text_analytics_client = TextAnalyticsClient(endpoint, AzureKeyCredential(key))

请注意, 5.2.X 和 更新面向适用于语言 API 的 Azure 认知服务。 这些 API 包括以前版本的 文本分析 客户端库中的文本分析和自然语言处理功能。 此外,服务 API 已从语义化更改为基于日期的版本控制。 此版本的客户端库默认为最新支持的 API 版本,当前为 2023-04-01

下表显示了服务 SDK 版本与支持的 API 版本之间的关系

SDK 版本 服务支持的 API 版本
5.3.X - 最新稳定版本 3.0、3.1、2022-05-01、2023-04-01 (默认)
5.2.X 3.0、3.1、2022-05-01 (默认)
5.1.0 3.0、3.1(默认)
5.0.0 3.0

可以通过将 api_version 关键字 (keyword) 参数传递到客户端来选择 API 版本。 对于最新的语言服务功能,请考虑选择最新的 beta API 版本。 对于生产方案,建议使用最新的稳定版本。 设置为较旧版本可能会导致功能兼容性降低。

验证客户端

获取终结点

可以使用 Azure 门户Azure CLI 查找语言服务资源的终结点:

# Get the endpoint for the Language service resource
az cognitiveservices account show --name "resource-name" --resource-group "resource-group-name" --query "properties.endpoint"

获取 API 密钥

可以从 Azure 门户中的认知服务或语言服务资源获取 API 密钥。 或者,可以使用下面的 Azure CLI 代码片段获取资源的 API 密钥。

az cognitiveservices account keys list --name "resource-name" --resource-group "resource-group-name"

使用 API 密钥凭据创建 TextAnalyticsClient

获得 API 密钥的值后,可以将其作为字符串传递到 AzureKeyCredential 的实例中。 使用密钥作为凭据参数对客户端进行身份验证:

import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient
endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]

text_analytics_client = TextAnalyticsClient(endpoint, AzureKeyCredential(key))

使用 Azure Active Directory 凭据创建 TextAnalyticsClient

若要使用 Azure Active Directory (AAD) 令牌凭据,请提供从 azure 标识 库获取的所需凭据类型的实例。 请注意,区域终结点不支持 AAD 身份验证。 为资源 创建自定义子域名 ,以便使用此类型的身份验证。

使用 AAD 进行身份验证需要一些初始设置:

设置后,可以从 azure.identity 中选择要使用的 凭据 类型。 例如, DefaultAzureCredential 可用于对客户端进行身份验证:

将 AAD 应用程序的客户端 ID、租户 ID 和客户端密码的值设置为环境变量:AZURE_CLIENT_ID、AZURE_TENANT_ID、AZURE_CLIENT_SECRET

使用返回的令牌凭据对客户端进行身份验证:

import os
from azure.ai.textanalytics import TextAnalyticsClient
from azure.identity import DefaultAzureCredential

endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
credential = DefaultAzureCredential()

text_analytics_client = TextAnalyticsClient(endpoint, credential=credential)

关键概念

TextAnalyticsClient

文本分析客户端库提供 TextAnalyticsClient,用于对批处理进行分析。 它提供同步和异步操作来访问特定使用的文本分析,例如语言检测或关键短语提取。

输入

文档是语言服务中的预测模型要分析的单个单元。 每个操作的输入作为文档 列表 传递。

每个文档都可以作为列表中的字符串传递,例如

documents = ["I hated the movie. It was so slow!", "The movie made it into my top ten favorites. What a great movie!"]

或者,如果要传入每个项的文档idlanguagecountry_hint/,则可以将其作为 DetectLanguageInputTextDocumentInput 的列表或对象的类似于 dict 的表示形式传递:

documents = [
    {"id": "1", "language": "en", "text": "I hated the movie. It was so slow!"},
    {"id": "2", "language": "en", "text": "The movie made it into my top ten favorites. What a great movie!"},
]

请参阅输入 的服务限制 ,包括文档长度限制、最大批大小和支持的文本编码。

返回值

单个文档的返回值可以是结果或错误对象。 从每个操作返回包含结果和错误对象集合的异类列表。 这些结果/错误与所提供的文档的顺序进行索引匹配。

结果(如 AnalyzeSentimentResult)是文本分析操作的结果,包含有关文档输入的预测。

错误对象 DocumentError 指示服务在处理文档时遇到问题,并包含失败的原因。

文档错误处理

可以使用 属性筛选列表中 is_error 的结果或错误对象。 对于结果对象,它始终 False 为 ,对于 DocumentError ,这是 True

例如,若要筛选出所有 DocumentErrors,可以使用列表理解:

response = text_analytics_client.analyze_sentiment(documents)
successful_responses = [doc for doc in response if not doc.is_error]

还可以使用 kind 属性在结果类型之间进行筛选:

poller = text_analytics_client.begin_analyze_actions(documents, actions)
response = poller.result()
for result in response:
    if result.kind == "SentimentAnalysis":
        print(f"Sentiment is {result.sentiment}")
    elif result.kind == "KeyPhraseExtraction":
        print(f"Key phrases: {result.key_phrases}")
    elif result.is_error is True:
        print(f"Document error: {result.code}, {result.message}")

Long-Running操作

长时间运行的操作包括发送到服务以启动操作的初始请求,然后每隔一段时间轮询服务以确定操作是否已完成或失败,以及是否成功获取结果。

支持医疗保健分析、自定义文本分析或多个分析的方法将建模为长时间运行的操作。 客户端公开返回 begin_<method-name> 轮询器对象的方法。 调用方应通过对从 begin_<method-name> 方法返回的轮询器对象调用 result() 来等待操作完成。 提供了示例代码片段来说明如何使用长时间运行的操作

示例

以下部分提供了几个代码片段,涵盖了一些最常见的语言服务任务,包括:

分析情绪

analyze_sentiment 查看其输入文本,并确定其情绪是积极、消极、中性还是混合。 它的响应包括每句情绪分析和置信度分数。

import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient

endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]

text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key))

documents = [
    """I had the best day of my life. I decided to go sky-diving and it made me appreciate my whole life so much more.
    I developed a deep-connection with my instructor as well, and I feel as if I've made a life-long friend in her.""",
    """This was a waste of my time. All of the views on this drop are extremely boring, all I saw was grass. 0/10 would
    not recommend to any divers, even first timers.""",
    """This was pretty good! The sights were ok, and I had fun with my instructors! Can't complain too much about my experience""",
    """I only have one word for my experience: WOW!!! I can't believe I have had such a wonderful skydiving company right
    in my backyard this whole time! I will definitely be a repeat customer, and I want to take my grandmother skydiving too,
    I know she'll love it!"""
]


result = text_analytics_client.analyze_sentiment(documents, show_opinion_mining=True)
docs = [doc for doc in result if not doc.is_error]

print("Let's visualize the sentiment of each of these documents")
for idx, doc in enumerate(docs):
    print(f"Document text: {documents[idx]}")
    print(f"Overall sentiment: {doc.sentiment}")

返回的响应是结果和错误对象的异类列表:list[AnalyzeSentimentResultDocumentError]

有关 情绪分析的概念性讨论,请参阅服务文档。 若要了解如何对文本中的产品或服务的属性 ((如产品或服务的属性) )的相关观点进行更精细的分析,请参阅 此处

识别实体

recognize_entities 在其输入文本中识别实体并将其分类为人员、地点、组织、日期/时间、数量、百分比、货币等。

import os
import typing
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient

endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]

text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key))
reviews = [
    """I work for Foo Company, and we hired Contoso for our annual founding ceremony. The food
    was amazing and we all can't say enough good words about the quality and the level of service.""",
    """We at the Foo Company re-hired Contoso after all of our past successes with the company.
    Though the food was still great, I feel there has been a quality drop since their last time
    catering for us. Is anyone else running into the same problem?""",
    """Bar Company is over the moon about the service we received from Contoso, the best sliders ever!!!!"""
]

result = text_analytics_client.recognize_entities(reviews)
result = [review for review in result if not review.is_error]
organization_to_reviews: typing.Dict[str, typing.List[str]] = {}

for idx, review in enumerate(result):
    for entity in review.entities:
        print(f"Entity '{entity.text}' has category '{entity.category}'")
        if entity.category == 'Organization':
            organization_to_reviews.setdefault(entity.text, [])
            organization_to_reviews[entity.text].append(reviews[idx])

for organization, reviews in organization_to_reviews.items():
    print(
        "\n\nOrganization '{}' has left us the following review(s): {}".format(
            organization, "\n\n".join(reviews)
        )
    )

返回的响应是结果和错误对象的异类列表:list[RecognizeEntitiesResultDocumentError]

有关 命名实体识别 和支持 的类型的概念性讨论,请参阅服务文档。

识别链接实体

例如,recognize_linked_entities 识别并消除在其输入文本中找到的每个实体的身份 (,确定“火星”一词的出现指的是行星,还是罗马战神) 。 识别的实体与已知知识库(如维基百科)的 URL 相关联。

import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient

endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]

text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key))
documents = [
    """
    Microsoft was founded by Bill Gates with some friends he met at Harvard. One of his friends,
    Steve Ballmer, eventually became CEO after Bill Gates as well. Steve Ballmer eventually stepped
    down as CEO of Microsoft, and was succeeded by Satya Nadella.
    Microsoft originally moved its headquarters to Bellevue, Washington in January 1979, but is now
    headquartered in Redmond.
    """
]

result = text_analytics_client.recognize_linked_entities(documents)
docs = [doc for doc in result if not doc.is_error]

print(
    "Let's map each entity to it's Wikipedia article. I also want to see how many times each "
    "entity is mentioned in a document\n\n"
)
entity_to_url = {}
for doc in docs:
    for entity in doc.entities:
        print("Entity '{}' has been mentioned '{}' time(s)".format(
            entity.name, len(entity.matches)
        ))
        if entity.data_source == "Wikipedia":
            entity_to_url[entity.name] = entity.url

返回的响应是结果和错误对象的异类列表:list[RecognizeLinkedEntitiesResultDocumentError]

有关 实体链接 和支持 的类型的概念性讨论,请参阅服务文档。

识别 PII 实体

recognize_pii_entities识别个人身份信息并将其分类, (PII 在其输入文本中) 实体,例如社会安全号码、银行帐户信息、信用卡号码等。

import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient

endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]

text_analytics_client = TextAnalyticsClient(
    endpoint=endpoint, credential=AzureKeyCredential(key)
)
documents = [
    """Parker Doe has repaid all of their loans as of 2020-04-25.
    Their SSN is 859-98-0987. To contact them, use their phone number
    555-555-5555. They are originally from Brazil and have Brazilian CPF number 998.214.865-68"""
]

result = text_analytics_client.recognize_pii_entities(documents)
docs = [doc for doc in result if not doc.is_error]

print(
    "Let's compare the original document with the documents after redaction. "
    "I also want to comb through all of the entities that got redacted"
)
for idx, doc in enumerate(docs):
    print(f"Document text: {documents[idx]}")
    print(f"Redacted document text: {doc.redacted_text}")
    for entity in doc.entities:
        print("...Entity '{}' with category '{}' got redacted".format(
            entity.text, entity.category
        ))

返回的响应是结果和错误对象的异类列表:list[RecognizePiiEntitiesResultDocumentError]

有关 支持的 PII 实体类型,请参阅服务文档。

注意:识别 PII 实体服务在 API 版本 v3.1 及更新版本中可用。

提取关键短语

extract_key_phrases确定其输入文本中的main谈话点。 例如,对于输入文本“食物很美味,并且有出色的工作人员”,API 返回:“food”和“wonderful staff”。

import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient

endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]

text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key))
articles = [
    """
    Washington, D.C. Autumn in DC is a uniquely beautiful season. The leaves fall from the trees
    in a city chock-full of forests, leaving yellow leaves on the ground and a clearer view of the
    blue sky above...
    """,
    """
    Redmond, WA. In the past few days, Microsoft has decided to further postpone the start date of
    its United States workers, due to the pandemic that rages with no end in sight...
    """,
    """
    Redmond, WA. Employees at Microsoft can be excited about the new coffee shop that will open on campus
    once workers no longer have to work remotely...
    """
]

result = text_analytics_client.extract_key_phrases(articles)
for idx, doc in enumerate(result):
    if not doc.is_error:
        print("Key phrases in article #{}: {}".format(
            idx + 1,
            ", ".join(doc.key_phrases)
        ))

返回的响应是结果和错误对象的异类列表:list[ExtractKeyPhrasesResultDocumentError]

有关 关键短语提取的概念性讨论,请参阅服务文档。

检测语言

detect_language 确定其输入文本的语言,包括预测语言的置信度分数。

import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient

endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]

text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key))
documents = [
    """
    The concierge Paulette was extremely helpful. Sadly when we arrived the elevator was broken, but with Paulette's help we barely noticed this inconvenience.
    She arranged for our baggage to be brought up to our room with no extra charge and gave us a free meal to refurbish all of the calories we lost from
    walking up the stairs :). Can't say enough good things about my experience!
    """,
    """
    最近由于工作压力太大,我们决定去富酒店度假。那儿的温泉实在太舒服了,我跟我丈夫都完全恢复了工作前的青春精神!加油!
    """
]

result = text_analytics_client.detect_language(documents)
reviewed_docs = [doc for doc in result if not doc.is_error]

print("Let's see what language each review is in!")

for idx, doc in enumerate(reviewed_docs):
    print("Review #{} is in '{}', which has ISO639-1 name '{}'\n".format(
        idx, doc.primary_language.name, doc.primary_language.iso6391_name
    ))

返回的响应是结果和错误对象的异类列表:list[DetectLanguageResultDocumentError]

有关语言检测以及语言和区域支持的概念性讨论,请参阅服务文档。

医疗保健实体分析

长时间运行的操作begin_analyze_healthcare_entities 提取在医疗保健域中识别的实体,并识别输入文档中的实体之间的关系,以及各种已知数据库(如 UMLS、CHV、MSH 等)中已知信息源的链接。

import os
import typing
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient, HealthcareEntityRelation

endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]

text_analytics_client = TextAnalyticsClient(
    endpoint=endpoint,
    credential=AzureKeyCredential(key),
)

documents = [
    """
    Patient needs to take 100 mg of ibuprofen, and 3 mg of potassium. Also needs to take
    10 mg of Zocor.
    """,
    """
    Patient needs to take 50 mg of ibuprofen, and 2 mg of Coumadin.
    """
]

poller = text_analytics_client.begin_analyze_healthcare_entities(documents)
result = poller.result()

docs = [doc for doc in result if not doc.is_error]

print("Let's first visualize the outputted healthcare result:")
for doc in docs:
    for entity in doc.entities:
        print(f"Entity: {entity.text}")
        print(f"...Normalized Text: {entity.normalized_text}")
        print(f"...Category: {entity.category}")
        print(f"...Subcategory: {entity.subcategory}")
        print(f"...Offset: {entity.offset}")
        print(f"...Confidence score: {entity.confidence_score}")
        if entity.data_sources is not None:
            print("...Data Sources:")
            for data_source in entity.data_sources:
                print(f"......Entity ID: {data_source.entity_id}")
                print(f"......Name: {data_source.name}")
        if entity.assertion is not None:
            print("...Assertion:")
            print(f"......Conditionality: {entity.assertion.conditionality}")
            print(f"......Certainty: {entity.assertion.certainty}")
            print(f"......Association: {entity.assertion.association}")
    for relation in doc.entity_relations:
        print(f"Relation of type: {relation.relation_type} has the following roles")
        for role in relation.roles:
            print(f"...Role '{role.name}' with entity '{role.entity.text}'")
    print("------------------------------------------")

print("Now, let's get all of medication dosage relations from the documents")
dosage_of_medication_relations = [
    entity_relation
    for doc in docs
    for entity_relation in doc.entity_relations if entity_relation.relation_type == HealthcareEntityRelation.DOSAGE_OF_MEDICATION
]

注意:医疗保健实体分析仅适用于 API v3.1 及更新版本。

多重分析

长时间运行的操作begin_analyze_actions 在单个请求中对一组文档执行多个分析。 目前,在单个请求中支持使用以下语言 API 的任意组合:

  • 实体识别
  • PII 实体识别
  • 链接实体识别
  • 关键短语提取
  • 情绪分析
  • 自定义实体识别 (API 版本 2022-05-01 及更新)
  • 自定义单标签分类 (API 版本 2022-05-01 及更新)
  • 自定义多标签分类 (API 版本 2022-05-01 及更新)
  • 医疗保健实体分析 (API 版本 2022-05-01 及更新)
  • 提取式摘要 (API 版本 2023-04-01 及更新)
  • 抽象摘要 (API 版本 2023-04-01 及更新)
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import (
    TextAnalyticsClient,
    RecognizeEntitiesAction,
    RecognizeLinkedEntitiesAction,
    RecognizePiiEntitiesAction,
    ExtractKeyPhrasesAction,
    AnalyzeSentimentAction,
)

endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]
key = os.environ["AZURE_LANGUAGE_KEY"]

text_analytics_client = TextAnalyticsClient(
    endpoint=endpoint,
    credential=AzureKeyCredential(key),
)

documents = [
    'We went to Contoso Steakhouse located at midtown NYC last week for a dinner party, and we adore the spot! '
    'They provide marvelous food and they have a great menu. The chief cook happens to be the owner (I think his name is John Doe) '
    'and he is super nice, coming out of the kitchen and greeted us all.'
    ,

    'We enjoyed very much dining in the place! '
    'The Sirloin steak I ordered was tender and juicy, and the place was impeccably clean. You can even pre-order from their '
    'online menu at www.contososteakhouse.com, call 312-555-0176 or send email to order@contososteakhouse.com! '
    'The only complaint I have is the food didn\'t come fast enough. Overall I highly recommend it!'
]

poller = text_analytics_client.begin_analyze_actions(
    documents,
    display_name="Sample Text Analysis",
    actions=[
        RecognizeEntitiesAction(),
        RecognizePiiEntitiesAction(),
        ExtractKeyPhrasesAction(),
        RecognizeLinkedEntitiesAction(),
        AnalyzeSentimentAction(),
    ],
)

document_results = poller.result()
for doc, action_results in zip(documents, document_results):
    print(f"\nDocument text: {doc}")
    for result in action_results:
        if result.kind == "EntityRecognition":
            print("...Results of Recognize Entities Action:")
            for entity in result.entities:
                print(f"......Entity: {entity.text}")
                print(f".........Category: {entity.category}")
                print(f".........Confidence Score: {entity.confidence_score}")
                print(f".........Offset: {entity.offset}")

        elif result.kind == "PiiEntityRecognition":
            print("...Results of Recognize PII Entities action:")
            for pii_entity in result.entities:
                print(f"......Entity: {pii_entity.text}")
                print(f".........Category: {pii_entity.category}")
                print(f".........Confidence Score: {pii_entity.confidence_score}")

        elif result.kind == "KeyPhraseExtraction":
            print("...Results of Extract Key Phrases action:")
            print(f"......Key Phrases: {result.key_phrases}")

        elif result.kind == "EntityLinking":
            print("...Results of Recognize Linked Entities action:")
            for linked_entity in result.entities:
                print(f"......Entity name: {linked_entity.name}")
                print(f".........Data source: {linked_entity.data_source}")
                print(f".........Data source language: {linked_entity.language}")
                print(
                    f".........Data source entity ID: {linked_entity.data_source_entity_id}"
                )
                print(f".........Data source URL: {linked_entity.url}")
                print(".........Document matches:")
                for match in linked_entity.matches:
                    print(f"............Match text: {match.text}")
                    print(f"............Confidence Score: {match.confidence_score}")
                    print(f"............Offset: {match.offset}")
                    print(f"............Length: {match.length}")

        elif result.kind == "SentimentAnalysis":
            print("...Results of Analyze Sentiment action:")
            print(f"......Overall sentiment: {result.sentiment}")
            print(
                f"......Scores: positive={result.confidence_scores.positive}; \
                neutral={result.confidence_scores.neutral}; \
                negative={result.confidence_scores.negative} \n"
            )

        elif result.is_error is True:
            print(
                f"...Is an error with code '{result.error.code}' and message '{result.error.message}'"
            )

    print("------------------------------------------")

返回的响应是封装多个迭代的对象,每个迭代对象表示单个分析的结果。

注意:API v3.1 及更新版本中提供了多个分析。

可选配置

可选关键字 (keyword) 参数可以在客户端和按操作级别传入。 azure-core 参考文档 介绍了重试、日志记录、传输协议等的可用配置。

疑难解答

常规

文本分析客户端将引发 Azure Core 中定义的异常。

日志记录

此库使用标准 日志记录 库进行日志记录。 有关 HTTP 会话 (URL、标头等的基本信息,) 在 INFO 级别记录。

在客户端上使用 logging_enable 关键字参数可启用详细的调试级别日志记录(包括请求/响应正文和未编辑的标头):

import sys
import logging
from azure.identity import DefaultAzureCredential
from azure.ai.textanalytics import TextAnalyticsClient

# Create a logger for the 'azure' SDK
logger = logging.getLogger('azure')
logger.setLevel(logging.DEBUG)

# Configure a console output
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)

endpoint = "https://<resource-name>.cognitiveservices.azure.com/"
credential = DefaultAzureCredential()

# This client will log detailed information about its HTTP sessions, at DEBUG level
text_analytics_client = TextAnalyticsClient(endpoint, credential, logging_enable=True)
result = text_analytics_client.analyze_sentiment(["I did not like the restaurant. The food was too spicy."])

同样,即使没有为客户端启用详细日志记录,logging_enable 也可以为单个操作启用:

result = text_analytics_client.analyze_sentiment(documents, logging_enable=True)

后续步骤

更多示例代码

这些代码示例演示 Azure 文本分析 客户端库的常见方案操作。

使用认知服务/语言服务 API 密钥或 azure-identity 中的令牌凭据对客户端进行身份验证:

常见方案

高级方案

其他文档

有关适用于语言的 Azure 认知服务的广泛文档,请参阅有关 docs.microsoft.com 的语言服务文档

贡献

本项目欢迎贡献和建议。 大多数贡献要求你同意贡献者许可协议 (CLA),并声明你有权(并且确实有权)授予我们使用你的贡献的权利。 有关详细信息,请访问 cla.microsoft.com

提交拉取请求时,CLA 机器人将自动确定你是否需要提供 CLA,并相应地修饰 PR(例如标签、注释)。 直接按机器人提供的说明操作。 只需使用 CLA 对所有存储库执行一次这样的操作。

此项目采用了 Microsoft 开放源代码行为准则。 有关详细信息,请参阅行为准则常见问题解答,或如果有任何其他问题或意见,请与 联系。