コンテンツの分類

コンテンツの分類は、ドキュメントを分析し、ドキュメント内で見つかったテキストに適用されるコンテンツ カテゴリのリストを返します。ドキュメント内のコンテンツを分類するには、classifyText メソッドを呼び出します。

classifyText メソッドに対して返されるコンテンツ カテゴリの完全なリストについては、こちらをご覧ください。

このセクションでは、ドキュメント内のコンテンツを分類する方法について説明します。

コンテンツの分類

文字列として指定されたコンテンツを分類する例を以下に示します。

プロトコル

ドキュメントに含まれるコンテンツを分類するには、documents:classifyText REST メソッドに対して POST リクエストを行います。リクエストには、次の例に示す適切なリクエスト本文を指定します。

この例では、Google Cloud Platform の Cloud SDK を使用してプロジェクト用にセットアップされたサービス アカウントのアクセス トークンを取得するために、gcloud auth application-default print-access-token コマンドを使用しています。Cloud SDK をインストールし、サービス アカウントを使用してプロジェクトをセットアップする手順については、クイックスタートをご覧ください。

curl -X POST \
     -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
     -H "Content-Type: application/json; charset=utf-8" \
     --data "{
  'document':{
    'type':'PLAIN_TEXT',
    'content':'Google, headquartered in Mountain View, unveiled the new Android
    phone at the Consumer Electronic Show.  Sundar Pichai said in his keynote
    that users love their new Android phones.'
  }
}" "https://language.googleapis.com/v1/documents:classifyText"

C#

        private static void ClassifyTextFromText(string text)
        {
            var client = LanguageServiceClient.Create();
            var response = client.ClassifyText(new Document()
            {
                Content = text,
                Type = Document.Types.Type.PlainText
            });
            WriteCategories(response.Categories);
        }

        private static void WriteCategories(IEnumerable<ClassificationCategory> categories)
        {
            Console.WriteLine("Categories:");
            foreach (var category in categories)
            {
                Console.WriteLine($"\tCategory: {category.Name}");
                Console.WriteLine($"\t\tConfidence: {category.Confidence}");
            }
        }

Go

func classifyText(ctx context.Context, client *language.Client, text string) (*languagepb.ClassifyTextResponse, error) {
	return client.ClassifyText(ctx, &languagepb.ClassifyTextRequest{
		Document: &languagepb.Document{
			Source: &languagepb.Document_Content{
				Content: text,
			},
			Type: languagepb.Document_PLAIN_TEXT,
		},
	})
}

Java

// Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
try (LanguageServiceClient language = LanguageServiceClient.create()) {
  // set content to the text string
  Document doc = Document.newBuilder()
      .setContent(text)
      .setType(Type.PLAIN_TEXT)
      .build();
  ClassifyTextRequest request = ClassifyTextRequest.newBuilder()
      .setDocument(doc)
      .build();
  // detect categories in the given text
  ClassifyTextResponse response = language.classifyText(request);

  for (ClassificationCategory category : response.getCategoriesList()) {
    System.out.printf("Category name : %s, Confidence : %.3f\n",
        category.getName(), category.getConfidence());
  }
}

Node.js

// Imports the Google Cloud client library
const language = require('@google-cloud/language');

// Creates a client
const client = new language.LanguageServiceClient();

/**
 * TODO(developer): Uncomment the following line to run this code.
 */
// const text = 'Your text to analyze, e.g. Hello, world!';

// Prepares a document, representing the provided text
const document = {
  content: text,
  type: 'PLAIN_TEXT',
};

// Classifies text in the document
const [classification] = await client.classifyText({document});
console.log('Categories:');
classification.categories.forEach(category => {
  console.log(`Name: ${category.name}, Confidence: ${category.confidence}`);
});

Python

import six
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types

text = 'Android is a mobile operating system developed by Google, ' \
       'based on the Linux kernel and designed primarily for ' \
       'touchscreen mobile devices such as smartphones and tablets.'

client = language.LanguageServiceClient()

if isinstance(text, six.binary_type):
    text = text.decode('utf-8')

document = types.Document(
    content=text.encode('utf-8'),
    type=enums.Document.Type.PLAIN_TEXT)

categories = client.classify_text(document).categories

for category in categories:
    print(u'=' * 20)
    print(u'{:<16}: {}'.format('name', category.name))
    print(u'{:<16}: {}'.format('confidence', category.confidence))

PHP

namespace Google\Cloud\Samples\Language;

use Google\Cloud\Language\V1beta2\Document;
use Google\Cloud\Language\V1beta2\Document\Type;
use Google\Cloud\Language\V1beta2\LanguageServiceClient;

/**
 * Classify text (20+ words) into categories.
 * ```
 * classify_text(
 *     'The first two gubernatorial elections since President Donald Trump ' .
 *     'took office went in favor of Democratic candidates in Virginia and ' .
 *     'New Jersey.'
 * );
 * ```
 *
 * @param string $text The text to analyze.
 * @param string $projectId (optional) Your Google Cloud Project ID
 */

function classify_text($text, $projectId = null)
{
    // Make sure we have enough words (20+) to call classifyText
    if (str_word_count($text) < 20) {
        printf('20+ words are required to classify text.' . PHP_EOL);
        return;
    }
    $languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]);
    try {
        // Create a new Document
        $document = new Document();
        // Pass GCS URI and set document type to PLAIN_TEXT (1)
        $document->setContent($text)->setType(Type::PLAIN_TEXT);
        // Call the analyzeSentiment function
        $response = $languageServiceClient->classifyText($document);
        $categories = $response->getCategories();
        // Print document information
        foreach ($categories as $category) {
            printf('Category Name: %s' . PHP_EOL, $category->getName());
            printf('Confidence: %s' . PHP_EOL, $category->getConfidence());
            print(PHP_EOL);
        }
    } finally {
        $languageServiceClient->close();
    }
}

Ruby

# text_content = "Text to classify"

require "google/cloud/language"

language = Google::Cloud::Language.new
response = language.classify_text content: text_content, type: :PLAIN_TEXT

categories = response.categories

categories.each do |category|
  puts "Name: #{category.name} Confidence: #{category.confidence}"
end

Google Cloud Storage からのコンテンツの分類

Google Cloud Storage 上のテキスト ファイルに保存されたコンテンツを分類する例を以下に示します。

プロトコル

Google Cloud Storage に保存されたドキュメントに含まれるコンテンツを分類するには、documents:classifyText REST メソッドに対して POST リクエストを行います。リクエストには、次の例に示す適切なリクエスト本文とドキュメントへのパスを指定します。

curl -X POST \
     -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
     -H "Content-Type: application/json; charset=utf-8" \
     --data "{
  'document':{
    'type':'PLAIN_TEXT',
    'gcsContentUri':'gs://<bucket-name>/<object-name>'
  }
}" "https://language.googleapis.com/v1/documents:classifyText"

C#

private static void ClassifyTextFromFile(string gcsUri)
{
    var client = LanguageServiceClient.Create();
    var response = client.ClassifyText(new Document()
    {
        GcsContentUri = gcsUri,
        Type = Document.Types.Type.PlainText
    });
    WriteCategories(response.Categories);
}
private static void WriteCategories(IEnumerable<ClassificationCategory> categories)
{
    Console.WriteLine("Categories:");
    foreach (var category in categories)
    {
        Console.WriteLine($"\tCategory: {category.Name}");
        Console.WriteLine($"\t\tConfidence: {category.Confidence}");
    }
}

Go

func classifyTextFromGCS(ctx context.Context, gcsURI string) (*languagepb.ClassifyTextResponse, error) {
	return client.ClassifyText(ctx, &languagepb.ClassifyTextRequest{
		Document: &languagepb.Document{
			Source: &languagepb.Document_GcsContentUri{
				GcsContentUri: gcsURI,
			},
			Type: languagepb.Document_PLAIN_TEXT,
		},
	})
}

Java

// Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
try (LanguageServiceClient language = LanguageServiceClient.create()) {
  // set the GCS content URI path
  Document doc = Document.newBuilder()
      .setGcsContentUri(gcsUri)
      .setType(Type.PLAIN_TEXT)
      .build();
  ClassifyTextRequest request = ClassifyTextRequest.newBuilder()
      .setDocument(doc)
      .build();
  // detect categories in the given file
  ClassifyTextResponse response = language.classifyText(request);

  for (ClassificationCategory category : response.getCategoriesList()) {
    System.out.printf("Category name : %s, Confidence : %.3f\n",
        category.getName(), category.getConfidence());
  }
}

Node.js

// Imports the Google Cloud client library.
const language = require('@google-cloud/language');

// Creates a client.
const client = new language.LanguageServiceClient();

/**
 * TODO(developer): Uncomment the following lines to run this code
 */
// const bucketName = 'Your bucket name, e.g. my-bucket';
// const fileName = 'Your file name, e.g. my-file.txt';

// Prepares a document, representing a text file in Cloud Storage
const document = {
  gcsContentUri: `gs://${bucketName}/${fileName}`,
  type: 'PLAIN_TEXT',
};

// Classifies text in the document
const [classification] = await client.classifyText({document});

console.log('Categories:');
classification.categories.forEach(category => {
  console.log(`Name: ${category.name}, Confidence: ${category.confidence}`);
});

Python

from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types

gcs_uri = 'gs://cloud-samples-data/language/android.txt'

client = language.LanguageServiceClient()

document = types.Document(
    gcs_content_uri=gcs_uri,
    type=enums.Document.Type.PLAIN_TEXT)

categories = client.classify_text(document).categories

for category in categories:
    print(u'=' * 20)
    print(u'{:<16}: {}'.format('name', category.name))
    print(u'{:<16}: {}'.format('confidence', category.confidence))

PHP

namespace Google\Cloud\Samples\Language;

use Google\Cloud\Language\V1beta2\Document;
use Google\Cloud\Language\V1beta2\Document\Type;
use Google\Cloud\Language\V1beta2\LanguageServiceClient;

/**
 * Classify text (20+ words) into categories.
 * ```
 * classify_text_from_file('gs://storage-bucket/file-name');
 * ```
 *
 * @param string $cloud_storage_uri Your Cloud Storage bucket URI
 * @param string $projectId (optional) Your Google Cloud Project ID
 */

function classify_text_from_file($gcsUri, $projectId = null)
{
    $languageServiceClient = new LanguageServiceClient(['projectId' => $projectId]);
    try {
        // Create a new Document
        $document = new Document();
        // Pass GCS URI and set document type to PLAIN_TEXT
        $document->setGcsContentUri($gcsUri)->setType(Type::PLAIN_TEXT);
        // Call the analyzeSentiment function
        $response = $languageServiceClient->classifyText($document);
        $categories = $response->getCategories();
        // Print document information
        foreach ($categories as $category) {
            printf('Category Name: %s' . PHP_EOL, $category->getName());
            printf('Confidence: %s' . PHP_EOL, $category->getConfidence());
            print(PHP_EOL);
        }
    } finally {
        $languageServiceClient->close();
    }
}

Ruby

# storage_path = "Path to file in Google Cloud Storage, eg. gs://bucket/file"

require "google/cloud/language"

language = Google::Cloud::Language.new
response = language.classify_text gcs_content_uri: storage_path, type: :PLAIN_TEXT

categories = response.categories

categories.each do |category|
  puts "Name: #{category.name} Confidence: #{category.confidence}"
end

このページは役立ちましたか?評価をお願いいたします。

フィードバックを送信...

Cloud Natural Language API
ご不明な点がありましたら、Google のサポートページをご覧ください。