Search in sources :

Example 1 with ToneOptions

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

the class ToneAnalyzerIT method testToneExample.

/**
 * Test ToneExample.
 */
@Test
public void testToneExample() {
    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! " + "We need to acknowledge and fix our sales challenges. " + "We can’t blame the economy for our lack of execution! " + "We are missing critical sales opportunities. " + "Our product is in no way inferior to the competitor products. " + "Our clients are hungry for analytical tools to improve their " + "business outcomes. Economy has nothing to do with it.";
    // Call the service and get the tone
    ToneOptions tonOptions = new ToneOptions.Builder().text(text).build();
    ToneAnalysis tone = service.tone(tonOptions).execute();
    System.out.println(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 2 with ToneOptions

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

the class AssistantToneAnalyzerIntegrationExample method main.

public static void main(String[] args) throws Exception {
    // instantiate the assistant service
    Assistant assistantService = new Assistant("2018-02-16");
    assistantService.setUsernameAndPassword("<username>", "<password>");
    // instantiate the tone analyzer service
    ToneAnalyzer toneService = new ToneAnalyzer("2017-09-21");
    toneService.setUsernameAndPassword("<username>", "<password>");
    // workspace id
    String workspaceId = "<workspace-id>";
    // maintain history in the context variable - will add a history variable to
    // each of the emotion, social
    // and language tones
    boolean maintainHistory = false;
    /**
     * Input for the Assistant service: input (String): an input string (the user's conversation turn) and context
     * (Map<String,Object>: any context that needs to be maintained - either added by the client app or passed in the
     * response from the Assistant service on the previous conversation turn.
     */
    String input = "I am happy";
    Map<String, Object> context = new HashMap<>();
    // UPDATE CONTEXT HERE IF CONTINUING AN ONGOING CONVERSATION
    // set local context variable to the context from the last response from the
    // Assistant Service
    // (see the getContext() method of the MessageResponse class in
    // com.ibm.watson.developer_cloud.assistant.v1.model)
    // async call to Tone Analyzer
    ToneOptions toneOptions = new ToneOptions.Builder().text(input).build();
    toneService.tone(toneOptions).enqueue(new ServiceCallback<ToneAnalysis>() {

        @Override
        public void onResponse(ToneAnalysis toneResponsePayload) {
            // update context with the tone data returned by the Tone Analyzer
            ToneDetection.updateUserTone(context, toneResponsePayload, maintainHistory);
            // call Assistant Service with the input and tone-aware context
            MessageOptions messageOptions = new MessageOptions.Builder(workspaceId).input(new InputData.Builder(input).build()).context(context).build();
            assistantService.message(messageOptions).enqueue(new ServiceCallback<MessageResponse>() {

                @Override
                public void onResponse(MessageResponse response) {
                    System.out.println(response);
                }

                @Override
                public void onFailure(Exception e) {
                }
            });
        }

        @Override
        public void onFailure(Exception e) {
        }
    });
}
Also used : ToneAnalysis(com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneAnalysis) HashMap(java.util.HashMap) MessageResponse(com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse) ServiceCallback(com.ibm.watson.developer_cloud.http.ServiceCallback) ToneAnalyzer(com.ibm.watson.developer_cloud.tone_analyzer.v3.ToneAnalyzer) MessageOptions(com.ibm.watson.developer_cloud.assistant.v1.model.MessageOptions) Assistant(com.ibm.watson.developer_cloud.assistant.v1.Assistant)

Example 3 with ToneOptions

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

the class ToneAnalyzerExample method main.

public static void main(String[] args) {
    ToneAnalyzer service = new ToneAnalyzer("2017-09-21");
    service.setUsernameAndPassword("<username>", "<password>");
    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! " + "We need to acknowledge and fix our sales challenges. " + "We can’t blame the economy for our lack of execution! " + "We are missing critical sales opportunities. " + "Our product is in no way inferior to the competitor products. " + "Our clients are hungry for analytical tools to improve their " + "business outcomes. Economy has nothing to do with it.";
    // Call the service and get the tone
    ToneOptions toneOptions = new ToneOptions.Builder().text(text).build();
    ToneAnalysis tone = service.tone(toneOptions).execute();
    System.out.println(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)

Example 4 with ToneOptions

use of com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneOptions 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 5 with ToneOptions

use of com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneOptions 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)

Aggregations

ToneAnalysis (com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneAnalysis)8 ToneOptions (com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneOptions)6 Test (org.junit.Test)5 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)3 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)2 Assistant (com.ibm.watson.developer_cloud.assistant.v1.Assistant)1 MessageOptions (com.ibm.watson.developer_cloud.assistant.v1.model.MessageOptions)1 MessageResponse (com.ibm.watson.developer_cloud.assistant.v1.model.MessageResponse)1 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)1 ServiceCallback (com.ibm.watson.developer_cloud.http.ServiceCallback)1 ToneAnalyzer (com.ibm.watson.developer_cloud.tone_analyzer.v3.ToneAnalyzer)1 HashMap (java.util.HashMap)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1