Classify content

Stay organized with collections Save and categorize content based on your preferences.

Analyze a document and return a list of content categories that apply to the text found in the document.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample


def classify(text, verbose=True):
    """Classify the input text into categories."""

    language_client = language_v1.LanguageServiceClient()

    document = language_v1.Document(
        content=text, type_=language_v1.Document.Type.PLAIN_TEXT
    response = language_client.classify_text(request={"document": document})
    categories = response.categories

    result = {}

    for category in categories:
        # Turn the categories into a dictionary of the form:
        # { category.confidence}, so that they can
        # be treated as a sparse vector.
        result[] = category.confidence

    if verbose:
        for category in categories:
            print("=" * 20)
            print("{:<16}: {}".format("category",
            print("{:<16}: {}".format("confidence", category.confidence))

    return result

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.