Watson NLU - Not enough text provided for language detection

2.1k Views Asked by At

I am working with Watson NLU and trying to sentiment analysis on some text. The problem is that some text is too small for it to detect what language it is (E.g: Excellent service).Is there a way for me to specify that if language detection isn't possible, it should consider it English?

The snippet of my NLU (Java) is this:

SentimentOptions sentiment = new SentimentOptions.Builder()
    .targets(targets)
    .document(true)
    .build();


Features features = new Features.Builder()
    .sentiment(sentiment)
    .build();


AnalyzeOptions parameters = new AnalyzeOptions.Builder()
    .text(text)
    .features(features)
    .build();

AnalysisResults response = service
    .analyze(parameters)
    .execute();

String mySentiment = response.getSentiment().getDocument().getLabel();
1

There are 1 best solutions below

6
On BEST ANSWER

According to the Official API Reference documentation, you need to specify the language parameter in your POST request.

See more about these parameters on Watson Developer Cloud - Github - Java SDK.

Explanation of the API Reference - NLU:

  • language (string): ISO 639-1 code indicating the language to use for the analysis. This code overrides the automatic language detection performed by the service. Valid codes are ar (Arabic), en (English), fr (French), de (German), it (Italian), pt (Portuguese), ru (Russian), es (Spanish), and sv (Swedish). For more information about which features are supported in each language, see this table.

Example of how it works:

parameters.json file example:

{
  "text": "Excelent service",
  "features": {
    "semantic_roles": {}
  },
  "language": "en"
}

The cURL example:

curl -X POST \
-H "Content-Type: application/json" \
-u "{username}":"{password}" \
-d @parameters.json \
"https://gateway.watsonplatform.net/natural-language-understanding/api/v1/analyze?version=2017-02-27"

Probably your example (you did not specify your programming language, so):

AnalyzeOptions parameters = new AnalyzeOptions.Builder()
    .text(text)
    .features(features)
    .language('en')
    .build();