Lab setup instructions and requirements
Protect your account and progress. Always use a private browser window and lab credentials to run this lab.

使用 Google API 分析語音和語言:挑戰實驗室

Lab 10 minutes universal_currency_alt 1 Credit show_chart Introductory
info This lab may incorporate AI tools to support your learning.
This content is not yet optimized for mobile devices.
For the best experience, please visit us on a desktop computer using a link sent by email.

ARC114

Google Cloud 自學實驗室標誌

總覽

在挑戰研究室中,您會在特定情境下完成一系列任務。挑戰研究室不會提供逐步說明,您將運用從課程研究室學到的技巧,自行找出方法完成任務!自動評分系統 (如本頁所示) 將根據您是否正確完成任務來提供意見回饋。

在您完成任務的期間,挑戰研究室不會介紹新的 Google Cloud 概念。您須靈活運用所學技巧,例如變更預設值或詳讀並研究錯誤訊息,解決遇到的問題。

若想滿分達標,就必須在時限內成功完成所有任務!

設定和需求

瞭解以下事項後,再點選「Start Lab」按鈕

請詳閱以下操作說明。實驗室活動會計時,且中途無法暫停。點選「Start Lab」後就會開始計時,顯示可使用 Google Cloud 資源的時間。

您將在真正的雲端環境完成實作實驗室活動,而不是模擬或示範環境。為此,我們會提供新的暫時憑證,供您在實驗室活動期間登入及存取 Google Cloud。

為了順利完成這個實驗室,請先確認:

  • 可以使用標準的網際網路瀏覽器 (Chrome 瀏覽器為佳)。
注意事項:請使用無痕模式 (建議選項) 或私密瀏覽視窗執行此實驗室,這可以防止個人帳戶和學員帳戶之間的衝突,避免個人帳戶產生額外費用。
  • 是時候完成實驗室活動了!別忘了,活動一旦開始將無法暫停。
注意事項:務必使用實驗室專用的學員帳戶。如果使用其他 Google Cloud 帳戶,可能會產生額外費用。

挑戰情境

您剛開始擔任初階雲端架構師,公司指派您參與團隊專案,需要使用 Google Cloud 的 Cloud Natural Language API 和 Cloud Speech API 服務。

公司認為您具備完成下列工作所需的技能與知識。

您的挑戰

在這項挑戰中,您需要分別使用 Cloud Natural Language API 和 Cloud Speech API,分析一些文字和語音。您還需要使用 Python 對文字文件執行情緒分析。

您需要完成下列動作:

  • 建立 API 金鑰
  • 建立實體分析要求並呼叫 Natural Language API
  • 建立語音分析要求並呼叫 Speech API
  • 使用 Natural Language API 分析情緒

在本挑戰實驗室,我們已設定好虛擬機器 (VM) 執行個體 ,供您完成工作。

各項工作的詳細說明如下,祝您好運!

工作 1:建立 API 金鑰

  1. 您需要在這項工作中建立 API 金鑰,以便在傳送要求至 Natural Language API 時使用。

  2. 請儲存 API 金鑰,方便在其他工作中使用。

點選「Check my progress」,確認目標已達成。 建立 API 金鑰

工作 2:建立實體分析要求並呼叫 Natural Language API

  1. 在這項工作中,請透過 SSH 連至為您佈建的執行個體

  2. 接著建立名為 nl_request.json 的 JSON 檔案,並將其傳送至 Natural Language API 進行分析。您可以將下列程式碼新增至 JSON 檔案,分析有關波士頓市的文字,也可以將您選擇的文字新增至 content 物件,對該文字執行實體分析。

{ "document":{ "type":"PLAIN_TEXT", "content":"With approximately 8.2 million people residing in Boston, the capital city of Massachusetts is one of the largest in the United States." }, "encodingType":"UTF8" }
  1. 現在能使用 curl 指令,將要求主體和稍早儲存的 API 金鑰環境變數傳送至 Natural Language API,或使用 gcloud 機器學習指令分析文字。

  2. 將回應儲存至名為 nl_response.json 的檔案。

點選「Check my progress」,確認目標已達成。 建立實體分析要求並呼叫 Natural Language API

工作 3:建立語音分析要求並呼叫 Speech API

注意:在這項工作中,您將使用預先錄好的檔案,該檔案可從 Cloud Storage 取得:gs://cloud-samples-tests/speech/brooklyn.flac請先聆聽音訊檔案,再傳送至 Speech API
  1. 為這項工作建立另一個 JSON 檔案,並命名為 speech_request.json,然後使用範例音訊檔案的 URI 值新增內容。
{ "config": { "encoding":"FLAC", "languageCode": "en-US" }, "audio": { "uri":"Pass the API the uri of the audio file in Cloud Storage" } }
  1. 現在能使用 curl 指令,將要求主體和稍早儲存的 API 金鑰環境變數傳送至 Natural Language API,或使用 gcloud 機器學習指令分析語音。

  2. 將回應儲存至名為 speech_response.json 的檔案。

點選「Check my progress」,確認目標已達成。 建立語音分析要求並呼叫 Speech API

工作 4:使用 Natural Language API 分析情緒

這項工作需要使用 Google Cloud Natural Language API 分析文字情緒,判斷內容樣本 (例如電影評論) 的整體態度 (正面或負面)。在 執行個體中,系統已為您建立名為 sentiment_analysis.py 的簡單 Python 應用程式程式碼檔案,並已完成設定。為執行分析,您將使用虛構影評來測試模型,內容評論雷利史考特執導的 1982 年科幻動作片《銀翼殺手》

如要使用 Natural Language API 執行情緒分析,您需要呼叫 LanguageServiceClient 執行個體的 analyze_sentiment 方法,存取這項服務。

  1. 您需要在 sentiment_analysis.py 檔案中編輯 def analyze(movie_review_filename): 方法,並使用 Python 程式碼完成該方法,以執行下列動作:

    • LanguageServiceClient 執行個體例項化為用戶端。
    • 將包含文字資料的檔案名稱讀取為變數。
    • 例項化具備檔案內容的 Document 物件。
    • 呼叫用戶端的 analyze_sentiment 方法。
  2. 從 Google Cloud Storage 下載虛構的電影評論範例:gs://cloud-samples-tests/natural-language/sentiment-samples.tgz

  3. 將範例檔案解壓縮,然後使用相關的 Python 指令,對其中一個檔案 bladerunner-pos.txt 執行情緒分析。

點選「Check my progress」,確認目標已達成。 使用 Natural Language API 分析情緒

恭喜!

「使用 Google API 分析語音和語言」技能徽章

您已成功使用 Natural Language API 和 Speech API,對文字和語音執行實體和情緒分析。

Google Cloud 教育訓練與認證

協助您瞭解如何充分運用 Google Cloud 的技術。我們的課程會介紹專業技能和最佳做法,讓您可以快速掌握要領並持續進修。我們提供從基本到進階等級的訓練課程,並有隨選、線上和虛擬課程等選項,方便您抽空參加。認證可協助您驗證及證明自己在 Google Cloud 技術方面的技能和專業知識。

使用手冊上次更新日期:2023 年 9 月 20 日

實驗室上次測試日期:2023 年 9 月 20 日

Copyright 2026 Google LLC 保留所有權利。Google 和 Google 標誌是 Google LLC 的商標,其他公司和產品名稱則有可能是其關聯公司的商標。

Before you begin

  1. Labs create a Google Cloud project and resources for a fixed time
  2. Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
  3. On the top left of your screen, click Start lab to begin

Use private browsing

  1. Copy the provided Username and Password for the lab
  2. Click Open console in private mode

Sign in to the Console

  1. Sign in using your lab credentials. Using other credentials might cause errors or incur charges.
  2. Accept the terms, and skip the recovery resource page
  3. Don't click End lab unless you've finished the lab or want to restart it, as it will clear your work and remove the project

This content is not currently available

We will notify you via email when it becomes available

Great!

We will contact you via email if it becomes available

One lab at a time

Confirm to end all existing labs and start this one

Use private browsing to run the lab

Using an Incognito or private browser window is the best way to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.