Inspect text for known entities (proper nouns such as public figures and landmarks), and return information about those entities.

## Explore further


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

- [Analyzing Entities](https://docs.cloud.google.com/natural-language/docs/analyzing-entities)

## Code sample

### Go


To learn how to install and use the client library for Natural Language, see
[Natural Language client libraries](https://docs.cloud.google.com/natural-language/docs/reference/libraries).


For more information, see the
[Natural Language Go API
reference documentation](https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/language/latest/apiv1).


To authenticate to Natural Language, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).


    func analyzeEntities(ctx context.Context, client *language.Client, text string) (*languagepb.AnalyzeEntitiesResponse, error) {
    	return client.AnalyzeEntities(ctx, &languagepb.AnalyzeEntitiesRequest{
    		Document: &languagepb.Document{
    			Source: &languagepb.Document_Content{
    				Content: text,
    			},
    			Type: languagepb.Document_PLAIN_TEXT,
    		},
    		EncodingType: languagepb.EncodingType_UTF8,
    	})
    }

### Java


To learn how to install and use the client library for Natural Language, see
[Natural Language client libraries](https://docs.cloud.google.com/natural-language/docs/reference/libraries).


For more information, see the
[Natural Language Java API
reference documentation](https://docs.cloud.google.com/java/docs/reference/google-cloud-language/latest/overview).


To authenticate to Natural Language, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    // 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


To learn how to install and use the client library for Natural Language, see
[Natural Language client libraries](https://docs.cloud.google.com/natural-language/docs/reference/libraries).


For more information, see the
[Natural Language Node.js API
reference documentation](https://docs.cloud.google.com/nodejs/docs/reference/language/latest).


To authenticate to Natural Language, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    // Imports the Google Cloud client library
    const language = require('https://docs.cloud.google.com/nodejs/docs/reference/language/latest/overview.html');

    // Creates a client
    const client = new language.https://docs.cloud.google.com/nodejs/docs/reference/language/latest/overview.html();

    /**
     * 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}, Salience: ${entity.salience}`);
      if (entity.metadata && entity.metadata.wikipedia_url) {
        console.log(` - Wikipedia URL: ${entity.metadata.wikipedia_url}`);
      }
    });

### PHP


To learn how to install and use the client library for Natural Language, see
[Natural Language client libraries](https://docs.cloud.google.com/natural-language/docs/reference/libraries).


To authenticate to Natural Language, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    use Google\Cloud\Language\V1\AnalyzeEntitiesRequest;
    use Google\Cloud\Language\V1\Client\LanguageServiceClient;
    use Google\Cloud\Language\V1\Document;
    use Google\Cloud\Language\V1\Document\Type;
    use Google\Cloud\Language\V1\Entity\Type as EntityType;

    /**
     * @param string $text The text to analyze
     */
    function analyze_entities(string $text): void
    {
        // Create the Natural Language client
        $languageServiceClient = new LanguageServiceClient();

        // Create a new Document, add text as content and set type to PLAIN_TEXT
        $document = (new Document())
            ->setContent($text)
            ->setType(Type::PLAIN_TEXT);

        // Call the analyzeEntities function
        $request = (new AnalyzeEntitiesRequest())
            ->setDocument($document);
        $response = $languageServiceClient->analyzeEntities($request);
        $entities = $response->getEntities();
        // Print out information about each entity
        foreach ($entities as $entity) {
            printf('Name: %s' . PHP_EOL, $entity->getName());
            printf('Type: %s' . PHP_EOL, EntityType::name($entity->getType()));
            printf('Salience: %s' . PHP_EOL, $entity->getSalience());
            /** @var array<string, string> $metadata */
            $metadata = $entity->getMetadata();
            if (isset($metadata['wikipedia_url'])) {
                printf('Wikipedia URL: %s' . PHP_EOL, $metadata['wikipedia_url']);
            }
            if (isset($metadata['mid'])) {
                printf('Knowledge Graph MID: %s' . PHP_EOL, $metadata['mid']);
            }
            printf(PHP_EOL);
        }
    }

### Python


To learn how to install and use the client library for Natural Language, see
[Natural Language client libraries](https://docs.cloud.google.com/natural-language/docs/reference/libraries).


For more information, see the
[Natural Language Python API
reference documentation](https://docs.cloud.google.com/python/docs/reference/language/latest).


To authenticate to Natural Language, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    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.https://docs.cloud.google.com/python/docs/reference/language/latest/google.cloud.language_v1.services.language_service.LanguageServiceClient.html#google_cloud_language_v1_services_language_service_LanguageServiceClient_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}")

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples?product=cloudnaturallanguageapi).