Search in sources :

Example 6 with AnalysisResults

use of com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.AnalysisResults in project java-sdk by watson-developer-cloud.

the class NaturalLanguageUnderstandingIT method analyzeTextForSemanticRolesIsSuccessful.

/**
 * Analyze input text for semantic roles.
 *
 * @throws Exception the exception
 */
@Test
public void analyzeTextForSemanticRolesIsSuccessful() throws Exception {
    SemanticRolesOptions options = new SemanticRolesOptions.Builder().limit(7).keywords(true).entities(true).build();
    Features features = new Features.Builder().semanticRoles(options).build();
    AnalyzeOptions parameters = new AnalyzeOptions.Builder().text(text).features(features).returnAnalyzedText(true).build();
    AnalysisResults results = service.analyze(parameters).execute();
    assertNotNull(results);
    assertEquals(results.getAnalyzedText(), text);
    assertEquals(results.getLanguage(), "en");
    assertNotNull(results.getSemanticRoles());
    for (SemanticRolesResult result : results.getSemanticRoles()) {
        assertEquals(result.getSentence(), text);
        if (result.getSubject() != null) {
            assertNotNull(result.getSubject().getText());
        }
        if (result.getAction() != null) {
            assertNotNull(result.getAction().getText());
        }
        if (result.getObject() != null) {
            assertNotNull(result.getObject().getText());
        }
    }
}
Also used : AnalyzeOptions(com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.AnalyzeOptions) AnalysisResults(com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.AnalysisResults) Features(com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.Features) SemanticRolesOptions(com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.SemanticRolesOptions) SemanticRolesResult(com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.SemanticRolesResult) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Example 7 with AnalysisResults

use of com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.AnalysisResults in project java-sdk by watson-developer-cloud.

the class NaturalLanguageUnderstandingIT method analyzeTextForEmotionsWithoutTargetsIsSuccessful.

/**
 * Analyze input text for emotions without targets.
 *
 * @throws Exception the exception
 */
@Test
public void analyzeTextForEmotionsWithoutTargetsIsSuccessful() throws Exception {
    String text = "But I believe this thinking is wrong. I believe the road of true democracy remains the better path." + " I believe that in the 21st century, economies can only grow to a certain point until they need to open up" + " -- because entrepreneurs need to access information in order to invent; young people need a global" + " education in order to thrive; independent media needs to check the abuses of power.";
    EmotionOptions emotion = new EmotionOptions.Builder().build();
    Features features = new Features.Builder().emotion(emotion).build();
    AnalyzeOptions parameters = new AnalyzeOptions.Builder().text(text).features(features).returnAnalyzedText(true).build();
    AnalysisResults results = service.analyze(parameters).execute();
    assertNotNull(results);
    assertNotNull(results.getAnalyzedText());
    assertNotNull(results.getEmotion());
    assertNotNull(results.getEmotion().getDocument());
    assertNotNull(results.getEmotion().getDocument().getEmotion());
    EmotionScores scores = results.getEmotion().getDocument().getEmotion();
    assertNotNull(scores.getAnger());
    assertNotNull(scores.getDisgust());
    assertNotNull(scores.getFear());
    assertNotNull(scores.getJoy());
    assertNotNull(scores.getSadness());
    assertNull(results.getEmotion().getTargets());
}
Also used : AnalyzeOptions(com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.AnalyzeOptions) EmotionScores(com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.EmotionScores) AnalysisResults(com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.AnalysisResults) EmotionOptions(com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.EmotionOptions) Features(com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.Features) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Example 8 with AnalysisResults

use of com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.AnalysisResults in project java-sdk by watson-developer-cloud.

the class NaturalLanguageUnderstanding method analyze.

/**
 * Analyze text, HTML, or a public webpage.
 *
 * Analyzes text, HTML, or a public webpage with one or more text analysis features.
 *
 * @param analyzeOptions the {@link AnalyzeOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link AnalysisResults}
 */
public ServiceCall<AnalysisResults> analyze(AnalyzeOptions analyzeOptions) {
    Validator.notNull(analyzeOptions, "analyzeOptions cannot be null");
    String[] pathSegments = { "v1/analyze" };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
    builder.query(VERSION, versionDate);
    final JsonObject contentJson = new JsonObject();
    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());
    }
    contentJson.add("features", GsonSingleton.getGson().toJsonTree(analyzeOptions.features()));
    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);
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(AnalysisResults.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) AnalysisResults(com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.AnalysisResults) JsonObject(com.google.gson.JsonObject)

Example 9 with AnalysisResults

use of com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.AnalysisResults in project java-sdk by watson-developer-cloud.

the class NaturalLanguageUnderstandingIT method analyzeTextForConceptsIsSuccessful.

/**
 * Analyze given test input text for concepts.
 *
 * @throws Exception the exception
 */
@Test
public void analyzeTextForConceptsIsSuccessful() throws Exception {
    String text = "In remote corners of the world, citizens are demanding respect" + " for the dignity of all people no matter their gender, or race, or religion, or disability," + " or sexual orientation, and those who deny others dignity are subject to public reproach." + " An explosion of social media has given ordinary people more ways to express themselves," + " and has raised people's expectations for those of us in power. Indeed, our international" + " order has been so successful that we take it as a given that great powers no longer" + " fight world wars; that the end of the Cold War lifted the shadow of nuclear Armageddon;" + " that the battlefields of Europe have been replaced by peaceful union; that China and India" + " remain on a path of remarkable growth.";
    ConceptsOptions concepts = new ConceptsOptions.Builder().limit(5).build();
    Features features = new Features.Builder().concepts(concepts).build();
    AnalyzeOptions parameters = new AnalyzeOptions.Builder().text(text).features(features).returnAnalyzedText(true).build();
    AnalysisResults results = service.analyze(parameters).execute();
    assertNotNull(results);
    assertNotNull(results.getAnalyzedText());
    assertNotNull(results.getConcepts());
    for (ConceptsResult concept : results.getConcepts()) {
        assertNotNull(concept.getText());
        assertNotNull(concept.getDbpediaResource());
        assertNotNull(concept.getRelevance());
    }
}
Also used : AnalyzeOptions(com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.AnalyzeOptions) AnalysisResults(com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.AnalysisResults) ConceptsOptions(com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.ConceptsOptions) Features(com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.Features) ConceptsResult(com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.ConceptsResult) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Example 10 with AnalysisResults

use of com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.AnalysisResults in project java-sdk by watson-developer-cloud.

the class NaturalLanguageUnderstandingIT method analyzeHtmlForConceptsIsSuccessful.

/**
 * Analyze test HTML for concepts.
 *
 * @throws Exception the exception
 */
@Test
public void analyzeHtmlForConceptsIsSuccessful() throws Exception {
    String testHtmlFileName = "src/test/resources/natural_language_understanding/testArticle.html";
    String html = getStringFromInputStream(new FileInputStream(testHtmlFileName));
    ConceptsOptions concepts = new ConceptsOptions.Builder().build();
    Features features = new Features.Builder().concepts(concepts).build();
    AnalyzeOptions parameters = new AnalyzeOptions.Builder().html(html).features(features).returnAnalyzedText(true).build();
    AnalysisResults results = service.analyze(parameters).execute();
    assertNotNull(results);
    assertNotNull(results.getAnalyzedText());
    assertNotNull(results.getConcepts());
    for (ConceptsResult concept : results.getConcepts()) {
        assertNotNull(concept.getText());
        assertNotNull(concept.getDbpediaResource());
        assertNotNull(concept.getRelevance());
    }
}
Also used : AnalyzeOptions(com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.AnalyzeOptions) AnalysisResults(com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.AnalysisResults) ConceptsOptions(com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.ConceptsOptions) Features(com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.Features) ConceptsResult(com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.ConceptsResult) FileInputStream(java.io.FileInputStream) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest) Test(org.junit.Test)

Aggregations

AnalysisResults (com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.AnalysisResults)19 AnalyzeOptions (com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.AnalyzeOptions)18 Features (com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.Features)18 Test (org.junit.Test)18 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)16 ConceptsOptions (com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.ConceptsOptions)6 FileInputStream (java.io.FileInputStream)4 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)2 CategoriesOptions (com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.CategoriesOptions)2 ConceptsResult (com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.ConceptsResult)2 EntitiesOptions (com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.EntitiesOptions)2 EntitiesResult (com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.EntitiesResult)2 SentimentOptions (com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.SentimentOptions)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 JsonObject (com.google.gson.JsonObject)1 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)1 Author (com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.Author)1 CategoriesResult (com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.CategoriesResult)1 EmotionOptions (com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.EmotionOptions)1 EmotionScores (com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.EmotionScores)1