Watson NLU - 没有为语言检测提供足够的文本

Aur*_*ore 3 java ibm-watson watson-nlu

我正在与 Watson NLU 合作并尝试对某些文本进行情感分析。问题是某些文本太小,无法检测到它是什么语言(例如:优质的服务)。有没有办法让我指定如果无法进行语言检测,则应将其视为英语?

我的 NLU (Java) 的片段是这样的:

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();
Run Code Online (Sandbox Code Playgroud)

Say*_*chi 5

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"
}
Run Code Online (Sandbox Code Playgroud)

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"
Run Code Online (Sandbox Code Playgroud)

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

AnalyzeOptions parameters = new AnalyzeOptions.Builder()
    .text(text)
    .features(features)
    .language('en')
    .build();
Run Code Online (Sandbox Code Playgroud)