Google Cloud Natural Language 運算子¶
您可以透過強大的機器學習模型,使用 Google Cloud Natural Language 來揭示文字的結構和意義。您可以使用它來提取文本文件、新聞文章或部落格文章中提到的人物、地點、事件等資訊。您可以使用它來了解社群媒體上關於您產品的情緒,或解析來自電話客服中心或訊息應用程式中客戶對話的意圖。
先決條件任務¶
要使用這些運算子,您必須完成幾件事
使用 Cloud Console 選擇或建立 Cloud Platform 專案。
為您的專案啟用計費功能,如 Google Cloud 文件中所述。
啟用 API,如 Cloud Console 文件中所述。
透過 pip 安裝 API 函式庫。
pip install 'apache-airflow[google]'詳細資訊請參閱 安裝。
文件¶
每個運算子都使用 Document
來表示文字。
以下是以字串形式提供文字的文件範例
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 中的內容。
GCS_CONTENT_URI = "gs://INVALID BUCKET NAME/sentiment-me.txt"
document_gcs = Document(gcs_content_uri=GCS_CONTENT_URI, type="PLAIN_TEXT")
分析實體¶
實體分析會檢查給定的文字中已知的實體(例如公眾人物、地標等專有名詞),並傳回有關這些實體的資訊。實體分析是透過 CloudNaturalLanguageAnalyzeEntitiesOperator
運算子執行的。
analyze_entities = CloudNaturalLanguageAnalyzeEntitiesOperator(
document=document, task_id="analyze_entities"
)
您可以將 Jinja 模板 與 document
、 gcp_conn_id
、 impersonation_chain
參數一起使用,這讓您可以動態決定值。結果會儲存到 XCom,以便其他運算子可以使用。
analyze_entities_result = BashOperator(
bash_command=f"echo {analyze_entities.output}",
task_id="analyze_entities_result",
)
分析實體情感¶
情感分析會檢查給定的文字,並識別文字中普遍的情緒觀點,特別是判斷作者的態度是正面、負面還是中性。情感分析是透過 CloudNaturalLanguageAnalyzeEntitySentimentOperator
運算子執行的。
analyze_entity_sentiment = CloudNaturalLanguageAnalyzeEntitySentimentOperator(
document=document, task_id="analyze_entity_sentiment"
)
您可以將 Jinja 模板 與 document
、 gcp_conn_id
、 impersonation_chain
參數一起使用,這讓您可以動態決定值。結果會儲存到 XCom,以便其他運算子可以使用。
analyze_entity_sentiment_result = BashOperator(
bash_command=f"echo {analyze_entity_sentiment.output}",
task_id="analyze_entity_sentiment_result",
)
分析情感¶
情感分析會檢查給定的文字,並識別文字中普遍的情緒觀點,特別是判斷作者的態度是正面、負面還是中性。情感分析是透過 CloudNaturalLanguageAnalyzeSentimentOperator
運算子執行的。
analyze_sentiment = CloudNaturalLanguageAnalyzeSentimentOperator(
document=document, task_id="analyze_sentiment"
)
您可以將 Jinja 模板 與 document
、 gcp_conn_id
、 impersonation_chain
參數一起使用,這讓您可以動態決定值。結果會儲存到 XCom,以便其他運算子可以使用。
analyze_sentiment_result = BashOperator(
bash_command=f"echo {analyze_sentiment.output}",
task_id="analyze_sentiment_result",
)
內容分類¶
內容分類會分析文件,並傳回適用於文件中文字的內容類別列表。若要對文件中的內容進行分類,請使用 CloudNaturalLanguageClassifyTextOperator
運算子。
analyze_classify_text = CloudNaturalLanguageClassifyTextOperator(
document=document, task_id="analyze_classify_text"
)
您可以將 Jinja 模板 與 document
、 gcp_conn_id
、 impersonation_chain
參數一起使用,這讓您可以動態決定值。結果會儲存到 XCom,以便其他運算子可以使用。
analyze_classify_text_result = BashOperator(
bash_command=f"echo {analyze_classify_text.output}",
task_id="analyze_classify_text_result",
)