Skip to content

3. Make your first call

Once you have a client tool set up, you can try out named entity recognition on a short English text.

First call with curl

Run the following command from a shell after replacing the string token (only that, the string Bearer must not be changed) with the actual authorization token you obtained in the previous step:

curl -X POST https://nlapi.expert.ai/v2/analyze/standard/en/entities \
    -H 'Authorization: Bearer token' \
    -H 'Content-Type: application/json; charset=utf-8' \
    -d '{
  "document": {
    "text" : "Michael Jordan was one of the best basketball players of all time. Scoring was Jordan stand-out skill, but he still holds a defensive NBA record, with eight steals in a half."
  }
}'

Open a command prompt in the folder where you installed curl and run the following command after replacing the string token (only that, the string Bearer must not be changed) with the actual authorization token you obtained in the previous step:

curl -X POST https://nlapi.expert.ai/v2/analyze/standard/en/entities -H "Authorization: Bearer token" -H "Content-Type: application/json"  -d "{\"document\": {\"text\": \"Michael Jordan was one of the best basketball players of all time. Scoring was Jordan stand-out skill, but he still holds a defensive NBA record, with eight steals in a half.\"}}"

The command result is a JSON object containing recognized entities.

First call with Postman

  1. Run Postman.
  2. Expand the expert.ai Natural Language API collection.
  3. Expand the Document analysis folder.
  4. Select entities.
  5. Select Send in the right panel. The result is a JSON object containing recognized entities.

First call with an expert.ai client package

This example code uses expertai-nlapi, the open-source Python client corresponding to the nlapi-python GitHub project.

The Python client needs to know your account credentials. It can get them from two environment variables:

EAI_USERNAME
EAI_PASSWORD

Set those variables before running the script below.

The script prints out the named entities recognized in a short English text.

from expertai.nlapi.cloud.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."
language= 'en'

output = client.specific_resource_analysis(body={"document": {"text": text}}, params={'language': language, 'resource': 'entities'})

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 code uses @expertai/nlapi, the open-source NodeJS client corresponding to the nlapi-nodejs GitHub project.

The client gets user credentials from two environment variables:

EAI_USERNAME
EAI_PASSWORD

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

The program prints out the named entities recognized in a short English text.

import {NLClient} from "@expertai/nlapi";
import {Language} from "@expertai/nlapi";
import {Analysis} from "@expertai/nlapi";

var nlClient = new NLClient();

var 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.";

nlClient.analyze(text, {
  language: Language.EN,
  context: "standard",
  analysis: Analysis.Entities
}).then((result) => {
    console.log("Named entities with their type:");
    console.table(result.data.entities, ["lemma", "type"]);
})

This example code uses nlapi-java-sdk, the open-source Java client corresponding to the nlapi-java GitHub project.

The Java client needs to know your account credentials. It can get them from two environment variables:

EAI_USERNAME
EAI_PASSWORD

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

The program prints a JSON object containing the entities recognized in a short English text.

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.cloud.Analyzer;
import ai.expert.nlapi.v2.cloud.AnalyzerConfig;
import ai.expert.nlapi.v2.message.AnalyzeResponse;

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)
                                          .withContext("standard")
                                          .withLanguage(API.Languages.en)
                                          .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

            entities.prettyPrint();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}

The next step

Now that you've had a taste of the API, discover its capabilites and learn more about using it programmatically.