Search in sources :

Example 1 with AnalyzeSentimentResponse

use of com.google.cloud.language.v1.AnalyzeSentimentResponse in project google-cloud-java by GoogleCloudPlatform.

the class LanguageServiceClientTest method analyzeSentimentTest.

@Test
@SuppressWarnings("all")
public void analyzeSentimentTest() {
    String language = "language-1613589672";
    AnalyzeSentimentResponse expectedResponse = AnalyzeSentimentResponse.newBuilder().setLanguage(language).build();
    mockLanguageService.addResponse(expectedResponse);
    Document document = Document.newBuilder().build();
    AnalyzeSentimentResponse actualResponse = client.analyzeSentiment(document);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockLanguageService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    AnalyzeSentimentRequest actualRequest = (AnalyzeSentimentRequest) actualRequests.get(0);
    Assert.assertEquals(document, actualRequest.getDocument());
}
Also used : AnalyzeSentimentResponse(com.google.cloud.language.v1beta2.AnalyzeSentimentResponse) Document(com.google.cloud.language.v1beta2.Document) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) AnalyzeSentimentRequest(com.google.cloud.language.v1beta2.AnalyzeSentimentRequest) Test(org.junit.Test)

Example 2 with AnalyzeSentimentResponse

use of com.google.cloud.language.v1.AnalyzeSentimentResponse in project google-cloud-java by GoogleCloudPlatform.

the class LanguageServiceClientTest method analyzeSentimentTest.

@Test
@SuppressWarnings("all")
public void analyzeSentimentTest() {
    String language = "language-1613589672";
    AnalyzeSentimentResponse expectedResponse = AnalyzeSentimentResponse.newBuilder().setLanguage(language).build();
    mockLanguageService.addResponse(expectedResponse);
    Document document = Document.newBuilder().build();
    AnalyzeSentimentResponse actualResponse = client.analyzeSentiment(document);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockLanguageService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    AnalyzeSentimentRequest actualRequest = (AnalyzeSentimentRequest) actualRequests.get(0);
    Assert.assertEquals(document, actualRequest.getDocument());
}
Also used : AnalyzeSentimentResponse(com.google.cloud.language.v1.AnalyzeSentimentResponse) Document(com.google.cloud.language.v1.Document) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) AnalyzeSentimentRequest(com.google.cloud.language.v1.AnalyzeSentimentRequest) Test(org.junit.Test)

Example 3 with AnalyzeSentimentResponse

use of com.google.cloud.language.v1.AnalyzeSentimentResponse in project java-docs-samples by GoogleCloudPlatform.

the class Analyze method analyzeSentimentText.

/**
 * Identifies the sentiment in the string {@code text}.
 */
public static Sentiment analyzeSentimentText(String text) throws Exception {
    // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
    try (LanguageServiceClient language = LanguageServiceClient.create()) {
        Document doc = Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build();
        AnalyzeSentimentResponse response = language.analyzeSentiment(doc);
        Sentiment sentiment = response.getDocumentSentiment();
        if (sentiment == null) {
            System.out.println("No sentiment found");
        } else {
            System.out.printf("Sentiment magnitude: %.3f\n", sentiment.getMagnitude());
            System.out.printf("Sentiment score: %.3f\n", sentiment.getScore());
        }
        return sentiment;
    }
// [END analyze_sentiment_text]
}
Also used : LanguageServiceClient(com.google.cloud.language.v1.LanguageServiceClient) AnalyzeSentimentResponse(com.google.cloud.language.v1.AnalyzeSentimentResponse) Document(com.google.cloud.language.v1.Document) Sentiment(com.google.cloud.language.v1.Sentiment)

Example 4 with AnalyzeSentimentResponse

use of com.google.cloud.language.v1.AnalyzeSentimentResponse in project java-docs-samples by GoogleCloudPlatform.

the class Analyze method analyzeSentimentFile.

/**
 * Gets {@link Sentiment} from the contents of the GCS hosted file.
 */
public static Sentiment analyzeSentimentFile(String gcsUri) throws Exception {
    // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
    try (LanguageServiceClient language = LanguageServiceClient.create()) {
        Document doc = Document.newBuilder().setGcsContentUri(gcsUri).setType(Type.PLAIN_TEXT).build();
        AnalyzeSentimentResponse response = language.analyzeSentiment(doc);
        Sentiment sentiment = response.getDocumentSentiment();
        if (sentiment == null) {
            System.out.println("No sentiment found");
        } else {
            System.out.printf("Sentiment magnitude : %.3f\n", sentiment.getMagnitude());
            System.out.printf("Sentiment score : %.3f\n", sentiment.getScore());
        }
        return sentiment;
    }
// [END analyze_sentiment_file]
}
Also used : LanguageServiceClient(com.google.cloud.language.v1.LanguageServiceClient) AnalyzeSentimentResponse(com.google.cloud.language.v1.AnalyzeSentimentResponse) Document(com.google.cloud.language.v1.Document) Sentiment(com.google.cloud.language.v1.Sentiment)

Example 5 with AnalyzeSentimentResponse

use of com.google.cloud.language.v1.AnalyzeSentimentResponse in project java-docs-samples by GoogleCloudPlatform.

the class AnalyzeBeta method analyzeSentimentText.

/**
 * Detects sentiments from the string {@code text}.
 */
public static Sentiment analyzeSentimentText(String text, String lang) throws Exception {
    // Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient
    try (LanguageServiceClient language = LanguageServiceClient.create()) {
        // NL auto-detects the language, if not provided
        Document doc;
        if (lang != null) {
            doc = Document.newBuilder().setLanguage(lang).setContent(text).setType(Type.PLAIN_TEXT).build();
        } else {
            doc = Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build();
        }
        AnalyzeSentimentResponse response = language.analyzeSentiment(doc);
        Sentiment sentiment = response.getDocumentSentiment();
        if (sentiment != null) {
            System.out.println("Found sentiment.");
            System.out.printf("\tMagnitude: %.3f\n", sentiment.getMagnitude());
            System.out.printf("\tScore: %.3f\n", sentiment.getScore());
        } else {
            System.out.println("No sentiment found");
        }
        return sentiment;
    }
// [END beta_sentiment_text]
}
Also used : LanguageServiceClient(com.google.cloud.language.v1beta2.LanguageServiceClient) AnalyzeSentimentResponse(com.google.cloud.language.v1beta2.AnalyzeSentimentResponse) Document(com.google.cloud.language.v1beta2.Document) Sentiment(com.google.cloud.language.v1beta2.Sentiment)

Aggregations

AnalyzeSentimentResponse (com.google.cloud.language.v1.AnalyzeSentimentResponse)3 Document (com.google.cloud.language.v1.Document)3 LanguageServiceClient (com.google.cloud.language.v1.LanguageServiceClient)2 Sentiment (com.google.cloud.language.v1.Sentiment)2 AnalyzeSentimentResponse (com.google.cloud.language.v1beta2.AnalyzeSentimentResponse)2 Document (com.google.cloud.language.v1beta2.Document)2 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)2 Test (org.junit.Test)2 AnalyzeSentimentRequest (com.google.cloud.language.v1.AnalyzeSentimentRequest)1 AnalyzeSentimentRequest (com.google.cloud.language.v1beta2.AnalyzeSentimentRequest)1 LanguageServiceClient (com.google.cloud.language.v1beta2.LanguageServiceClient)1 Sentiment (com.google.cloud.language.v1beta2.Sentiment)1