Skip to content

Commit

Permalink
Add generated code samples. (#9153)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rebecca Taylor authored and tseaver committed Sep 4, 2019
1 parent 95953a5 commit 37da8b4
Show file tree
Hide file tree
Showing 19 changed files with 1,426 additions and 11 deletions.
20 changes: 20 additions & 0 deletions packages/google-cloud-language/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@ def system(session):
session.run("py.test", "--quiet", system_test_folder_path, *session.posargs)


@nox.session(python=["3.7"])
def samples(session):
"""Run the samples test suite."""
# Sanity check: Only run tests if the environment variable is set.
if not os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", ""):
session.skip("Credentials must be set via environment variable")

samples_path = "samples"
if not os.path.exists(samples_path):
session.skip("Samples not found.")

session.install("pyyaml")
session.install("sample-tester")
for local_dep in LOCAL_DEPS:
session.install("-e", local_dep)
session.install("-e", ".")

session.run("sample-tester", samples_path, *session.posargs)


@nox.session(python="3.7")
def cover(session):
"""Run the final coverage report.
Expand Down
85 changes: 85 additions & 0 deletions packages/google-cloud-language/samples/v1/language_classify_gcs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# -*- coding: utf-8 -*-
#
# Copyright 2019 Google LLC
#
# Licensed 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
#
# https://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.

# DO NOT EDIT! This is a generated sample ("Request", "language_classify_gcs")

# To install the latest published package dependency, execute the following:
# pip install google-cloud-language

# sample-metadata
# title: Classify Content (GCS)
# description: Classifying Content in text file stored in Cloud Storage
# usage: python3 samples/v1/language_classify_gcs.py [--gcs_content_uri "gs://cloud-samples-data/language/classify-entertainment.txt"]

# [START language_classify_gcs]
from google.cloud import language_v1
from google.cloud.language_v1 import enums


def sample_classify_text(gcs_content_uri):
"""
Classifying Content in text file stored in Cloud Storage
Args:
gcs_content_uri Google Cloud Storage URI where the file content is located.
e.g. gs://[Your Bucket]/[Path to File]
The text file must include at least 20 words.
"""

client = language_v1.LanguageServiceClient()

# gcs_content_uri = 'gs://cloud-samples-data/language/classify-entertainment.txt'

# Available types: PLAIN_TEXT, HTML
type_ = enums.Document.Type.PLAIN_TEXT

# Optional. If not specified, the language is automatically detected.
# For list of supported languages:
# https://cloud.google.com/natural-language/docs/languages
language = "en"
document = {"gcs_content_uri": gcs_content_uri, "type": type_, "language": language}

response = client.classify_text(document)
# Loop through classified categories returned from the API
for category in response.categories:
# Get the name of the category representing the document.
# See the predefined taxonomy of categories:
# https://cloud.google.com/natural-language/docs/categories
print(u"Category name: {}".format(category.name))
# Get the confidence. Number representing how certain the classifier
# is that this category represents the provided text.
print(u"Confidence: {}".format(category.confidence))


# [END language_classify_gcs]


def main():
import argparse

parser = argparse.ArgumentParser()
parser.add_argument(
"--gcs_content_uri",
type=str,
default="gs://cloud-samples-data/language/classify-entertainment.txt",
)
args = parser.parse_args()

sample_classify_text(args.gcs_content_uri)


if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# -*- coding: utf-8 -*-
#
# Copyright 2019 Google LLC
#
# Licensed 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
#
# https://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.

# DO NOT EDIT! This is a generated sample ("Request", "language_classify_text")

# To install the latest published package dependency, execute the following:
# pip install google-cloud-language

# sample-metadata
# title: Classify Content
# description: Classifying Content in a String
# usage: python3 samples/v1/language_classify_text.py [--text_content "That actor on TV makes movies in Hollywood and also stars in a variety of popular new TV shows."]

# [START language_classify_text]
from google.cloud import language_v1
from google.cloud.language_v1 import enums


def sample_classify_text(text_content):
"""
Classifying Content in a String
Args:
text_content The text content to analyze. Must include at least 20 words.
"""

client = language_v1.LanguageServiceClient()

# text_content = 'That actor on TV makes movies in Hollywood and also stars in a variety of popular new TV shows.'

# Available types: PLAIN_TEXT, HTML
type_ = enums.Document.Type.PLAIN_TEXT

# Optional. If not specified, the language is automatically detected.
# For list of supported languages:
# https://cloud.google.com/natural-language/docs/languages
language = "en"
document = {"content": text_content, "type": type_, "language": language}

response = client.classify_text(document)
# Loop through classified categories returned from the API
for category in response.categories:
# Get the name of the category representing the document.
# See the predefined taxonomy of categories:
# https://cloud.google.com/natural-language/docs/categories
print(u"Category name: {}".format(category.name))
# Get the confidence. Number representing how certain the classifier
# is that this category represents the provided text.
print(u"Confidence: {}".format(category.confidence))


# [END language_classify_text]


def main():
import argparse

parser = argparse.ArgumentParser()
parser.add_argument(
"--text_content",
type=str,
default="That actor on TV makes movies in Hollywood and also stars in a variety of popular new TV shows.",
)
args = parser.parse_args()

sample_classify_text(args.text_content)


if __name__ == "__main__":
main()
105 changes: 105 additions & 0 deletions packages/google-cloud-language/samples/v1/language_entities_gcs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# -*- coding: utf-8 -*-
#
# Copyright 2019 Google LLC
#
# Licensed 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
#
# https://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.

# DO NOT EDIT! This is a generated sample ("Request", "language_entities_gcs")

# To install the latest published package dependency, execute the following:
# pip install google-cloud-language

# sample-metadata
# title: Analyzing Entities (GCS)
# description: Analyzing Entities in text file stored in Cloud Storage
# usage: python3 samples/v1/language_entities_gcs.py [--gcs_content_uri "gs://cloud-samples-data/language/entity.txt"]

# [START language_entities_gcs]
from google.cloud import language_v1
from google.cloud.language_v1 import enums


def sample_analyze_entities(gcs_content_uri):
"""
Analyzing Entities in text file stored in Cloud Storage
Args:
gcs_content_uri Google Cloud Storage URI where the file content is located.
e.g. gs://[Your Bucket]/[Path to File]
"""

client = language_v1.LanguageServiceClient()

# gcs_content_uri = 'gs://cloud-samples-data/language/entity.txt'

# Available types: PLAIN_TEXT, HTML
type_ = enums.Document.Type.PLAIN_TEXT

# Optional. If not specified, the language is automatically detected.
# For list of supported languages:
# https://cloud.google.com/natural-language/docs/languages
language = "en"
document = {"gcs_content_uri": gcs_content_uri, "type": type_, "language": language}

# Available values: NONE, UTF8, UTF16, UTF32
encoding_type = enums.EncodingType.UTF8

response = client.analyze_entities(document, encoding_type=encoding_type)
# Loop through entitites returned from the API
for entity in response.entities:
print(u"Representative name for the entity: {}".format(entity.name))
# Get entity type, e.g. PERSON, LOCATION, ADDRESS, NUMBER, et al
print(u"Entity type: {}".format(enums.Entity.Type(entity.type).name))
# Get the salience score associated with the entity in the [0, 1.0] range
print(u"Salience score: {}".format(entity.salience))
# Loop over the metadata associated with entity. For many known entities,
# the metadata is a Wikipedia URL (wikipedia_url) and Knowledge Graph MID (mid).
# Some entity types may have additional metadata, e.g. ADDRESS entities
# may have metadata for the address street_name, postal_code, et al.
for metadata_name, metadata_value in entity.metadata.items():
print(u"{}: {}".format(metadata_name, metadata_value))

# Loop over the mentions of this entity in the input document.
# The API currently supports proper noun mentions.
for mention in entity.mentions:
print(u"Mention text: {}".format(mention.text.content))
# Get the mention type, e.g. PROPER for proper noun
print(
u"Mention type: {}".format(enums.EntityMention.Type(mention.type).name)
)

# Get the language of the text, which will be the same as
# the language specified in the request or, if not specified,
# the automatically-detected language.
print(u"Language of the text: {}".format(response.language))


# [END language_entities_gcs]


def main():
import argparse

parser = argparse.ArgumentParser()
parser.add_argument(
"--gcs_content_uri",
type=str,
default="gs://cloud-samples-data/language/entity.txt",
)
args = parser.parse_args()

sample_analyze_entities(args.gcs_content_uri)


if __name__ == "__main__":
main()
Loading

0 comments on commit 37da8b4

Please sign in to comment.