Search in sources :

Example 6 with ToneAnalysis

use of com.ibm.watson.tone_analyzer.v3.model.ToneAnalysis in project java-sdk by watson-developer-cloud.

the class ToneAnalyzerIT method testtoneFromHtml.

/**
 * Test get tone from html.
 */
@Test
public void testtoneFromHtml() {
    ToneOptions options = new ToneOptions.Builder().html(text).build();
    ToneAnalysis tone = service.tone(options).execute().getResult();
    assertToneAnalysis(tone);
}
Also used : ToneAnalysis(com.ibm.watson.tone_analyzer.v3.model.ToneAnalysis) ToneOptions(com.ibm.watson.tone_analyzer.v3.model.ToneOptions) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 7 with ToneAnalysis

use of com.ibm.watson.tone_analyzer.v3.model.ToneAnalysis in project java-sdk by watson-developer-cloud.

the class ToneAnalyzer method tone.

/**
 * Analyze general tone.
 *
 * Use the general purpose endpoint to analyze the tone of your input content. The service analyzes the content for
 * emotional and language tones. The method always analyzes the tone of the full document; by default, it also
 * analyzes the tone of each individual sentence of the content. You can submit no more than 128 KB of total input
 * content and no more than 1000 individual sentences in JSON, plain text, or HTML format. The service analyzes the
 * first 1000 sentences for document-level analysis and only the first 100 sentences for sentence-level analysis. Per
 * the JSON specification, the default character encoding for JSON content is effectively always UTF-8; per the HTTP
 * specification, the default encoding for plain text and HTML is ISO-8859-1 (effectively, the ASCII character set).
 * When specifying a content type of plain text or HTML, include the `charset` parameter to indicate the character
 * encoding of the input text; for example: `Content-Type: text/plain;charset=utf-8`. For `text/html`, the service
 * removes HTML tags and analyzes only the textual content.
 *
 * @param toneOptions the {@link ToneOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link ToneAnalysis}
 */
public ServiceCall<ToneAnalysis> tone(ToneOptions toneOptions) {
    Validator.notNull(toneOptions, "toneOptions cannot be null");
    String[] pathSegments = { "v3/tone" };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
    builder.query(VERSION, versionDate);
    builder.header("Content-Type", toneOptions.contentType());
    if (toneOptions.contentLanguage() != null) {
        builder.header("Content-Language", toneOptions.contentLanguage());
    }
    if (toneOptions.acceptLanguage() != null) {
        builder.header("Accept-Language", toneOptions.acceptLanguage());
    }
    if (toneOptions.sentences() != null) {
        builder.query("sentences", String.valueOf(toneOptions.sentences()));
    }
    if (toneOptions.tones() != null) {
        builder.query("tones", RequestUtils.join(toneOptions.tones(), ","));
    }
    if (toneOptions.contentType().equalsIgnoreCase(ToneOptions.ContentType.APPLICATION_JSON)) {
        builder.bodyJson(GsonSingleton.getGson().toJsonTree(toneOptions.toneInput()).getAsJsonObject());
    } else {
        builder.bodyContent(toneOptions.body(), toneOptions.contentType());
    }
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(ToneAnalysis.class));
}
Also used : ToneAnalysis(com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneAnalysis) RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder)

Example 8 with ToneAnalysis

use of com.ibm.watson.tone_analyzer.v3.model.ToneAnalysis in project java-sdk by watson-developer-cloud.

the class ToneAnalyzerIT method testtoneFromHtml.

/**
 * Test get tone from html.
 */
@Test
public void testtoneFromHtml() {
    ToneOptions options = new ToneOptions.Builder().html(text).build();
    ToneAnalysis tone = service.tone(options).execute();
    assertToneAnalysis(tone);
}
Also used : ToneAnalysis(com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneAnalysis) ToneOptions(com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneOptions) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 9 with ToneAnalysis

use of com.ibm.watson.tone_analyzer.v3.model.ToneAnalysis in project java-sdk by watson-developer-cloud.

the class ToneAnalyzerIT method testtoneFromText.

/**
 * Test get tone from text.
 */
@Test
public void testtoneFromText() {
    ToneOptions options = new ToneOptions.Builder().text(text).addTone(ToneOptions.Tone.EMOTION).addTone(ToneOptions.Tone.LANGUAGE).addTone(ToneOptions.Tone.SOCIAL).build();
    ToneAnalysis tone = service.tone(options).execute();
    assertToneAnalysis(tone);
}
Also used : ToneAnalysis(com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneAnalysis) ToneOptions(com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneOptions) Test(org.junit.Test) WatsonServiceTest(com.ibm.watson.developer_cloud.WatsonServiceTest)

Example 10 with ToneAnalysis

use of com.ibm.watson.tone_analyzer.v3.model.ToneAnalysis in project java-sdk by watson-developer-cloud.

the class ToneAnalyzerTest method testtones.

/**
 * Test get tones.
 *
 * @throws InterruptedException the interrupted exception
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Test
public void testtones() throws InterruptedException, IOException {
    String text = "I know the times are difficult! Our sales have been " + "disappointing for the past three quarters for our data analytics " + "product suite. We have a competitive data analytics product " + "suite in the industry. But we need to do our job selling it! ";
    ToneAnalysis mockResponse = loadFixture(FIXTURE, ToneAnalysis.class);
    server.enqueue(jsonResponse(mockResponse));
    server.enqueue(jsonResponse(mockResponse));
    server.enqueue(jsonResponse(mockResponse));
    // execute request
    ToneOptions toneOptions = new ToneOptions.Builder().html(text).build();
    ToneAnalysis serviceResponse = service.tone(toneOptions).execute();
    // first request
    RecordedRequest request = server.takeRequest();
    String path = StringUtils.join(TONE_PATH, "?", VERSION_DATE, "=", VERSION_DATE_VALUE);
    assertEquals(path, request.getPath());
    assertNotNull(request.getHeader(HttpHeaders.AUTHORIZATION));
    assertEquals(serviceResponse, mockResponse);
    assertEquals(HttpMediaType.APPLICATION_JSON, request.getHeader(HttpHeaders.ACCEPT));
    // second request
    serviceResponse = service.tone(new ToneOptions.Builder().html(text).build()).execute();
    request = server.takeRequest();
    assertEquals(path, request.getPath());
    assertTrue(request.getHeader(HttpHeaders.CONTENT_TYPE).startsWith(HttpMediaType.TEXT_HTML));
    // third request
    ToneOptions toneOptions1 = new ToneOptions.Builder().html(text).addTone(ToneOptions.Tone.EMOTION).addTone(ToneOptions.Tone.LANGUAGE).addTone(ToneOptions.Tone.SOCIAL).build();
    serviceResponse = service.tone(toneOptions1).execute();
    request = server.takeRequest();
    path = path + "&tones=emotion,language,social";
    assertEquals(path, request.getPath());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) ToneAnalysis(com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneAnalysis) ToneOptions(com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneOptions) Test(org.junit.Test) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest)

Aggregations

ToneAnalysis (com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneAnalysis)8 Test (org.junit.Test)8 ToneAnalysis (com.ibm.watson.tone_analyzer.v3.model.ToneAnalysis)7 ToneOptions (com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneOptions)6 ToneOptions (com.ibm.watson.tone_analyzer.v3.model.ToneOptions)6 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)3 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)3 Authenticator (com.ibm.cloud.sdk.core.security.Authenticator)2 IamAuthenticator (com.ibm.cloud.sdk.core.security.IamAuthenticator)2 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)2 HashMap (java.util.HashMap)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)1 Response (com.ibm.cloud.sdk.core.http.Response)1 ServiceCallback (com.ibm.cloud.sdk.core.http.ServiceCallback)1 Assistant (com.ibm.watson.assistant.v1.Assistant)1 Context (com.ibm.watson.assistant.v1.model.Context)1 MessageInput (com.ibm.watson.assistant.v1.model.MessageInput)1 MessageOptions (com.ibm.watson.assistant.v1.model.MessageOptions)1 MessageResponse (com.ibm.watson.assistant.v1.model.MessageResponse)1