javaibm-watsonwatson-nlu

Watson NLU - Not enough text provided for language detection


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();

Solution

  • 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:

    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();