Search in sources :

Example 6 with Sentiment

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

the class AnalyzeIT method analyzeSentimentText_returnPositive.

@Test
public void analyzeSentimentText_returnPositive() throws Exception {
    Sentiment sentiment = Analyze.analyzeSentimentText("Tom Cruise is one of the finest actors in hollywood and a great star!");
    assertThat(sentiment.getMagnitude()).isGreaterThan(0.0F);
    assertThat(sentiment.getScore()).isGreaterThan(0.0F);
}
Also used : Sentiment(com.google.cloud.language.v1.Sentiment) Test(org.junit.Test)

Example 7 with Sentiment

use of com.google.cloud.language.v1.Sentiment 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 8 with Sentiment

use of com.google.cloud.language.v1.Sentiment 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 9 with Sentiment

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

the class Analyze method entitySentimentText.

/**
 * Detects the entity sentiments in the string {@code text} using the Language Beta API.
 */
public static void entitySentimentText(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();
        AnalyzeEntitySentimentRequest request = AnalyzeEntitySentimentRequest.newBuilder().setDocument(doc).setEncodingType(EncodingType.UTF16).build();
        // detect entity sentiments in the given string
        AnalyzeEntitySentimentResponse response = language.analyzeEntitySentiment(request);
        // Print the response
        for (Entity entity : response.getEntitiesList()) {
            System.out.printf("Entity: %s\n", entity.getName());
            System.out.printf("Salience: %.3f\n", entity.getSalience());
            System.out.printf("Sentiment : %s\n", entity.getSentiment());
            for (EntityMention mention : entity.getMentionsList()) {
                System.out.printf("Begin offset: %d\n", mention.getText().getBeginOffset());
                System.out.printf("Content: %s\n", mention.getText().getContent());
                System.out.printf("Magnitude: %.3f\n", mention.getSentiment().getMagnitude());
                System.out.printf("Sentiment score : %.3f\n", mention.getSentiment().getScore());
                System.out.printf("Type: %s\n\n", mention.getType());
            }
        }
    }
// [END entity_sentiment_text]
}
Also used : LanguageServiceClient(com.google.cloud.language.v1.LanguageServiceClient) Entity(com.google.cloud.language.v1.Entity) EntityMention(com.google.cloud.language.v1.EntityMention) AnalyzeEntitySentimentRequest(com.google.cloud.language.v1.AnalyzeEntitySentimentRequest) Document(com.google.cloud.language.v1.Document) AnalyzeEntitySentimentResponse(com.google.cloud.language.v1.AnalyzeEntitySentimentResponse)

Example 10 with Sentiment

use of com.google.cloud.language.v1.Sentiment 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

Sentiment (com.google.cloud.language.v1.Sentiment)9 Document (com.google.cloud.language.v1.Document)6 LanguageServiceClient (com.google.cloud.language.v1.LanguageServiceClient)6 Test (org.junit.Test)6 AnalyzeEntitySentimentRequest (com.google.cloud.language.v1.AnalyzeEntitySentimentRequest)2 AnalyzeEntitySentimentResponse (com.google.cloud.language.v1.AnalyzeEntitySentimentResponse)2 AnalyzeSentimentResponse (com.google.cloud.language.v1.AnalyzeSentimentResponse)2 Entity (com.google.cloud.language.v1.Entity)2 EntityMention (com.google.cloud.language.v1.EntityMention)2 Sentiment (com.google.cloud.language.v1beta2.Sentiment)2 AnalyzeSentimentResponse (com.google.cloud.language.v1beta2.AnalyzeSentimentResponse)1 Document (com.google.cloud.language.v1beta2.Document)1 LanguageServiceClient (com.google.cloud.language.v1beta2.LanguageServiceClient)1