forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguage_classify_text.py
More file actions
64 lines (52 loc) · 2.22 KB
/
language_classify_text.py
File metadata and controls
64 lines (52 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#
# Copyright 2023 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.
# 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
# [START language_classify_text]
from google.cloud import language_v2
def sample_classify_text(
text_content: str = "That actor on TV makes movies in Hollywood and also stars in a variety of popular new TV shows.",
) -> None:
"""
Classifies Content in a string.
Args:
text_content: The text content to analyze.
"""
client = language_v2.LanguageServiceClient()
# Available types: PLAIN_TEXT, HTML
document_type_in_plain_text = language_v2.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_code = "en"
document = {
"content": text_content,
"type_": document_type_in_plain_text,
"language_code": language_code,
}
response = client.classify_text(request={"document": 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(f"Category name: {category.name}")
# Get the confidence. Number representing how certain the classifier
# is that this category represents the provided text.
print(f"Confidence: {category.confidence}")
# [END language_classify_text]