Search in sources :

Example 11 with LanguageServiceClient

use of com.google.cloud.language.spi.v1.LanguageServiceClient 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 12 with LanguageServiceClient

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

Example 13 with LanguageServiceClient

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

the class AnalyzeBeta method classifyText.

/**
 * Detects categories in text using the Language Beta API.
 */
public static void classifyText(String text) throws Exception {
    // Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient
    try (LanguageServiceClient language = LanguageServiceClient.create()) {
        // set content to the text string
        Document doc = Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build();
        ClassifyTextRequest request = ClassifyTextRequest.newBuilder().setDocument(doc).build();
        // detect categories in the given text
        ClassifyTextResponse response = language.classifyText(request);
        for (ClassificationCategory category : response.getCategoriesList()) {
            System.out.printf("Category name : %s, Confidence : %.3f\n", category.getName(), category.getConfidence());
        }
    }
// [END classify_text]
}
Also used : LanguageServiceClient(com.google.cloud.language.v1beta2.LanguageServiceClient) ClassifyTextResponse(com.google.cloud.language.v1beta2.ClassifyTextResponse) Document(com.google.cloud.language.v1beta2.Document) ClassificationCategory(com.google.cloud.language.v1beta2.ClassificationCategory) ClassifyTextRequest(com.google.cloud.language.v1beta2.ClassifyTextRequest)

Example 14 with LanguageServiceClient

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

the class QuickstartSample method main.

public static void main(String... args) throws Exception {
    // Instantiates a client
    try (LanguageServiceClient language = LanguageServiceClient.create()) {
        // The text to analyze
        String text = "Hello, world!";
        Document doc = Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build();
        // Detects the sentiment of the text
        Sentiment sentiment = language.analyzeSentiment(doc).getDocumentSentiment();
        System.out.printf("Text: %s%n", text);
        System.out.printf("Sentiment: %s, %s%n", sentiment.getScore(), sentiment.getMagnitude());
    }
}
Also used : LanguageServiceClient(com.google.cloud.language.v1.LanguageServiceClient) Document(com.google.cloud.language.v1.Document) Sentiment(com.google.cloud.language.v1.Sentiment)

Example 15 with LanguageServiceClient

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

the class AnalyzeSentiment method main.

public static void main(String... args) throws Exception {
    // Instantiates a client
    LanguageServiceClient language = LanguageServiceClient.create();
    // The text to analyze
    String[] texts = { "I love this!", "I hate this!" };
    for (String text : texts) {
        Document doc = Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build();
        // Detects the sentiment of the text
        Sentiment sentiment = language.analyzeSentiment(doc).getDocumentSentiment();
        System.out.printf("Text: \"%s\"%n", text);
        System.out.printf("Sentiment: score = %s, magnitude = %s%n", sentiment.getScore(), sentiment.getMagnitude());
    }
}
Also used : LanguageServiceClient(com.google.cloud.language.v1.LanguageServiceClient) Document(com.google.cloud.language.v1.Document) Sentiment(com.google.cloud.language.v1.Sentiment)

Aggregations

Document (com.google.cloud.language.v1.Document)12 LanguageServiceClient (com.google.cloud.language.v1.LanguageServiceClient)12 Entity (com.google.cloud.language.v1.Entity)4 EntityMention (com.google.cloud.language.v1.EntityMention)4 Sentiment (com.google.cloud.language.v1.Sentiment)4 Document (com.google.cloud.language.v1beta2.Document)3 LanguageServiceClient (com.google.cloud.language.v1beta2.LanguageServiceClient)3 AnalyzeEntitiesRequest (com.google.cloud.language.v1.AnalyzeEntitiesRequest)2 AnalyzeEntitiesResponse (com.google.cloud.language.v1.AnalyzeEntitiesResponse)2 AnalyzeEntitySentimentRequest (com.google.cloud.language.v1.AnalyzeEntitySentimentRequest)2 AnalyzeEntitySentimentResponse (com.google.cloud.language.v1.AnalyzeEntitySentimentResponse)2 AnalyzeSentimentResponse (com.google.cloud.language.v1.AnalyzeSentimentResponse)2 AnalyzeSyntaxRequest (com.google.cloud.language.v1.AnalyzeSyntaxRequest)2 AnalyzeSyntaxResponse (com.google.cloud.language.v1.AnalyzeSyntaxResponse)2 ClassificationCategory (com.google.cloud.language.v1.ClassificationCategory)2 ClassifyTextRequest (com.google.cloud.language.v1.ClassifyTextRequest)2 ClassifyTextResponse (com.google.cloud.language.v1.ClassifyTextResponse)2 Token (com.google.cloud.language.v1.Token)2 ClassificationCategory (com.google.cloud.language.v1beta2.ClassificationCategory)2 ClassifyTextRequest (com.google.cloud.language.v1beta2.ClassifyTextRequest)2