Search in sources :

Example 16 with ProfileOptions

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

the class PersonalityInsightsTest method testGetProfileWithSpanishText.

/**
 * Test get profile with spanish text.
 *
 * @throws InterruptedException the interrupted exception
 */
@Test
public void testGetProfileWithSpanishText() throws InterruptedException {
    final ProfileOptions options = new ProfileOptions.Builder().text(text).contentLanguage(ProfileOptions.ContentLanguage.ES).consumptionPreferences(true).rawScores(true).build();
    server.enqueue(jsonResponse(profile));
    final Profile profile = service.profile(options).execute();
    final RecordedRequest request = server.takeRequest();
    assertEquals(PROFILE_PATH + "?version=2016-10-19&raw_scores=true&consumption_preferences=true", request.getPath());
    assertEquals("POST", request.getMethod());
    assertEquals("es", request.getHeader(HttpHeaders.CONTENT_LANGUAGE));
    assertEquals(HttpMediaType.TEXT.toString(), request.getHeader(HttpHeaders.CONTENT_TYPE));
    assertEquals(text, request.getBody().readUtf8());
    assertNotNull(profile);
    assertEquals(profile, this.profile);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) ProfileOptions(com.ibm.watson.developer_cloud.personality_insights.v3.model.ProfileOptions) Profile(com.ibm.watson.developer_cloud.personality_insights.v3.model.Profile) Test(org.junit.Test) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest)

Example 17 with ProfileOptions

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

the class PersonalityInsightsTest method testProfileBuilders.

/**
 * Test profile options builders.
 */
@Test
public void testProfileBuilders() {
    final ProfileOptions options = new ProfileOptions.Builder().html(text).contentLanguage(ProfileOptions.ContentLanguage.ES).acceptLanguage(ProfileOptions.AcceptLanguage.EN).build();
    final ProfileOptions newOptions = options.newBuilder().build();
    assertEquals(newOptions.body(), text);
    assertEquals(newOptions.contentLanguage(), ProfileOptions.ContentLanguage.ES);
    assertEquals(newOptions.acceptLanguage(), ProfileOptions.AcceptLanguage.EN);
}
Also used : ProfileOptions(com.ibm.watson.developer_cloud.personality_insights.v3.model.ProfileOptions) Test(org.junit.Test) WatsonServiceUnitTest(com.ibm.watson.developer_cloud.WatsonServiceUnitTest)

Example 18 with ProfileOptions

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

the class PersonalityInsights method profile.

/**
 * Get profile.
 *
 * <p>Generates a personality profile for the author of the input text. The service accepts a
 * maximum of 20 MB of input content, but it requires much less text to produce an accurate
 * profile. The service can analyze text in Arabic, English, Japanese, Korean, or Spanish. It can
 * return its results in a variety of languages.
 *
 * <p>**See also:** * [Requesting a
 * profile](https://cloud.ibm.com/docs/personality-insights?topic=personality-insights-input#input)
 * * [Providing sufficient
 * input](https://cloud.ibm.com/docs/personality-insights?topic=personality-insights-input#sufficient)
 *
 * <p>### Content types
 *
 * <p>You can provide input content as plain text (`text/plain`), HTML (`text/html`), or JSON
 * (`application/json`) by specifying the **Content-Type** parameter. The default is `text/plain`.
 * * 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).
 *
 * <p>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`.
 *
 * <p>**See also:** [Specifying request and response
 * formats](https://cloud.ibm.com/docs/personality-insights?topic=personality-insights-input#formats)
 *
 * <p>### Accept types
 *
 * <p>You must request a response as JSON (`application/json`) or comma-separated values
 * (`text/csv`) by specifying the **Accept** parameter. CSV output includes a fixed number of
 * columns. Set the **csv_headers** parameter to `true` to request optional column headers for CSV
 * output.
 *
 * <p>**See also:** * [Understanding a JSON
 * profile](https://cloud.ibm.com/docs/personality-insights?topic=personality-insights-output#output)
 * * [Understanding a CSV
 * profile](https://cloud.ibm.com/docs/personality-insights?topic=personality-insights-outputCSV#outputCSV).
 *
 * @param profileOptions the {@link ProfileOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link Profile}
 */
public ServiceCall<Profile> profile(ProfileOptions profileOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(profileOptions, "profileOptions cannot be null");
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v3/profile"));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("personality_insights", "v3", "profile");
    for (Entry<String, String> header : sdkHeaders.entrySet()) {
        builder.header(header.getKey(), header.getValue());
    }
    builder.header("Accept", "application/json");
    if (profileOptions.contentType() != null) {
        builder.header("Content-Type", profileOptions.contentType());
    }
    if (profileOptions.contentLanguage() != null) {
        builder.header("Content-Language", profileOptions.contentLanguage());
    }
    if (profileOptions.acceptLanguage() != null) {
        builder.header("Accept-Language", profileOptions.acceptLanguage());
    }
    builder.query("version", String.valueOf(this.version));
    if (profileOptions.rawScores() != null) {
        builder.query("raw_scores", String.valueOf(profileOptions.rawScores()));
    }
    if (profileOptions.csvHeaders() != null) {
        builder.query("csv_headers", String.valueOf(profileOptions.csvHeaders()));
    }
    if (profileOptions.consumptionPreferences() != null) {
        builder.query("consumption_preferences", String.valueOf(profileOptions.consumptionPreferences()));
    }
    builder.bodyContent(profileOptions.contentType(), profileOptions.content(), null, profileOptions.body());
    ResponseConverter<Profile> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Profile>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) Profile(com.ibm.watson.personality_insights.v3.model.Profile)

Example 19 with ProfileOptions

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

the class PersonalityInsightsIT method getProfileWithTextAsCSVWithHeaders.

/**
 * Gets the profile with text as a CSV string with headers.
 *
 * @throws Exception the exception
 */
@Test
public void getProfileWithTextAsCSVWithHeaders() throws Exception {
    File file = new File(RESOURCE + "en.txt");
    String englishText = getStringFromInputStream(new FileInputStream(file));
    ProfileOptions options = new ProfileOptions.Builder().text(englishText).csvHeaders(true).build();
    InputStream result = service.profileAsCsv(options).execute().getResult();
    String profileString = CharStreams.toString(new InputStreamReader(result, "UTF-8"));
    Assert.assertNotNull(profileString);
    Assert.assertTrue(profileString.split("\n").length == 2);
}
Also used : ProfileOptions(com.ibm.watson.personality_insights.v3.model.ProfileOptions) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Example 20 with ProfileOptions

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

the class PersonalityInsightsIT method getProfileWithASingleSpanishContentItem.

/**
 * Gets the profile from a single content item in Spanish.
 *
 * @throws Exception the exception
 */
@Test
public void getProfileWithASingleSpanishContentItem() throws Exception {
    File file = new File(RESOURCE + "es.txt");
    String englishText = getStringFromInputStream(new FileInputStream(file));
    ContentItem cItem = new ContentItem.Builder(englishText).language(ContentItem.Language.ES).build();
    Content content = new Content.Builder().contentItems(Collections.singletonList(cItem)).build();
    ProfileOptions options = new ProfileOptions.Builder().content(content).consumptionPreferences(true).rawScores(true).build();
    Profile profile = service.profile(options).execute().getResult();
    assertProfile(profile);
}
Also used : ProfileOptions(com.ibm.watson.personality_insights.v3.model.ProfileOptions) Content(com.ibm.watson.personality_insights.v3.model.Content) File(java.io.File) FileInputStream(java.io.FileInputStream) ContentItem(com.ibm.watson.personality_insights.v3.model.ContentItem) Profile(com.ibm.watson.personality_insights.v3.model.Profile) WatsonServiceTest(com.ibm.watson.common.WatsonServiceTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)18 ProfileOptions (com.ibm.watson.developer_cloud.personality_insights.v3.model.ProfileOptions)12 File (java.io.File)10 FileInputStream (java.io.FileInputStream)10 Profile (com.ibm.watson.developer_cloud.personality_insights.v3.model.Profile)9 ProfileOptions (com.ibm.watson.personality_insights.v3.model.ProfileOptions)9 WatsonServiceTest (com.ibm.watson.common.WatsonServiceTest)7 WatsonServiceTest (com.ibm.watson.developer_cloud.WatsonServiceTest)7 Profile (com.ibm.watson.personality_insights.v3.model.Profile)7 Content (com.ibm.watson.personality_insights.v3.model.Content)5 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)5 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)4 Content (com.ibm.watson.developer_cloud.personality_insights.v3.model.Content)4 ContentItem (com.ibm.watson.personality_insights.v3.model.ContentItem)4 InputStream (java.io.InputStream)3 ContentItem (com.ibm.watson.developer_cloud.personality_insights.v3.model.ContentItem)2 InputStreamReader (java.io.InputStreamReader)2 Date (java.util.Date)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 Test (org.testng.annotations.Test)2