/** * Analyzes the sentiment of the provided text. * * <p>Sample code: * * <pre><code> * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) { * Document document = Document.newBuilder().build(); * AnalyzeSentimentResponse response = languageServiceClient.analyzeSentiment(document); * } * </code></pre> * * @param document Input document. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final AnalyzeSentimentResponse analyzeSentiment(Document document) { AnalyzeSentimentRequest request = AnalyzeSentimentRequest.newBuilder().setDocument(document).build(); return analyzeSentiment(request); }
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()); } } }
public static void executeNoCatch() throws Exception { try (LanguageServiceClient client = LanguageServiceClient.create()) { String content = "Hello, world!"; Document.Type type = Document.Type.PLAIN_TEXT; Document document = Document.newBuilder().setContent(content).setType(type).build(); AnalyzeSentimentResponse response = client.analyzeSentiment(document); } }
@Test @SuppressWarnings("all") public void analyzeSentimentExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockLanguageService.addException(exception); try { Document document = Document.newBuilder().build(); client.analyzeSentiment(document); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@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()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
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()); } } }
/** * 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] }
/** * Identifies the sentiment in the string {@code text}. */ public static Sentiment analyzeSentimentText(String text) throws Exception { // [START language_sentiment_text] // 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 language_sentiment_text] }
@Override public void process(InputStream inputStream) throws IOException { StringWriter writer = new StringWriter(); IOUtils.copy(inputStream, writer, "UTF-8"); String flowFileString = writer.toString(); Document doc = Document.newBuilder() .setContent(flowFileString).setType(Document.Type.PLAIN_TEXT).build(); result.set(languageClient.analyzeSentiment(doc)); } });
/** * Analyzes the sentiment of the provided text. * * <p>Sample code: * * <pre><code> * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) { * Document document = Document.newBuilder().build(); * AnalyzeSentimentResponse response = languageServiceClient.analyzeSentiment(document); * } * </code></pre> * * @param document Input document. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final AnalyzeSentimentResponse analyzeSentiment(Document document) { AnalyzeSentimentRequest request = AnalyzeSentimentRequest.newBuilder().setDocument(document).build(); return analyzeSentiment(request); }