Source code for tests.system.google.cloud.natural_language.example_natural_language
## Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License."""Example Airflow DAG for Google Cloud Natural Language service"""from__future__importannotationsimportosfromdatetimeimportdatetimefromgoogle.cloud.language_v1importDocumentfromairflow.models.dagimportDAGfromairflow.providers.google.cloud.operators.natural_languageimport(CloudNaturalLanguageAnalyzeEntitiesOperator,CloudNaturalLanguageAnalyzeEntitySentimentOperator,CloudNaturalLanguageAnalyzeSentimentOperator,CloudNaturalLanguageClassifyTextOperator,)fromairflow.providers.standard.operators.bashimportBashOperator
[docs]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."""
# [END howto_operator_gcp_natural_language_document_gcs]withDAG(DAG_ID,schedule="@once",# Override to match your needsstart_date=datetime(2021,1,1),catchup=False,tags=["example","natural-language"],)asdag:# [START howto_operator_gcp_natural_language_analyze_entities]
# [END howto_operator_gcp_natural_language_analyze_entities]# [START howto_operator_gcp_natural_language_analyze_entities_result]analyze_entities_result=BashOperator(bash_command=f"echo {analyze_entities.output}",task_id="analyze_entities_result",)# [END howto_operator_gcp_natural_language_analyze_entities_result]# [START howto_operator_gcp_natural_language_analyze_entity_sentiment]analyze_entity_sentiment=CloudNaturalLanguageAnalyzeEntitySentimentOperator(document=document,task_id="analyze_entity_sentiment")# [END howto_operator_gcp_natural_language_analyze_entity_sentiment]# [START howto_operator_gcp_natural_language_analyze_entity_sentiment_result]analyze_entity_sentiment_result=BashOperator(bash_command=f"echo {analyze_entity_sentiment.output}",task_id="analyze_entity_sentiment_result",)# [END howto_operator_gcp_natural_language_analyze_entity_sentiment_result]# [START howto_operator_gcp_natural_language_analyze_sentiment]analyze_sentiment=CloudNaturalLanguageAnalyzeSentimentOperator(document=document,task_id="analyze_sentiment")# [END howto_operator_gcp_natural_language_analyze_sentiment]# [START howto_operator_gcp_natural_language_analyze_sentiment_result]analyze_sentiment_result=BashOperator(bash_command=f"echo {analyze_sentiment.output}",task_id="analyze_sentiment_result",)# [END howto_operator_gcp_natural_language_analyze_sentiment_result]# [START howto_operator_gcp_natural_language_analyze_classify_text]analyze_classify_text=CloudNaturalLanguageClassifyTextOperator(document=document,task_id="analyze_classify_text")# [END howto_operator_gcp_natural_language_analyze_classify_text]# [START howto_operator_gcp_natural_language_analyze_classify_text_result]analyze_classify_text_result=BashOperator(bash_command=f"echo {analyze_classify_text.output}",task_id="analyze_classify_text_result",)# [END howto_operator_gcp_natural_language_analyze_classify_text_result]analyze_entities>>analyze_entities_resultanalyze_entity_sentiment>>analyze_entity_sentiment_resultanalyze_sentiment>>analyze_sentiment_resultanalyze_classify_text>>analyze_classify_text_resultfromtests_common.test_utils.watcherimportwatcher# This test needs watcher in order to properly mark success/failure# when "teardown" task with trigger rule is part of the DAGlist(dag.tasks)>>watcher()fromtests_common.test_utils.system_testsimportget_test_run# noqa: E402# Needed to run the example DAG with pytest (see: tests/system/README.md#run_via_pytest)