.setGcsContentUri(gcsUri) .setType(Type.PLAIN_TEXT) .build();
/** * Gets {@link Sentiment} from the contents of the GCS hosted file. */ public static Sentiment analyzeSentimentFile(String gcsUri) throws Exception { // [START language_sentiment_gcs] // 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 language_sentiment_gcs] }
.setGcsContentUri(gcsUri) .setType(Type.PLAIN_TEXT) .build();
.setGcsContentUri(gcsUri) .setType(Type.PLAIN_TEXT) .build();
/** * Detects categories in a GCS hosted file using the Language Beta API. */ public static void classifyFile(String gcsUri) throws Exception { // [START language_classify_gcs] // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient try (LanguageServiceClient language = LanguageServiceClient.create()) { // set the GCS content URI path Document doc = Document.newBuilder() .setGcsContentUri(gcsUri) .setType(Type.PLAIN_TEXT) .build(); ClassifyTextRequest request = ClassifyTextRequest.newBuilder() .setDocument(doc) .build(); // detect categories in the given file ClassifyTextResponse response = language.classifyText(request); for (ClassificationCategory category : response.getCategoriesList()) { System.out.printf("Category name : %s, Confidence : %.3f\n", category.getName(), category.getConfidence()); } } // [END language_classify_gcs] }