/** * Finds entities, similar to * [AnalyzeEntities][google.cloud.language.v1.LanguageService.AnalyzeEntities] in the text and * analyzes sentiment associated with each entity and its mentions. * * <p>Sample code: * * <pre><code> * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) { * Document document = Document.newBuilder().build(); * EncodingType encodingType = EncodingType.NONE; * AnalyzeEntitySentimentResponse response = languageServiceClient.analyzeEntitySentiment(document, encodingType); * } * </code></pre> * * @param document Input document. * @param encodingType The encoding type used by the API to calculate offsets. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final AnalyzeEntitySentimentResponse analyzeEntitySentiment( Document document, EncodingType encodingType) { AnalyzeEntitySentimentRequest request = AnalyzeEntitySentimentRequest.newBuilder() .setDocument(document) .setEncodingType(encodingType) .build(); return analyzeEntitySentiment(request); }
@Test @SuppressWarnings("all") public void analyzeEntitySentimentExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockLanguageService.addException(exception); try { Document document = Document.newBuilder().build(); EncodingType encodingType = EncodingType.NONE; client.analyzeEntitySentiment(document, encodingType); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void analyzeEntitySentimentTest() { String language = "language-1613589672"; AnalyzeEntitySentimentResponse expectedResponse = AnalyzeEntitySentimentResponse.newBuilder().setLanguage(language).build(); mockLanguageService.addResponse(expectedResponse); Document document = Document.newBuilder().build(); EncodingType encodingType = EncodingType.NONE; AnalyzeEntitySentimentResponse actualResponse = client.analyzeEntitySentiment(document, encodingType); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockLanguageService.getRequests(); Assert.assertEquals(1, actualRequests.size()); AnalyzeEntitySentimentRequest actualRequest = (AnalyzeEntitySentimentRequest) actualRequests.get(0); Assert.assertEquals(document, actualRequest.getDocument()); Assert.assertEquals(encodingType, actualRequest.getEncodingType()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
/** * Detects the entity sentiments in the string {@code text} using the Language Beta API. */ public static void entitySentimentText(String text) throws Exception { // [START language_entity_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(); 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 language_entity_sentiment_text] }
.build(); AnalyzeEntitySentimentResponse response = language.analyzeEntitySentiment(request);
/** * Finds entities, similar to * [AnalyzeEntities][google.cloud.language.v1.LanguageService.AnalyzeEntities] in the text and * analyzes sentiment associated with each entity and its mentions. * * <p>Sample code: * * <pre><code> * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) { * Document document = Document.newBuilder().build(); * EncodingType encodingType = EncodingType.NONE; * AnalyzeEntitySentimentResponse response = languageServiceClient.analyzeEntitySentiment(document, encodingType); * } * </code></pre> * * @param document Input document. * @param encodingType The encoding type used by the API to calculate offsets. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final AnalyzeEntitySentimentResponse analyzeEntitySentiment( Document document, EncodingType encodingType) { AnalyzeEntitySentimentRequest request = AnalyzeEntitySentimentRequest.newBuilder() .setDocument(document) .setEncodingType(encodingType) .build(); return analyzeEntitySentiment(request); }