Skip to content

Named entity recognition

Named entity recognition is a type of document analysis.
It determines which entities—persons, places, organizations, dates, addresses, etc.—are mentioned in a text and the attributes of the entity that can be inferred by semantic analysis.

Named entity recognition also performs knowledge linking: Knowledge Graph information and open data—Wikidata, DBpedia and GeoNames references—are returned for entities corresponding to syncons of the expert.ai Knowledge Graph. In the case of actual places, geographic coordinates are also provided.

Entities are also recognized in pronouns and shorter forms that refer to named mentions.
This kind of by reference recognition is anaphoric because entities are recognized through anaphoras.

For example in this text:

Michael Jordan was one of the best basketball players of all time.

Scoring was Jordan's stand-out skill, but he still holds a defensive NBA record, with eight steals in a half.

three mentions of Michael Jordan are recognized:

  • the full named mention: Michael Jordan
  • the anaphoras—Jordan and he—for which Michael Jordan is considered the antecedent.

The API resource carrying out named entity recognition has the following endpoint:

/api/analyze

In the reference section of this manual you will find all the information you need to perform named entity recognition, specifically:

Here is an example of performing named entity recognition on a short English test:

This example is based on the Python client you can find on GitHub.

The client gets user credentials from two environment variables:

EAI_USERNAME
EAI_PASSWORD

Set those variables with you account credentials before running the sample program below.

The program prints the list of entities with their type.

from expertai.nlapi.edge.client import ExpertAiClient
client = ExpertAiClient()

text = "Michael Jordan was one of the best basketball players of all time. Scoring was Jordan's stand-out skill, but he still holds a defensive NBA record, with eight steals in a half."

output = client.named_entity_recognition(text)

print (f'{"ENTITY":{50}} {"TYPE":{10}}')
print (f'{"------":{50}} {"----":{10}}')

for entity in output.entities:
    print (f'{entity.lemma:{50}} {entity.type_:{10}}')

This example is based on the Java client you can find on GitHub.

The client gets user credentials from two environment variables:

EAI_USERNAME
EAI_PASSWORD

Set those variables with you account credentials before running the sample program below.

The program prints the JSON response and the list of entities with their type.

import ai.expert.nlapi.security.Authentication;
import ai.expert.nlapi.security.Authenticator;
import ai.expert.nlapi.security.BasicAuthenticator;
import ai.expert.nlapi.security.DefaultCredentialsProvider;
import ai.expert.nlapi.v2.API;
import ai.expert.nlapi.v2.edge.Analyzer;
import ai.expert.nlapi.v2.edge.AnalyzerConfig;
import ai.expert.nlapi.v2.message.AnalyzeResponse;
import ai.expert.nlapi.v2.model.AnalyzeDocument;

public class Main {

    public static Authentication createAuthentication() throws Exception {
        DefaultCredentialsProvider credentialsProvider = new DefaultCredentialsProvider();
        Authenticator authenticator = new BasicAuthenticator(credentialsProvider);
        return new Authentication(authenticator);
    }

    public static Analyzer createAnalyzer() throws Exception {
        return new Analyzer(AnalyzerConfig.builder()
                .withVersion(API.Versions.V2)
                .withHost(API.DEFAULT_EDGE_HOST)
                .withAuthentication(createAuthentication())
                .build());
    }

    public static void main(String[] args) {
        try {
            String text = "Michael Jordan was one of the best basketball players of all time. Scoring was Jordan's stand-out skill, but he still holds a defensive NBA record, with eight steals in a half.";

            Analyzer analyzer = createAnalyzer();

            AnalyzeResponse entities = analyzer.entities(text);


            // Output JSON representation

            System.out.println("JSON representation:");
            entities.prettyPrint();


            // Tab separated list of entitites' lemma and type.

            System.out.println("Tab separated list of entities' lemma and type:");
            AnalyzeDocument data = entities.getData();
            data.getEntities().stream().forEach(c -> System.out.println(c.getLemma() + "\t" + c.getType()));
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}