情緒分析教學課程

目標對象

本教學課程的設計在於讓您透過 Google Cloud Natural Language API 快速開始探索及開發應用程式。本教學課程的設計對象為熟悉基本程式設計的人員,但即便您對程式設計只有粗淺的認識,也應能跟得上課程。完成本教學課程後,您應能使用參考資料說明文件建立自己的基本應用程式。

本教學課程採用 Python 程式碼逐步操作 Natural Language API 應用程式。本文目的並非提供 Python 用戶端程式庫的相關說明,而是解釋如何呼叫 Natural Language API。以 Java 和 Node.js 撰寫應用程式的方法大致類似。請參閱 Natural Language API 範例以取得其他語言的範例 (包括本教學課程的範例)。

必備條件

本教學課程有幾項必備條件:

分析文件情緒

本教學課程會透過 analyzeSentiment 要求逐步引導您操作基本的 Natural Language API 應用程式,該應用程式會對文字執行情緒分析。情緒分析會嘗試判斷整體態度 (正面或負面),並以數值 scoremagnitude 值表示。(如要進一步瞭解這些概念,請參閱「自然語言基礎知識」)。

我們一開始會先提供完整的程式碼。(請注意,為表示程式碼其實相當簡潔,我們已從程式碼中移除大部分的註解。我們會在檢閱程式碼時提供更多註解內容)。

如要進一步瞭解如何安裝及使用 Python 適用的 Google Cloud Natural Language 用戶端程式庫,請參閱 Natural Language API 用戶端程式庫
"""Demonstrates how to make a simple call to the Natural Language API."""

import argparse

from google.cloud import language_v1



def print_result(annotations):
    score = annotations.document_sentiment.score
    magnitude = annotations.document_sentiment.magnitude

    for index, sentence in enumerate(annotations.sentences):
        sentence_sentiment = sentence.sentiment.score
        print(f"Sentence {index} has a sentiment score of {sentence_sentiment}")

    print(f"Overall Sentiment: score of {score} with magnitude of {magnitude}")
    return 0




def analyze(movie_review_filename):
    """Run a sentiment analysis request on text within a passed filename."""
    client = language_v1.LanguageServiceClient()

    with open(movie_review_filename) as review_file:
        # Instantiates a plain text document.
        content = review_file.read()

    document = language_v1.Document(
        content=content, type_=language_v1.Document.Type.PLAIN_TEXT
    )
    annotations = client.analyze_sentiment(request={"document": document})

    # Print the results
    print_result(annotations)




if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
    )
    parser.add_argument(
        "movie_review_filename",
        help="The filename of the movie review you'd like to analyze.",
    )
    args = parser.parse_args()

    analyze(args.movie_review_filename)

此簡易應用程式會執行下列工作:

  • 匯入執行應用程式時需要的程式庫
  • 擷取文字檔案並傳遞至 main() 函式
  • 讀取文字檔案並向服務提出要求
  • 剖析服務回應並向使用者顯示

以下將深入探討這些步驟。

匯入程式庫

如要進一步瞭解如何安裝及使用 Python 適用的 Google Cloud Natural Language 用戶端程式庫,請參閱 Natural Language API 用戶端程式庫
import argparse

from google.cloud import language_v1

我們匯入標準程式庫 argparse,以允許應用程式接受輸入檔案名稱做為引數。

為了使用 Cloud Natural Language API,我們還想從 google-cloud-language 程式庫匯入 language 模組。types 模組包含建立要求所需的類別。

執行應用程式

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
    )
    parser.add_argument(
        "movie_review_filename",
        help="The filename of the movie review you'd like to analyze.",
    )
    args = parser.parse_args()

    analyze(args.movie_review_filename)

我們在此只針對文字檔案名稱剖析傳遞的引數,並將該引數傳遞至 analyze() 函式。

向 API 進行驗證

在跟 Natural Language API 服務進行通訊前,您需要使用先前取得的憑證驗證您的服務。要在應用程式中取得憑證,最簡單的方式是使用應用程式預設憑證 (ADC)。根據預設,ADC 會嘗試從 GOOGLE_APPLICATION_CREDENTIALS 環境檔案取得憑證,憑證應設定為指向服務帳戶的 JSON 金鑰檔案。(您應該已在快速入門中設定使用 ADC 的服務帳戶與環境。詳情請參閱「設定服務帳戶」一文。

適用於 Python 的 Google Cloud 用戶端程式庫會自動使用應用程式預設憑證。

建立要求

在 Natural Language API 服務準備就緒後,我們可以呼叫 LanguageServiceClient 例項的 analyze_sentiment 方法來存取這項服務。

用戶端程式庫會封裝 API 要求與回應的詳細資料。如要完整瞭解這類要求的特定結構,請參閱 Natural Language API 參考資料

如要進一步瞭解如何安裝及使用 Python 適用的 Google Cloud Natural Language 用戶端程式庫,請參閱 Natural Language API 用戶端程式庫
def analyze(movie_review_filename):
    """Run a sentiment analysis request on text within a passed filename."""
    client = language_v1.LanguageServiceClient()

    with open(movie_review_filename) as review_file:
        # Instantiates a plain text document.
        content = review_file.read()

    document = language_v1.Document(
        content=content, type_=language_v1.Document.Type.PLAIN_TEXT
    )
    annotations = client.analyze_sentiment(request={"document": document})

    # Print the results
    print_result(annotations)

這個程式碼片段會執行下列工作:

  1. LanguageServiceClient 例項例項化為用戶端。
  2. 將包含文字資料的檔案名稱讀取為變數。
  3. 使用檔案內容,建立 Document 物件的例項。
  4. 呼叫用戶端的 analyze_sentiment 方法。

剖析回應

def print_result(annotations):
    score = annotations.document_sentiment.score
    magnitude = annotations.document_sentiment.magnitude

    for index, sentence in enumerate(annotations.sentences):
        sentence_sentiment = sentence.sentiment.score
        print(f"Sentence {index} has a sentiment score of {sentence_sentiment}")

    print(f"Overall Sentiment: score of {score} with magnitude of {magnitude}")
    return 0

我們會逐步查看回應來擷取各個語句的 score 情緒值,以及整個評論的整體 scoremagnitude 值,然後向使用者顯示。

執行範例

為執行範例,我們會先在電影《Bladerunner》(銀翼殺手) 的幾個 (假的) 電影評論上進行測試。

  1. 下載來自 Google Cloud Storage 的範例:

    gcloud storage cp gs://cloud-samples-tests/natural-language/sentiment-samples.tgz .
    

    如要安裝最新版的 Google Cloud CLI,請參閱 gcloud CLI 說明文件

  2. 將這些範例解壓縮,建立「reviews」資料夾。

    gunzip sentiment-samples.tgz
    tar -xvf sentiment-samples.tar
    
  3. 對其中一個指定檔案執行情緒分析:

    python sentiment_analysis.py reviews/bladerunner-pos.txt
    Sentence 0 has a sentiment score of 0.8
    Sentence 1 has a sentiment score of 0.9
    Sentence 2 has a sentiment score of 0.8
    Sentence 3 has a sentiment score of 0.2
    Sentence 4 has a sentiment score of 0.1
    Sentence 5 has a sentiment score of 0.4
    Sentence 6 has a sentiment score of 0.3
    Sentence 7 has a sentiment score of 0.4
    Sentence 8 has a sentiment score of 0.2
    Sentence 9 has a sentiment score of 0.9
    Overall Sentiment: score of 0.5 with magnitude of 5.5
    

上述範例表示評論相對正面 (分數為 0.5),且相對情緒化 (強度為 5.5)。

對其他範例執行分析應會產生類似以下的數值:

python sentiment_analysis.py reviews/bladerunner-neg.txt
...
Overall Sentiment: score of -0.6 with magnitude of 3.3

python sentiment_analysis.py reviews/bladerunner-mixed.txt
...
Overall Sentiment: score of 0 with magnitude of 4.7

python sentiment_analysis.py reviews/bladerunner-neutral.txt
...
Overall Sentiment: score of -0.1 with magnitude of 1.8

請注意,正負面的幅度值都很接近 (代表相對等量的深刻情緒),然而「中立」情況卻非如此,這代表評論的正面或負面情緒並不明顯。(如需進一步瞭解情緒分數與幅度以及如何解譯這些數值的資訊,請參閱情緒分析值說明。)

如果您想透過更多資料探索情緒分析,史丹佛大學提供 IMDB 電影評論資料集。要擷取這些電影評論:

  1. 下載大型電影評論資料集
  2. 將檔案解壓縮至您的工作目錄。電影評論會在 traintest 資料目錄中分為 posneg 目錄,且各個文字檔案包含一則電影評論。
  3. 對任何電影評論文字檔案執行 sentiment_analysis.py 工具。

恭喜!您已使用 Google Cloud Natural Language API 執行第一個推論工作!