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);
}
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));
}
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);
}
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);
}
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());
}
Aggregations