Skip to content

Full analysis

Try it live

Full analysis is the sum of all partial document analyses:

Natural Language API resources carrying out full document analysis have paths like this:

analyze/context name/language code

Boxed parts are placeholders, so for example:

https://nlapi.expert.ai/v2/analyze/standard/en

is the URL of the standard context resource performing the full analysis of an English text.
These resources must be requested with the POST method.

In the reference section of this manual you will find all the information you need to perform full document analysis using the API's RESTful interface, specifically:

Note

Even if you consume the API through a ready-to-use client that hides low-level requests and responses, knowing the output format helps you understand and navigate the results.

Here is an example of performing full document analysis on a short English text:

This example code uses expertai-nlapi, the open-source Python client corresponding to the nlapi-python 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 the number of items for each of the output's arrays.

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.full_analysis(body={"document": {"text": text}}, params={'language': language})


# Output arrays size

print("Output arrays size:");

print("knowledge: ", len(output.knowledge))
print("paragraphs: ", len(output.paragraphs))
print("sentences: ", len(output.sentences))
print("phrases: ", len(output.phrases))
print("tokens: ", len(output.tokens))
print("mainSentences: ", len(output.main_sentences))
print("mainPhrases: ", len(output.main_phrases))
print("mainLemmas: ", len(output.main_lemmas))
print("mainSyncons: ", len(output.main_syncons))
print("topics: ", len(output.topics))
print("entities: ", len(output.entities))
print("entities: ", len(output.relations))
print("sentiment.items: ", len(output.sentiment.items))

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 the number of items for each of the output's arrays.

import {NLClient} from "@expertai/nlapi";
import {Language} 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"
}).then((result) => {
    console.log("Arrays' lengths:");
    console.log("knowledge: " + result.data.knowledge.length);
    console.log("paragraphs: " + result.data.paragraphs.length);
    console.log("sentences: " + result.data.sentences.length);
    console.log("phrases: " + result.data.phrases.length);
    console.log("tokens: " + result.data.tokens.length);
    console.log("mainSentences: " + result.data.mainSentences.length);
    console.log("mainPhrases: " + result.data.mainPhrases.length);
    console.log("mainLemmas: " + result.data.mainLemmas.length);
    console.log("mainSyncons: " + result.data.mainSyncons.length);
    console.log("topics: " + result.data.topics.length);
    console.log("entities: " + result.data.entities.length);
    console.log("relations: " + result.data.relations.length);
    console.log("sentiment/items: " + result.data.sentiment.items.length);
})

This example code uses nlapi-java-sdk, the open-source Java client corresponding to the nlapi-java 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 the number of items for each of the output's arrays.

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;
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)
                                      .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 analysis = analyzer.analyze(text);


            // Output JSON representation

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


            // Output arrays size

            System.out.println("Output arrays size:");
            AnalyzeDocument data = analysis.getData();

            System.out.println("knowledge: " + data.getKnowledge().size());
            System.out.println("paragraphs: " + data.getParagraphs().size());
            System.out.println("sentences: " + data.getSentences().size());
            System.out.println("phrases: " + data.getPhrases().size());
            System.out.println("tokens: " + data.getTokens().size());
            System.out.println("mainSentences: " + data.getMainSentences().size());
            System.out.println("mainPhrases: " + data.getMainPhrases().size());
            System.out.println("mainLemmas: " + data.getMainLemmas().size());
            System.out.println("mainSyncons: " + data.getMainSyncons().size());
            System.out.println("topics: " + data.getTopics().size());
            System.out.println("entities: " + data.getEntities().size());
            System.out.println("relations: " + data.getRelations().size());
            System.out.println("sentiment/items: " + data.getSentiment().getItems().size());
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}

The following curl command posts a document to the full document analysis resource of the API's REST interface.
Run the command from a shell after replacing token with the actual authorization token.

curl -X POST https://nlapi.expert.ai/v2/analyze/standard/en \
    -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'\''s stand-out skill, but he still holds a defensive NBA record, with eight steals in a half."
  }
}'

The server returns a JSON object.

The following curl command posts a document to the full document analysis resource of the API's REST interface.
Open a command prompt in the folder where you installed curl and run the command after replacing token with the actual authorization token.

curl -X POST https://nlapi.expert.ai/v2/analyze/standard/en  -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's stand-out skill, but he still holds a defensive NBA record, with eight steals in a half.\"}}"

The server returns a JSON object.