Menganalisis Entity

Analisis Entity memeriksa teks tertentu untuk menemukan entity yang diketahui (kata benda khusus seperti tokoh masyarakat, tempat terkenal, dll.), dan menampilkan informasi tentang entity tersebut. Analisis entity dilakukan dengan metode analyzeEntities. Untuk mengetahui informasi tentang jenis entity yang diidentifikasi oleh Natural Language, lihat dokumentasi Entity. Untuk mengetahui informasi tentang bahasa yang didukung Natural Language API, baca bagian Dukungan Bahasa.

Bagian ini menunjukkan beberapa cara untuk mendeteksi entity dalam dokumen. Anda harus mengirimkan permintaan terpisah untuk setiap dokumen.

Menganalisis Entity dalam String

Berikut adalah contoh pelaksanaan analisis entity pada string teks yang dikirim langsung ke Natural Language API:

Protokol

Untuk menganalisis entity dalam dokumen, buat permintaan POST ke metode REST documents:analyzeEntities dan berikan isi permintaan yang sesuai seperti yang ditunjukkan dalam contoh berikut.

Contoh ini menggunakan perintah gcloud auth application-default print-access-token untuk mendapatkan token akses untuk akun layanan yang disiapkan untuk project menggunakan gcloud CLI Google Cloud Platform. Untuk mengetahui petunjuk cara menginstal gcloud CLI, lihat Panduan Memulai untuk menyiapkan project dengan akun layanan.

curl -X POST \
     -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
     -H "Content-Type: application/json; charset=utf-8" \
     --data "{
  'encodingType': 'UTF8',
  'document': {
    'type': 'PLAIN_TEXT',
    'content': 'President Trump will speak from the White House, located
  at 1600 Pennsylvania Ave NW, Washington, DC, on October 7.'
  }
}" "https://language.googleapis.com/v2/documents:analyzeEntities"

Jika Anda tidak menentukan document.language_code, bahasa akan otomatis terdeteksi. Untuk mengetahui informasi tentang bahasa yang didukung Natural Language API, lihat Dukungan Bahasa. Baca dokumentasi referensi Document untuk mengetahui informasi selengkapnya tentang cara mengonfigurasi isi permintaan.

Jika permintaan berhasil, server akan menampilkan kode status HTTP 200 OK dan respons dalam format JSON:

{
  "entities": [
    {
      "name": "October 7",
      "type": "DATE",
      "metadata": {
        "month": "10",
        "day": "7"
      },
      "mentions": [
        {
          "text": {
            "content": "October 7",
            "beginOffset": -1
          },
          "type": "TYPE_UNKNOWN",
          "probability": 1
        }
      ]
    },
    {
      "name": "1600",
      "type": "NUMBER",
      "metadata": {
        "value": "1600"
      },
      "mentions": [
        {
          "text": {
            "content": "1600",
            "beginOffset": -1
          },
          "type": "TYPE_UNKNOWN",
          "probability": 1
        }
      ]
    },
    {
      "name": "7",
      "type": "NUMBER",
      "metadata": {
        "value": "7"
      },
      "mentions": [
        {
          "text": {
            "content": "7",
            "beginOffset": -1
          },
          "type": "TYPE_UNKNOWN",
          "probability": 1
        }
      ]
    },
    {
      "name": "1600 Pennsylvania Ave NW, Washington, DC",
      "type": "ADDRESS",
      "metadata": {
        "locality": "Washington",
        "narrow_region": "District of Columbia",
        "street_name": "Pennsylvania Avenue Northwest",
        "street_number": "1600",
        "broad_region": "District of Columbia",
        "country": "US"
      },
      "mentions": [
        {
          "text": {
            "content": "1600 Pennsylvania Ave NW, Washington, DC",
            "beginOffset": -1
          },
          "type": "TYPE_UNKNOWN",
          "probability": 1
        }
      ]
    },
    {
      "name": "1600 Pennsylvania Ave NW",
      "type": "LOCATION",
      "metadata": {},
      "mentions": [
        {
          "text": {
            "content": "1600 Pennsylvania Ave NW",
            "beginOffset": -1
          },
          "type": "PROPER",
          "probability": 0.901
        }
      ]
    },
    {
      "name": "President",
      "type": "PERSON",
      "metadata": {},
      "mentions": [
        {
          "text": {
            "content": "President",
            "beginOffset": -1
          },
          "type": "COMMON",
          "probability": 0.941
        }
      ]
    },
    {
      "name": "Trump",
      "type": "PERSON",
      "metadata": {},
      "mentions": [
        {
          "text": {
            "content": "Trump",
            "beginOffset": -1
          },
          "type": "PROPER",
          "probability": 0.948
        }
      ]
    },
    {
      "name": "Washington, DC",
      "type": "LOCATION",
      "metadata": {},
      "mentions": [
        {
          "text": {
            "content": "Washington, DC",
            "beginOffset": -1
          },
          "type": "PROPER",
          "probability": 0.92
        }
      ]
    },
    {
      "name": "White House",
      "type": "LOCATION",
      "metadata": {},
      "mentions": [
        {
          "text": {
            "content": "White House",
            "beginOffset": -1
          },
          "type": "PROPER",
          "probability": 0.785
        }
      ]
    }
  ],
  "languageCode": "en",
  "languageSupported": true
}

Array entities berisi objek Entity yang mewakili entity yang terdeteksi, yang menyertakan informasi seperti nama dan jenis entity.

gcloud

Lihat perintah analyze-entities untuk detail selengkapnya.

Untuk melakukan analisis entity, gunakan gcloud CLI dan gunakan flag --content untuk mengidentifikasi konten yang akan dianalisis:

gcloud ml language analyze-entities --content="President Trump will speak from the White House, located
  at 1600 Pennsylvania Ave NW, Washington, DC, on October 7."

Jika permintaan berhasil, server akan menampilkan respons dalam format JSON:

{
  "entities": [
    {
      "name": "Trump",
      "type": "PERSON",
      "metadata": {
        "mid": "/m/0cqt90",
        "wikipedia_url": "https://en.wikipedia.org/wiki/Donald_Trump"
      },
      "salience": 0.7936003,
      "mentions": [
        {
          "text": {
            "content": "Trump",
            "beginOffset": 10
          },
          "type": "PROPER"
        },
        {
          "text": {
            "content": "President",
            "beginOffset": 0
          },
          "type": "COMMON"
        }
      ]
    },
    {
      "name": "White House",
      "type": "LOCATION",
      "metadata": {
        "mid": "/m/081sq",
        "wikipedia_url": "https://en.wikipedia.org/wiki/White_House"
      },
      "salience": 0.09172433,
      "mentions": [
        {
          "text": {
            "content": "White House",
            "beginOffset": 36
          },
          "type": "PROPER"
        }
      ]
    },
    {
      "name": "Pennsylvania Ave NW",
      "type": "LOCATION",
      "metadata": {
        "mid": "/g/1tgb87cq"
      },
      "salience": 0.085507184,
      "mentions": [
        {
          "text": {
            "content": "Pennsylvania Ave NW",
            "beginOffset": 65
          },
          "type": "PROPER"
        }
      ]
    },
    {
      "name": "Washington, DC",
      "type": "LOCATION",
      "metadata": {
        "mid": "/m/0rh6k",
        "wikipedia_url": "https://en.wikipedia.org/wiki/Washington,_D.C."
      },
      "salience": 0.029168168,
      "mentions": [
        {
          "text": {
            "content": "Washington, DC",
            "beginOffset": 86
          },
          "type": "PROPER"
        }
      ]
    }
    {
      "name": "1600 Pennsylvania Ave NW, Washington, DC",
      "type": "ADDRESS",
      "metadata": {
        "country": "US",
        "sublocality": "Fort Lesley J. McNair",
        "locality": "Washington",
        "street_name": "Pennsylvania Avenue Northwest",
        "broad_region": "District of Columbia",
        "narrow_region": "District of Columbia",
        "street_number": "1600"
      },
      "salience": 0,
      "mentions": [
        {
          "text": {
            "content": "1600 Pennsylvania Ave NW, Washington, DC",
            "beginOffset": 60
          },
          "type": "TYPE_UNKNOWN"
        }
      ]
      }
    }
    {
      "name": "1600",
       "type": "NUMBER",
       "metadata": {
           "value": "1600"
       },
       "salience": 0,
       "mentions": [
         {
          "text": {
              "content": "1600",
              "beginOffset": 60
           },
           "type": "TYPE_UNKNOWN"
        }
     ]
     },
     {
       "name": "October 7",
       "type": "DATE",
       "metadata": {
         "day": "7",
         "month": "10"
       },
       "salience": 0,
       "mentions": [
         {
           "text": {
             "content": "October 7",
             "beginOffset": 105
            },
           "type": "TYPE_UNKNOWN"
         }
       ]
     }
     {
       "name": "7",
       "type": "NUMBER",
       "metadata": {
         "value": "7"
       },
       "salience": 0,
       "mentions": [
         {
           "text": {
             "content": "7",
             "beginOffset": 113
           },
         "type": "TYPE_UNKNOWN"
         }
        ]
     }
  ],
  "language": "en"
}

Array entities berisi objek Entity yang mewakili entity yang terdeteksi, yang menyertakan informasi seperti nama dan jenis entity.

Go

Untuk mempelajari cara menginstal dan menggunakan library klien untuk Natural Language, lihat library klien Natural Language. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API Go Natural Language.

Untuk mengautentikasi ke Natural Language, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

import (
	"context"
	"fmt"
	"io"

	language "cloud.google.com/go/language/apiv2"
	"cloud.google.com/go/language/apiv2/languagepb"
)

// analyzeEntities sends a string of text to the Cloud Natural Language API to
// detect the entities of the text.
func analyzeEntities(w io.Writer, text string) error {
	ctx := context.Background()

	// Initialize client.
	client, err := language.NewClient(ctx)
	if err != nil {
		return err
	}
	defer client.Close()

	resp, err := client.AnalyzeEntities(ctx, &languagepb.AnalyzeEntitiesRequest{
		Document: &languagepb.Document{
			Source: &languagepb.Document_Content{
				Content: text,
			},
			Type: languagepb.Document_PLAIN_TEXT,
		},
		EncodingType: languagepb.EncodingType_UTF8,
	})

	if err != nil {
		return fmt.Errorf("AnalyzeEntities: %w", err)
	}
	fmt.Fprintf(w, "Response: %q\n", resp)

	return nil
}

Java

Untuk mempelajari cara menginstal dan menggunakan library klien untuk Natural Language, lihat library klien Natural Language. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API Java Natural Language.

Untuk mengautentikasi ke Natural Language, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

// Instantiate the Language client com.google.cloud.language.v2.LanguageServiceClient
try (LanguageServiceClient language = LanguageServiceClient.create()) {
  Document doc = Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build();
  AnalyzeEntitiesRequest request =
      AnalyzeEntitiesRequest.newBuilder()
          .setDocument(doc)
          .setEncodingType(EncodingType.UTF16)
          .build();

  AnalyzeEntitiesResponse response = language.analyzeEntities(request);

  // Print the response
  for (Entity entity : response.getEntitiesList()) {
    System.out.printf("Entity: %s", entity.getName());
    System.out.println("Metadata: ");
    for (Map.Entry<String, String> entry : entity.getMetadataMap().entrySet()) {
      System.out.printf("%s : %s", entry.getKey(), entry.getValue());
    }
    for (EntityMention mention : entity.getMentionsList()) {
      System.out.printf("Begin offset: %d\n", mention.getText().getBeginOffset());
      System.out.printf("Content: %s\n", mention.getText().getContent());
      System.out.printf("Type: %s\n\n", mention.getType());
      System.out.printf("Probability: %s\n\n", mention.getProbability());
    }
  }
}

Node.js

Untuk mempelajari cara menginstal dan menggunakan library klien untuk Natural Language, lihat library klien Natural Language. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API Node.js Natural Language.

Untuk mengautentikasi ke Natural Language, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

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

// 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',
};

// Detects entities in the document
const [result] = await client.analyzeEntities({document});

const entities = result.entities;

console.log('Entities:');
entities.forEach(entity => {
  console.log(entity.name);
  console.log(` - Type: ${entity.type}`);
  if (entity.metadata) {
    console.log(` - Metadata: ${entity.metadata}`);
  }
});

Python

Untuk mempelajari cara menginstal dan menggunakan library klien untuk Natural Language, lihat library klien Natural Language. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API Python Natural Language.

Untuk mengautentikasi ke Natural Language, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

from google.cloud import language_v2

def sample_analyze_entities(text_content: str = "California is a state.") -> None:
    """
    Analyzes Entities 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,
    }

    # Available values: NONE, UTF8, UTF16, UTF32.
    # See https://cloud.google.com/natural-language/docs/reference/rest/v2/EncodingType.
    encoding_type = language_v2.EncodingType.UTF8

    response = client.analyze_entities(
        request={"document": document, "encoding_type": encoding_type}
    )

    for entity in response.entities:
        print(f"Representative name for the entity: {entity.name}")

        # Get entity type, e.g. PERSON, LOCATION, ADDRESS, NUMBER, et al.
        # See https://cloud.google.com/natural-language/docs/reference/rest/v2/Entity#type.
        print(f"Entity type: {language_v2.Entity.Type(entity.type_).name}")

        # Loop over the metadata associated with entity.
        # 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(f"{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(f"Mention text: {mention.text.content}")

            # Get the mention type, e.g. PROPER for proper noun
            print(f"Mention type: {language_v2.EntityMention.Type(mention.type_).name}")

            # Get the probability score associated with the first mention of the entity in the (0, 1.0] range.
            print(f"Probability score: {mention.probability}")

    # 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(f"Language of the text: {response.language_code}")

Bahasa tambahan

C#: Ikuti Petunjuk penyiapan C# di halaman library klien lalu buka Dokumentasi referensi Natural Language untuk .NET.

PHP: Ikuti petunjuk penyiapan PHP di halaman library klien lalu buka Dokumentasi referensi Natural Language untuk PHP.

Ruby: Ikuti petunjuk penyiapan Ruby di halaman library klien, lalu buka Dokumentasi referensi Natural Language untuk Ruby.

Menganalisis Entity dari Cloud Storage

Untuk memudahkan Anda, Natural Language API dapat menjalankan analisis entity langsung pada file yang berada di Cloud Storage, tanpa perlu mengirimkan konten file ke isi permintaan Anda.

Berikut contoh pelaksanaan analisis entity pada file yang berada di Cloud Storage.

Protokol

Untuk menganalisis entity dari dokumen yang disimpan di Cloud Storage, buat permintaan POST ke metode REST documents:analyzeEntities dan berikan isi permintaan yang sesuai dengan jalur ke dokumen seperti yang ditunjukkan pada contoh berikut.

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/v2/documents:analyzeEntities"

Jika Anda tidak menentukan document.language_code, bahasa akan otomatis terdeteksi. Untuk mengetahui informasi tentang bahasa yang didukung Natural Language API, lihat Dukungan Bahasa. Lihat dokumentasi referensi Document untuk mengetahui informasi selengkapnya tentang cara mengonfigurasi isi permintaan.

Jika permintaan berhasil, server akan menampilkan kode status HTTP 200 OK dan respons dalam format JSON:

{
  "entities": [
    {
      "name": "October 7",
      "type": "DATE",
      "metadata": {
        "month": "10",
        "day": "7"
      },
      "mentions": [
        {
          "text": {
            "content": "October 7",
            "beginOffset": -1
          },
          "type": "TYPE_UNKNOWN",
          "probability": 1
        }
      ]
    },
    {
      "name": "1600",
      "type": "NUMBER",
      "metadata": {
        "value": "1600"
      },
      "mentions": [
        {
          "text": {
            "content": "1600",
            "beginOffset": -1
          },
          "type": "TYPE_UNKNOWN",
          "probability": 1
        }
      ]
    },
    {
      "name": "7",
      "type": "NUMBER",
      "metadata": {
        "value": "7"
      },
      "mentions": [
        {
          "text": {
            "content": "7",
            "beginOffset": -1
          },
          "type": "TYPE_UNKNOWN",
          "probability": 1
        }
      ]
    },
    {
      "name": "1600 Pennsylvania Ave NW, Washington, DC",
      "type": "ADDRESS",
      "metadata": {
        "locality": "Washington",
        "narrow_region": "District of Columbia",
        "street_name": "Pennsylvania Avenue Northwest",
        "street_number": "1600",
        "broad_region": "District of Columbia",
        "country": "US"
      },
      "mentions": [
        {
          "text": {
            "content": "1600 Pennsylvania Ave NW, Washington, DC",
            "beginOffset": -1
          },
          "type": "TYPE_UNKNOWN",
          "probability": 1
        }
      ]
    },
    {
      "name": "1600 Pennsylvania Ave NW",
      "type": "LOCATION",
      "metadata": {},
      "mentions": [
        {
          "text": {
            "content": "1600 Pennsylvania Ave NW",
            "beginOffset": -1
          },
          "type": "PROPER",
          "probability": 0.901
        }
      ]
    },
    {
      "name": "President",
      "type": "PERSON",
      "metadata": {},
      "mentions": [
        {
          "text": {
            "content": "President",
            "beginOffset": -1
          },
          "type": "COMMON",
          "probability": 0.941
        }
      ]
    },
    {
      "name": "Trump",
      "type": "PERSON",
      "metadata": {},
      "mentions": [
        {
          "text": {
            "content": "Trump",
            "beginOffset": -1
          },
          "type": "PROPER",
          "probability": 0.948
        }
      ]
    },
    {
      "name": "Washington, DC",
      "type": "LOCATION",
      "metadata": {},
      "mentions": [
        {
          "text": {
            "content": "Washington, DC",
            "beginOffset": -1
          },
          "type": "PROPER",
          "probability": 0.92
        }
      ]
    },
    {
      "name": "White House",
      "type": "LOCATION",
      "metadata": {},
      "mentions": [
        {
          "text": {
            "content": "White House",
            "beginOffset": -1
          },
          "type": "PROPER",
          "probability": 0.785
        }
      ]
    }
  ],
  "languageCode": "en",
  "languageSupported": true
}

Array entities berisi objek Entity yang mewakili entity yang terdeteksi, yang menyertakan informasi seperti nama dan jenis entity.

gcloud

Lihat perintah analyze-entities untuk detail selengkapnya.

Untuk melakukan analisis entity pada file di Cloud Storage, gunakan alat command line gcloud dan gunakan flag --content-file untuk mengidentifikasi jalur file yang berisi konten yang akan dianalisis:

gcloud ml language analyze-entities --content-file=gs://YOUR_BUCKET_NAME/YOUR_FILE_NAME

Jika permintaan berhasil, server akan menampilkan respons dalam format JSON:

{
  "entities": [
    {
      "name": "October 7",
      "type": "DATE",
      "metadata": {
        "month": "10",
        "day": "7"
      },
      "mentions": [
        {
          "text": {
            "content": "October 7",
            "beginOffset": -1
          },
          "type": "TYPE_UNKNOWN",
          "probability": 1
        }
      ]
    },
    {
      "name": "1600",
      "type": "NUMBER",
      "metadata": {
        "value": "1600"
      },
      "mentions": [
        {
          "text": {
            "content": "1600",
            "beginOffset": -1
          },
          "type": "TYPE_UNKNOWN",
          "probability": 1
        }
      ]
    },
    {
      "name": "7",
      "type": "NUMBER",
      "metadata": {
        "value": "7"
      },
      "mentions": [
        {
          "text": {
            "content": "7",
            "beginOffset": -1
          },
          "type": "TYPE_UNKNOWN",
          "probability": 1
        }
      ]
    },
    {
      "name": "1600 Pennsylvania Ave NW, Washington, DC",
      "type": "ADDRESS",
      "metadata": {
        "locality": "Washington",
        "narrow_region": "District of Columbia",
        "street_name": "Pennsylvania Avenue Northwest",
        "street_number": "1600",
        "broad_region": "District of Columbia",
        "country": "US"
      },
      "mentions": [
        {
          "text": {
            "content": "1600 Pennsylvania Ave NW, Washington, DC",
            "beginOffset": -1
          },
          "type": "TYPE_UNKNOWN",
          "probability": 1
        }
      ]
    },
    {
      "name": "1600 Pennsylvania Ave NW",
      "type": "LOCATION",
      "metadata": {},
      "mentions": [
        {
          "text": {
            "content": "1600 Pennsylvania Ave NW",
            "beginOffset": -1
          },
          "type": "PROPER",
          "probability": 0.901
        }
      ]
    },
    {
      "name": "President",
      "type": "PERSON",
      "metadata": {},
      "mentions": [
        {
          "text": {
            "content": "President",
            "beginOffset": -1
          },
          "type": "COMMON",
          "probability": 0.941
        }
      ]
    },
    {
      "name": "Trump",
      "type": "PERSON",
      "metadata": {},
      "mentions": [
        {
          "text": {
            "content": "Trump",
            "beginOffset": -1
          },
          "type": "PROPER",
          "probability": 0.948
        }
      ]
    },
    {
      "name": "Washington, DC",
      "type": "LOCATION",
      "metadata": {},
      "mentions": [
        {
          "text": {
            "content": "Washington, DC",
            "beginOffset": -1
          },
          "type": "PROPER",
          "probability": 0.92
        }
      ]
    },
    {
      "name": "White House",
      "type": "LOCATION",
      "metadata": {},
      "mentions": [
        {
          "text": {
            "content": "White House",
            "beginOffset": -1
          },
          "type": "PROPER",
          "probability": 0.785
        }
      ]
    }
  ],
  "languageCode": "en",
  "languageSupported": true
}

Array entities berisi objek Entity yang mewakili entity yang terdeteksi, yang menyertakan informasi seperti nama dan jenis entity.

Go

Untuk mempelajari cara menginstal dan menggunakan library klien untuk Natural Language, lihat library klien Natural Language. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API Go Natural Language.

Untuk mengautentikasi ke Natural Language, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.


func analyzeEntitiesFromGCS(ctx context.Context, gcsURI string) (*languagepb.AnalyzeEntitiesResponse, error) {
	return client.AnalyzeEntities(ctx, &languagepb.AnalyzeEntitiesRequest{
		Document: &languagepb.Document{
			Source: &languagepb.Document_GcsContentUri{
				GcsContentUri: gcsURI,
			},
			Type: languagepb.Document_PLAIN_TEXT,
		},
		EncodingType: languagepb.EncodingType_UTF8,
	})
}

Java

Untuk mempelajari cara menginstal dan menggunakan library klien untuk Natural Language, lihat library klien Natural Language. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API Java Natural Language.

Untuk mengautentikasi ke Natural Language, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

// Instantiate the Language client com.google.cloud.language.v2.LanguageServiceClient
try (LanguageServiceClient language = LanguageServiceClient.create()) {
  // Set the GCS Content URI path to the file to be analyzed
  Document doc =
      Document.newBuilder().setGcsContentUri(gcsUri).setType(Type.PLAIN_TEXT).build();
  AnalyzeEntitiesRequest request =
      AnalyzeEntitiesRequest.newBuilder()
          .setDocument(doc)
          .setEncodingType(EncodingType.UTF16)
          .build();

  AnalyzeEntitiesResponse response = language.analyzeEntities(request);

  // Print the response
  for (Entity entity : response.getEntitiesList()) {
    System.out.printf("Entity: %s\n", entity.getName());
    System.out.println("Metadata: ");
    for (Map.Entry<String, String> entry : entity.getMetadataMap().entrySet()) {
      System.out.printf("%s : %s", entry.getKey(), entry.getValue());
    }
    for (EntityMention mention : entity.getMentionsList()) {
      System.out.printf("Begin offset: %d\n", mention.getText().getBeginOffset());
      System.out.printf("Content: %s\n", mention.getText().getContent());
      System.out.printf("Type: %s\n\n", mention.getType());
      System.out.printf("Probability: %s\n\n", mention.getProbability());
    }
  }
}

Node.js

Untuk mempelajari cara menginstal dan menggunakan library klien untuk Natural Language, lihat library klien Natural Language. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API Node.js Natural Language.

Untuk mengautentikasi ke Natural Language, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

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

// 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',
};

// Detects entities in the document
const [result] = await client.analyzeEntities({document});
const entities = result.entities;

console.log('Entities:');
entities.forEach(entity => {
  console.log(entity.name);
  console.log(` - Type: ${entity.type}`);
  if (entity.metadata) {
    console.log(` - Metadata: ${entity.metadata}`);
  }
});

Python

Untuk mempelajari cara menginstal dan menggunakan library klien untuk Natural Language, lihat library klien Natural Language. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API Python Natural Language.

Untuk mengautentikasi ke Natural Language, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

from google.cloud import language_v2

def sample_analyze_entities(
    gcs_content_uri: str = "gs://cloud-samples-data/language/entity.txt",
) -> None:
    """
    Analyzes 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_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 = {
        "gcs_content_uri": gcs_content_uri,
        "type_": document_type_in_plain_text,
        "language_code": language_code,
    }

    # Available values: NONE, UTF8, UTF16, UTF32.
    # See https://cloud.google.com/natural-language/docs/reference/rest/v2/EncodingType.
    encoding_type = language_v2.EncodingType.UTF8

    response = client.analyze_entities(
        request={"document": document, "encoding_type": encoding_type}
    )

    for entity in response.entities:
        print(f"Representative name for the entity: {entity.name}")

        # Get entity type, e.g. PERSON, LOCATION, ADDRESS, NUMBER, et al.
        # See https://cloud.google.com/natural-language/docs/reference/rest/v2/Entity#type.
        print(f"Entity type: {language_v2.Entity.Type(entity.type_).name}")

        # Loop over the metadata associated with entity.
        # 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(f"{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(f"Mention text: {mention.text.content}")

            # Get the mention type, e.g. PROPER for proper noun
            print(
                "Mention type:" f" {language_v2.EntityMention.Type(mention.type_).name}"
            )

            # Get the probability score associated with the first mention of the entity in the (0, 1.0] range.
            print(f"Probability score: {mention.probability}")

    # 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(f"Language of the text: {response.language_code}")

Bahasa tambahan

C#: Ikuti Petunjuk penyiapan C# di halaman library klien lalu buka Dokumentasi referensi Natural Language untuk .NET.

PHP: Ikuti petunjuk penyiapan PHP di halaman library klien lalu buka Dokumentasi referensi Natural Language untuk PHP.

Ruby: Ikuti petunjuk penyiapan Ruby di halaman library klien, lalu buka Dokumentasi referensi Natural Language untuk Ruby.