Google Cloud Natural Language 運算子

透過強大的機器學習模型,Google Cloud Natural Language 可用於揭示文字的結構和含義。您可以使用它從文字文件、新聞文章或部落格文章中提取有關人物、地點、事件等的更多資訊。您還可以使用它來了解社交媒體上對您產品的看法,或者解析呼叫中心或訊息應用程式中客戶對話的意圖。

先決條件任務

要使用這些運算子,您必須完成以下幾項任務

文件

每個運算子都使用一個 Document 來表示文字。

以下是使用字串提供的文字文件示例

tests/system/google/cloud/natural_language/example_natural_language.py

TEXT = """Airflow is a platform to programmatically author, schedule and monitor workflows.

Use Airflow to author workflows as Directed Acyclic Graphs (DAGs) of tasks. The Airflow scheduler executes
 your tasks on an array of workers while following the specified dependencies. Rich command line utilities
 make performing complex surgeries on DAGs a snap. The rich user interface makes it easy to visualize
 pipelines running in production, monitor progress, and troubleshoot issues when needed.
"""
document = Document(content=TEXT, type="PLAIN_TEXT")

除了提供字串外,文件還可以引用儲存在 Google Cloud Storage 中的內容。

tests/system/google/cloud/natural_language/example_natural_language.py

GCS_CONTENT_URI = "gs://INVALID BUCKET NAME/sentiment-me.txt"
document_gcs = Document(gcs_content_uri=GCS_CONTENT_URI, type="PLAIN_TEXT")

分析實體

實體分析檢查給定文字中的已知實體(如公眾人物、地標等專有名詞),並返回有關這些實體的資訊。實體分析使用 CloudNaturalLanguageAnalyzeEntitiesOperator 運算子執行。

tests/system/google/cloud/natural_language/example_natural_language.py

analyze_entities = CloudNaturalLanguageAnalyzeEntitiesOperator(
    document=document, task_id="analyze_entities"
)

您可以使用 Jinja 模板功能 配合 documentgcp_conn_idimpersonation_chain 引數來動態確定值。結果會儲存到 XCom 中,以便其他運算子使用。

tests/system/google/cloud/natural_language/example_natural_language.py

analyze_entities_result = BashOperator(
    bash_command=f"echo {analyze_entities.output}",
    task_id="analyze_entities_result",
)

分析實體情感

情感分析檢查給定文字並識別文字中的主要情感傾向,特別是確定作者的態度是積極、消極還是中立。情感分析透過 CloudNaturalLanguageAnalyzeEntitySentimentOperator 運算子執行。

tests/system/google/cloud/natural_language/example_natural_language.py

analyze_entity_sentiment = CloudNaturalLanguageAnalyzeEntitySentimentOperator(
    document=document, task_id="analyze_entity_sentiment"
)

您可以使用 Jinja 模板功能 配合 documentgcp_conn_idimpersonation_chain 引數來動態確定值。結果會儲存到 XCom 中,以便其他運算子使用。

tests/system/google/cloud/natural_language/example_natural_language.py

analyze_entity_sentiment_result = BashOperator(
    bash_command=f"echo {analyze_entity_sentiment.output}",
    task_id="analyze_entity_sentiment_result",
)

分析情感

情感分析檢查給定文字並識別文字中的主要情感傾向,特別是確定作者的態度是積極、消極還是中立。情感分析透過 CloudNaturalLanguageAnalyzeSentimentOperator 運算子執行。

tests/system/google/cloud/natural_language/example_natural_language.py

analyze_sentiment = CloudNaturalLanguageAnalyzeSentimentOperator(
    document=document, task_id="analyze_sentiment"
)

您可以使用 Jinja 模板功能 配合 documentgcp_conn_idimpersonation_chain 引數來動態確定值。結果會儲存到 XCom 中,以便其他運算子使用。

tests/system/google/cloud/natural_language/example_natural_language.py

analyze_sentiment_result = BashOperator(
    bash_command=f"echo {analyze_sentiment.output}",
    task_id="analyze_sentiment_result",
)

內容分類

內容分類分析文件並返回適用於文件中文字的內容類別列表。要對文件中的內容進行分類,請使用 CloudNaturalLanguageClassifyTextOperator 運算子。

tests/system/google/cloud/natural_language/example_natural_language.py

analyze_classify_text = CloudNaturalLanguageClassifyTextOperator(
    document=document, task_id="analyze_classify_text"
)

您可以使用 Jinja 模板功能 配合 documentgcp_conn_idimpersonation_chain 引數來動態確定值。結果會儲存到 XCom 中,以便其他運算子使用。

tests/system/google/cloud/natural_language/example_natural_language.py

analyze_classify_text_result = BashOperator(
    bash_command=f"echo {analyze_classify_text.output}",
    task_id="analyze_classify_text_result",
)

參考

更多資訊,請查閱

此條目是否對您有幫助?