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
来表示文本。
以下是使用字符串提供的文本文档示例
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 模板功能 配合 document
、gcp_conn_id
、impersonation_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 模板功能 配合 document
、gcp_conn_id
、impersonation_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 模板功能 配合 document
、gcp_conn_id
、impersonation_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 模板功能 配合 document
、gcp_conn_id
、impersonation_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",
)
参考¶
更多信息,请查阅