Refine search
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()); } } }
/** * * * <pre> * Input document. * </pre> * * <code>.google.cloud.language.v1.Document document = 1;</code> */ public Builder mergeDocument(com.google.cloud.language.v1.Document value) { if (documentBuilder_ == null) { if (document_ != null) { document_ = com.google.cloud.language.v1.Document.newBuilder(document_) .mergeFrom(value) .buildPartial(); } else { document_ = value; } onChanged(); } else { documentBuilder_.mergeFrom(value); } return this; } /**
/** * Detects categories in text using the Language Beta API. */ public static void classifyText(String text) throws Exception { // [START language_classify_text] // Instantiate the Language client com.google.cloud.language.v1.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 language_classify_text] }
/** * 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] }
try (LanguageServiceClient language = LanguageServiceClient.create()) { Document doc = Document.newBuilder() .setContent(text) .setType(Type.PLAIN_TEXT) .build(); AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest.newBuilder() .setDocument(doc) .build(); AnalyzeEntitiesResponse response = language.analyzeEntities(request);
try (LanguageServiceClient language = LanguageServiceClient.create()) { Document doc = Document.newBuilder() .setContent(text) .setType(Type.PLAIN_TEXT) .build(); AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest.newBuilder() .setDocument(doc) .build(); AnalyzeSyntaxResponse response = language.analyzeSyntax(request);
@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 classifyTextExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockLanguageService.addException(exception); try { Document document = Document.newBuilder().build(); client.classifyText(document); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void analyzeSyntaxExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockLanguageService.addException(exception); try { Document document = Document.newBuilder().build(); EncodingType encodingType = EncodingType.NONE; client.analyzeSyntax(document, encodingType); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void analyzeEntitiesExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockLanguageService.addException(exception); try { Document document = Document.newBuilder().build(); EncodingType encodingType = EncodingType.NONE; client.analyzeEntities(document, encodingType); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@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 annotateTextExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockLanguageService.addException(exception); try { Document document = Document.newBuilder().build(); AnnotateTextRequest.Features features = AnnotateTextRequest.Features.newBuilder().build(); EncodingType encodingType = EncodingType.NONE; client.annotateText(document, features, encodingType); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } } }
subBuilder = document_.toBuilder(); com.google.cloud.language.v1.Document.parser(), extensionRegistry); if (subBuilder != null) { subBuilder.mergeFrom(document_); document_ = subBuilder.buildPartial();
@java.lang.Override public com.google.cloud.language.v1.Document build() { com.google.cloud.language.v1.Document result = buildPartial(); if (!result.isInitialized()) { throw newUninitializedMessageException(result); } return result; }
@java.lang.Override protected Builder newBuilderForType(com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { Builder builder = new Builder(parent); return builder; } /**
private Builder(com.google.protobuf.GeneratedMessageV3.BuilderParent parent) { super(parent); maybeForceBuilderInitialization(); }
private Builder() { maybeForceBuilderInitialization(); }
/** * * * <pre> * Input document. * </pre> * * <code>.google.cloud.language.v1.Document document = 1;</code> */ public Builder setDocument(com.google.cloud.language.v1.Document.Builder builderForValue) { if (documentBuilder_ == null) { document_ = builderForValue.build(); onChanged(); } else { documentBuilder_.setMessage(builderForValue.build()); } return this; } /**
/** * * * <pre> * Input document. * </pre> * * <code>.google.cloud.language.v1.Document document = 1;</code> */ public Builder setDocument(com.google.cloud.language.v1.Document.Builder builderForValue) { if (documentBuilder_ == null) { document_ = builderForValue.build(); onChanged(); } else { documentBuilder_.setMessage(builderForValue.build()); } return this; } /**