在 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 中建置應用程式還不熟悉,建議您在深入本文前先閱讀從頭建立應用程式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 的連線時,會使用此金鑰。You use this key later when you create a connection to the API.

    API 金鑰

建置應用程式Build the app

現在您已啟動並執行文字分析 API,而可從 PowerApps 加以連線,並建置呼叫 API 的應用程式。Now that you have the Text Analytics API up and running, you 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, you 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. web.powerapps.com 中,選擇 [開始從空白] > [手機應用程式圖示] (手機) > [製作此應用程式]。In web.powerapps.com, choose Start from blank > Phone app icon (phone) > Make this app.

    從空白開始

  2. 在 PowerApps Studio 的中間窗格中,選擇 [連接到資料]。In the middle pane of the PowerApps Studio, choose connect to data.

  3. 在 [資料] 面板上,按一下或點選 [新增連接] > [文字分析]。On the Data panel, click or tap New connection > Text Analytics.

  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 you can enter text to analyze. 將控制項命名為 tiTextToAnalyzeName the control tiTextToAnalyze. 應用程式現在看起來應該類似下列映像。The app should now look like the following image.

    應用程式與標題、子標題和文字輸入

  5. 新增三個核取方塊控制項,以選擇要執行哪些 API 作業。Add three Check box controls, so you can choose which API operations to perform. 將控制項命名為 chkLanguagechkPhraseschkSentimentName the controls chkLanguage, chkPhrases, and chkSentiment.

  6. 新增按鈕,以在選取要執行的作業後呼叫 API。Add a button, so you 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 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 holds 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 (you don't need this screen for the 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, you have a nice-looking app, but it doesn't do anything yet. 這個問題很快就能解決。You'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 you click or tap Analyze text, the app makes 1, 2, or 3 API calls.

  2. 應用程式會將 API 傳回的資料儲存在三個不同的集合中:languageCollectsentimentCollectphrasesCollectThe app stores data that the API returns in three different collections: languageCollect, sentimentCollect, and phrasesCollect.

  3. 應用程式會根據三個集合的內容,更新其中兩個標籤的 Text 屬性和資源庫的 Items 屬性。The app updates the Text property for two of the labels, and the Items property for the gallery, based on what's in the three collections.

有了此背景,我們將為按鈕的 OnSelect 屬性新增公式。With that background, let's add the formula for the OnSelect property of the 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, you specify tiTextToAnalyze.Text as the input text.

    • DetectLanguage() 中,numberOfLanguagesToDetect 硬式編碼為 1,但您可以根據應用程式中的某些邏輯來傳遞此參數。In DetectLanguage(), numberOfLanguagesToDetect is hard-coded as 1, but you could pass this parameter based on some logic in the app.

    • KeyPhrases()DetectSentiment() 中,語言硬式編碼為 "en",但您可以根據應用程式中的某些邏輯來傳遞此參數。In KeyPhrases() and DetectSentiment(),language is hard-coded as "en", but you could pass this parameter based on some logic in the app. 例如,您可以先偵測語言,然後根據 DetectLanguage() 傳回的內容設定此參數。For example, you 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, add the name of the language that was identified in the text.

    • 針對 phrasesCollect,新增在文字中識別出來的關鍵字詞For phrasesCollect, add the keyPhrases that were identified in the text.

    • 針對 sentimentCollect,新增文字的情感分數,這是 0-1 的值,1 代表 100% 正面。For sentimentCollect, 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 the app displays 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() 函式擷取單一值。You're now working with a gallery so you don't need the First() function to extract a single value. 您參考集合,且資源庫會將關鍵字詞顯示為清單。You reference the collection, and the gallery displays the key phrases as a list.

執行應用程式Run the app

應用程式已完成,現在請加以執行,以查看其運作方式:按一下或點選右上角的 [執行] 按鈕Now that the app is finished, run it to see how it works: click or tap the run button in the upper right corner 執行應用程式.. 在下圖中,三個選項皆已選取,且文字與文字分析 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 the results are the same.

希望您現在已更加瞭解文字分析 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.