NaturalLanguageUnderstanding extends BaseService { public ServiceCall<AnalysisResults> analyze(AnalyzeOptions analyzeOptions) { com.ibm.cloud.sdk.core.util.Validator.notNull(analyzeOptions, "analyzeOptions cannot be null"); String[] pathSegments = {"v1/analyze"}; RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getServiceUrl(), pathSegments)); builder.query("version", versionDate); Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("natural-language-understanding", "v1", "analyze"); for (Entry<String, String> header : sdkHeaders.entrySet()) { builder.header(header.getKey(), header.getValue()); } builder.header("Accept", "application/json"); final JsonObject contentJson = new JsonObject(); contentJson.add( "features", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(analyzeOptions.features())); if (analyzeOptions.text() != null) { contentJson.addProperty("text", analyzeOptions.text()); } if (analyzeOptions.html() != null) { contentJson.addProperty("html", analyzeOptions.html()); } if (analyzeOptions.url() != null) { contentJson.addProperty("url", analyzeOptions.url()); } if (analyzeOptions.clean() != null) { contentJson.addProperty("clean", analyzeOptions.clean()); } if (analyzeOptions.xpath() != null) { contentJson.addProperty("xpath", analyzeOptions.xpath()); } if (analyzeOptions.fallbackToRaw() != null) { contentJson.addProperty("fallback_to_raw", analyzeOptions.fallbackToRaw()); } if (analyzeOptions.returnAnalyzedText() != null) { contentJson.addProperty("return_analyzed_text", analyzeOptions.returnAnalyzedText()); } if (analyzeOptions.language() != null) { contentJson.addProperty("language", analyzeOptions.language()); } if (analyzeOptions.limitTextCharacters() != null) { contentJson.addProperty("limit_text_characters", analyzeOptions.limitTextCharacters()); } builder.bodyJson(contentJson); ResponseConverter<AnalysisResults> responseConverter = ResponseConverterUtils.getValue( new com.google.gson.reflect.TypeToken<AnalysisResults>() {}.getType()); return createServiceCall(builder.build(), responseConverter); } NaturalLanguageUnderstanding(String versionDate);  NaturalLanguageUnderstanding(String versionDate, Authenticator authenticator);  NaturalLanguageUnderstanding(String versionDate, String serviceName);  NaturalLanguageUnderstanding(
      String versionDate, String serviceName, Authenticator authenticator); ServiceCall<AnalysisResults> analyze(AnalyzeOptions analyzeOptions); ServiceCall<ListModelsResults> listModels(ListModelsOptions listModelsOptions); ServiceCall<ListModelsResults> listModels(); ServiceCall<DeleteModelResults> deleteModel(DeleteModelOptions deleteModelOptions);  }
@Test public void testAnalyze() throws InterruptedException, FileNotFoundException { String testHtmlFileName = RESOURCE + "testArticle.html"; String html = getStringFromInputStream(new FileInputStream(testHtmlFileName)); ConceptsOptions concepts = new ConceptsOptions.Builder().limit(5).build(); assertEquals(concepts.limit(), 5, 0); concepts.newBuilder(); Features features = new Features.Builder().concepts(concepts).build(); AnalyzeOptions parameters = new AnalyzeOptions.Builder().html(html).features(features).build(); server.enqueue(jsonResponse(analyzeResults)); final AnalysisResults response = service.analyze(parameters).execute().getResult(); final RecordedRequest request = server.takeRequest(); assertEquals(ANALYZE_PATH, request.getPath()); assertEquals("POST", request.getMethod()); assertEquals(analyzeResults, response); assertNotNull(analyzeResults.getAnalyzedText()); assertNotNull(analyzeResults.getSentiment()); assertNotNull(analyzeResults.getLanguage()); assertNotNull(analyzeResults.getEntities()); assertNotNull(analyzeResults.getEmotion()); assertNotNull(analyzeResults.getConcepts()); assertNotNull(analyzeResults.getCategories()); assertEquals( analyzeResults.getCategories().get(0).getExplanation().getRelevantText().get(0).getText(), response.getCategories().get(0).getExplanation().getRelevantText().get(0).getText()); assertNotNull(analyzeResults.getKeywords()); assertNotNull(analyzeResults.getMetadata()); assertNotNull(analyzeResults.getSemanticRoles()); assertNotNull(analyzeResults.getRetrievedUrl()); assertNotNull(analyzeResults.getRelations()); assertNotNull(analyzeResults.getSyntax()); assertNotNull(analyzeResults.getUsage()); assertEquals(CONFIDENCE, analyzeResults.getEntities().get(0).getConfidence()); assertEquals(TEXT, analyzeResults.getEntities().get(0).getMentions().get(0).getText()); assertEquals( LOCATION, analyzeResults.getEntities().get(0).getMentions().get(0).getLocation().get(0)); assertEquals( CONFIDENCE, analyzeResults.getEntities().get(0).getMentions().get(0).getConfidence()); }
@Test public void testAnalyzeNullParams() throws InterruptedException { server.enqueue(jsonResponse(analyzeResults)); Features features = new Features.Builder() .concepts(null) .categories(null) .emotion(null) .entities(null) .keywords(null) .metadata(null) .relations(null) .semanticRoles(null) .sentiment(null) .build(); AnalyzeOptions.Builder builder = new AnalyzeOptions.Builder().features(features); final AnalysisResults response = service.analyze(builder.build()).execute().getResult(); final RecordedRequest request = server.takeRequest(); assertEquals(ANALYZE_PATH, request.getPath()); assertEquals("POST", request.getMethod()); assertEquals(analyzeResults, response); }