在 PowerApps 中使用认知服务Use Cognitive Services in PowerApps

本文介绍如何生成使用 Microsoft 认知服务文本分析 API 分析文本的基本应用。This article shows you how to build a basic app that uses the Microsoft Cognitive Services Text Analytics API to analyze text. 我们将介绍如何设置文本分析 API,以及如何使用文本分析连接器连接到它。We'll show you how to set up the Text Analytics API, and connect to it with the Text Analytics connector. 随后将介绍如何创建调用此 API 的应用。Then we'll show you how to create an app that calls the API.

注意:如果你是刚刚开始在 PowerApps 中生成应用,建议先阅读从头开始创建应用,然后再深入了解本文。Note: If you are new to building apps in PowerApps, we recommend reading Create an app from scratch before diving into this article.

Microsoft 认知服务简介Introduction to Microsoft Cognitive Services

Microsoft 认知服务是一组可使应用程序更加智能、富有吸引力和易被发现的 API、SDK 和服务。Microsoft Cognitive Services are a set of APIs, SDKs, and services available to make your applications more intelligent, engaging, and discoverable. 借助这些服务,你可以轻松地将智能功能添加到应用程序,这些功能包括表情和视频检测;面部、语音和视觉识别;以及语音和语言理解等。These services enable you to easily add intelligent features – such as emotion and video detection; facial, speech and vision recognition; and speech and language understanding – into your applications.

本文重点介绍如何通过文本分析 API 添加“语言理解”功能。We'll focus on "language understanding" for this article, working with the Text Analytics API. 通过此 API 可以检测文本中的感情、关键词语、主题和语言。This API enables you to detect sentiment, key phrases, topics, and language from your text. 我们先体验一下此 API 的演示,然后再注册预览版。Let's get started by trying out a demo of the API, then signing up for a preview version.

体验文本分析 APITry out the Text Analytics API

此 API 具有在线演示,可以查看它的工作方式,并查看服务返回的 JSON。The API has an online demo – you can see how it works, and look at the JSON that the service returns.

  1. 转到文本分析 API 页。Go to the Text Analytics API page.
  2. 在“查看它的实际操作”部分中,使用示例文本或输入自己的文本。In the See it in action section, use the example text, or enter your own text. 然后单击或点击“分析”。Then click or tap Analyze.

    文本分析 API 演示

  3. 此页将显示“已分析的文本”选项卡上的格式化结果和“JSON”选项卡上的 JSON 响应。JSON 是一种表示数据的方法;在本示例中,是文本分析 API 返回的数据。The page shows formatted results on the Analyzed text tab, and the JSON response on the JSON tab. JSON is a way to represent data - in this case, data returned by the Text Analytics API.

注册文本分析 APISign up for the Text Analytics API

可免费获取此 API 的预览版,并且它与 Azure 订阅相关联。The API is available as a free preview, and it is associated with an Azure subscription. 可通过 Azure 门户管理此 API。You manage the API through the Azure portal.

  1. 如果还没有 Azure 订阅,可以注册免费订阅If you don't already have an Azure subscription, sign up for a free subscription.
  2. 登录你的 Azure 帐户。Sign in to your Azure account.
  3. 转到 Azure 门户中的“创建认知服务”边栏选项卡Go to the Create Cognitive Services blade in the Azure portal.
  4. 输入文本分析 API 的相关信息,如下图所示。Enter information for the Text Analytics API, like in the following image. 选择“F0”(免费)定价层。Select the F0 (free) pricing tier.

    创建文本分析 API

  5. 在左下角单击或点击“创建”。In the lower-left corner, click or tap Create.
  6. 在“仪表板”上单击或点击刚刚创建的 API。On the Dashboard, click or tap the API that you just created.

    Azure 仪表板

  7. 单击或点击“密钥”。Click or tap Keys.

    Azure 菜单

  8. 复制屏幕右侧的任一密钥。Copy one of the keys on the right of the screen. 后面与此 API 建立连接时将使用此密钥。We will use this key later when we create a connection to the API.

    API 密钥

生成应用Build the app

现在我们已启动并运行文本分析 API,可以从 PowerApps 连接到它,并生成可调用此 API 的应用。Now that we have the Text Analytics API up and running, we can connect to it from PowerApps, and build an app that calls the API. 这是单屏幕应用,它提供的功能与文本分析 API 页上的演示类似。This is a single screen app that provides functionality similar to the demo on the Text Analytics API page. 我们来开始生成应用!Let's get started on building this!

创建应用并添加连接Create the app and add a connection

首先,我们创建一个空白的手机应用,并添加与文本分析连接器的连接。First, we'll create a blank phone app and add a connection with the Text Analytics connector. 如果需要关于这些任务的详细信息,请参阅从头开始创建应用管理 PowerApps 中的连接If you need more information about these tasks, see Create an app from scratch and Manage your connections in PowerApps.

  1. 在 PowerApps Studio 中,依次单击或点击“文件” > “新建”和“空白应用”下的“手机布局”。In PowerApps Studio, click or tap File > New, then under Blank app, click or tap Phone layout.

    空白应用 -> 手机布局

  2. 在中间窗格中,单击或点击“连接到数据”。In the middle pane, click or tap connect to data.
  3. 在右侧窗格中,单击或点击“新建连接” > “文本分析”。In the right pane, click or tap New connection > Text Analytics.

    PowerApps 数据源

  4. 将密钥复制到“帐户密钥”中,然后单击或点击“创建”。Copy your key into Account Key, then click or tap Create.

    文本分析连接器

向应用添加控件Add controls to the app

创建此应用的下一步是添加所有控件。The next step in creating the app is to add all the controls. 通常在生成应用时,我会在此过程中向控件添加公式,但是在本示例中,我们将首先以控件为主,在下一节中再添加一些公式。Normally when I build apps, I add formulas to the controls as I go, but in this case we'll focus on the controls first, then add a few formulas in the next section. 下图演示了带有所有控件的应用。The following image shows the app with all the controls.

完成的应用

按照下列步骤创建此屏幕。Follow the steps below to create this screen. 如果已指定了控件名称,则在下一节的公式中使用该名称。If a control name is specified, that name is used in a formula in the next section.

  1. 在“开始”选项卡上,依次单击或点击“新建屏幕”和“可滚动屏幕”。On the Home tab, click or tap New Screen, then Scrollable screen.
  2. 在“屏幕2”上,选择“[标题]”并将其更改为“文本分析”。On Screen2, select [Title] and change it to Text Analysis.
  3. 添加“标签”控件以添加介绍性文本。Add a Label control for the introductory text.
  4. 添加“文本输入”控件,以便可以输入要分析的文本。Add a Text input control, so we can enter text to analyze. 将控件命名为 tiTextToAnalyze。Name the control tiTextToAnalyze. 此时,应用应如下图所示。The app should now look like the following image.

    带有标题、副标题和文本输入的应用

  5. 添加三个“复选框”控件,方便选择要执行的 API 操作。Add three Check box controls, so we can choose which API operations to perform. 将三个控件分别命名为 chkLanguage、chkPhrases、和 chkSentiment。Name the controls chkLanguage, chkPhrases, and chkSentiment.
  6. 添加一个按钮,以便在选择要执行的操作后可以调用此 API。Add a button, so we can call the API after selecting which operations to perform. 此时,应用应如下图所示。The app should now look like the following image.

    带有复选框和按钮的应用

  7. 添加三个“标签”控件。Add three Label controls. 前两个控件保存语言和感情 API 调用的结果;第三个控件只是对屏幕底部的库的介绍。The first two will hold results from the language and sentiment API calls; the third is just an introduction for the gallery at the bottom of the screen.
  8. 添加“空白垂直库”控件,然后向此库添加“标签”控件。Add a Blank vertical gallery control, then add a Label control to the gallery. 此库将保存关键词语 API 调用的结果。The gallery will hold results from the key phrases API call. 此时,应用应如下图所示。The app should now look like the following image.

    带有标签和库的应用

  9. 在左侧窗格中,选择“屏幕1”>“省略号(. . .)”>“删除”(此应用无需此屏幕)。In the left pane, select Screen1 > ellipsis (. . .) > Delete (we don't need this screen for our app).

我们将保持此应用的简单性,以便重点关注调用文本分析 API,但是你可以添加内容,例如添加根据所选中的复选框显示或隐藏控件的逻辑,用户未选择任何选项时的错误处理功能等。We're keeping this app simple to focus on calling the Text Analytics API, but you could add things, like logic to show and hide controls based on the check boxes selected, error handling if the user doesn't select any options, and so on.

添加逻辑以进行正确的 API 调用Add logic to make the right API calls

好了,我们拥有了一款美观的应用,但是它还不能执行任何操作。OK, we have a nice-looking app, but it doesn't do anything yet. 稍后我们将解决此问题。We'll fix that in a moment. 在深入了解详细信息前,我们先了解一下此应用遵循的模式:But before we dive into the details, let's understand the pattern that the app follows:

  1. 此应用根据在应用中选中的复选框进行特定 API 调用。The app makes specific API calls based on the check boxes selected in the app. 当我们单击或点击“分析文本”时,应用将进行 1 个、2 个或 3 个 API 调用。When we click or tap Analyze text, the app makes 1, 2, or 3 API calls.
  2. API 返回的数据存储在三个不同的集合中:分别是 languageCollect、sentimentCollect、和 phrasesCollect。Data that the API returns is stored in three different collections: languageCollect, sentimentCollect, and phrasesCollect.
  3. 两个标签的 Text 属性和库的 Items 属性根据三个集合中的内容进行更新。The Text property for two of the labels, and the Items property for the gallery, are updated based on what's in the three collections.

下面,我们来添加按钮的 OnSelect 属性的公式。With that background, let's add the formula for the OnSelect property of our button. 这里将会发生奇迹。This is where all the magic happens.

If(chkLanguage.Value=true,

        ClearCollect(languageCollect, TextAnalytics.DetectLanguage({numberOfLanguagesToDetect:1, text:tiTextToAnalyze.Text}).detectedLanguages.name)

);

If(chkPhrases.Value=true,

        ClearCollect(phrasesCollect, TextAnalytics.KeyPhrases({language:"en", text:tiTextToAnalyze.Text}).keyPhrases)

);

If(chkSentiment.Value=true,

        ClearCollect(sentimentCollect, TextAnalytics.DetectSentiment({language:"en", text:tiTextToAnalyze.Text}).score)

)

过程有些复杂,我们将分开介绍:There's a bit going on here, so let's break it down:

  • If 语句非常简单 – 如果选中了特定复选框,则针对该操作执行 API 调用。The If statements are straightforward – if a specific check box is selected, make the API call for that operation.
  • 在每个调用中,指定相应的参数:Within each call, specify the appropriate parameters:
    • 在全部三个调用中,均将 tiTextToAnalyze.Text 指定为输入文本。In all three calls, we specify tiTextToAnalyze.Text as the input text.
    • 在 DetectLanguage() 中,将 numberOfLanguagesToDetect 硬编码为 1,不过,也可以根据应用中的一些逻辑传递此参数。In DetectLanguage(), we hard-code numberOfLanguagesToDetect as 1, but we could pass this parameter based on some logic in the app.
    • 在 KeyPhrases() 和 DetectSentiment() 中,将语言硬编码为“en”,也可以根据应用中的一些逻辑传递此参数。In KeyPhrases() and DetectSentiment(), we hard-code language as "en", but we could pass this parameter based on some logic in the app. 例如,我们可以先检测语言,然后根据 DetectLanguage() 返回的内容设置此参数。For example, we could detect the language first, then set this parameter based on what DetectLanguage() returns.
  • 针对执行的各个调用,将结果添加到相应的集合:For each call that is made, add the results to the appropriate collection:
    • 在 languageCollect 中,添加在文本中识别的语言的名称。For languageCollect, we add the name of the language that was identified in the text.
    • 在 phrasesCollect 中,添加在文本中识别的 keyPhrases。For phrasesCollect, we add the keyPhrases that were identified in the text.
    • 在 sentimentCollect 中,添加文本的感情分数,这是 0 到 1 之间的值,1 为 100% 积极。For sentimentCollect, we add the sentiment score for the text, which is a value of 0-1, with 1 being 100% positive.

显示 API 调用的结果Display the results of the API calls

若要显示 API 调用的结果,在各个控件中引用相应的集合:To display the results of the API calls, reference the appropriate collection in each control:

  1. 将语言标签的 Text 属性设置为:"The language detected is " & First(languageCollect).nameSet the Text property of the language label to: "The language detected is " & First(languageCollect).name.

    First() 函数返回 languageCollect 中的第一个(仅适用于本示例)记录,我们显示与该记录关联的名称(唯一的字段)。The First() function returns the first (and in this case only) record in languageCollect, and we display the name (the only field) associated with that record.

  2. 将感情标签的 Text 属性设置为:"The sentiment score is " & Round(First(sentimentCollect.Value).Value, 3)\*100 & "% positive."Set the Text property of the sentiment label to: "The sentiment score is " & Round(First(sentimentCollect.Value).Value, 3)\*100 & "% positive.".

    此公式也使用 First() 函数,并获取第一个(也是唯一的一个)记录的值 (0-1),然后将其设置为百分比格式。This formula also uses the First() function, gets the Value (0-1) from the first and only record, then formats it as a percentage.

  3. 将关键词语库的 Items 属性设置为:phrasesCollectSet the Items property of the key phrases gallery to: phrasesCollect.

    我们现在正在使用一个库,因此无需使用 First() 函数来提取单个值。We're now working with a gallery so we don't need the First() function to extract a single value. 我们引用此集合,此库会将关键词语显示为一个列表。We reference the collection, and the gallery displays the key phrases as a list.

运行应用Run the app

至此,应用已创建完成。我们来运行应用,看看它能否正常运行。Now that the app is finished, let's run it to see how it works. 在下图中,已将三个选项全部选中,并且文本与文本分析 API 页上的默认文本相同。In the following image, all three options are selected, and the text is the same as the default text on the Text Analytics API page.

已完成且带有数据的应用

如果将此应用的输出与本文开头部分的文本分析 API 页进行比较,可以看到结果相同。If you compare the output of this app to the Text Analytics API page at the beginning of this article, you see that we get the same results.

希望现在你已加深了对文本分析 API 的了解,并且熟悉如何将其融入应用。We hope you now understand a little more about the Text Analytics API, and you've enjoyed seeing how to incorporate it into an app. 如果你希望我们的文章重点介绍其他认知服务(或其他通用服务),请告知我们。Let us know if there are other Cognitive Services (or other services in general) that you would like us to focus on in our articles. 与往常一样,请在评论中留下反馈及任何问题。As always, please leave feedback and any questions in the comments.