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()); } } }
@java.lang.Override public com.google.cloud.language.v1.Document buildPartial() { com.google.cloud.language.v1.Document result = new com.google.cloud.language.v1.Document(this); result.type_ = type_; if (sourceCase_ == 2) { result.source_ = source_; } if (sourceCase_ == 3) { result.source_ = source_; } result.language_ = language_; result.sourceCase_ = sourceCase_; onBuilt(); return result; }
@java.lang.Override public int hashCode() { if (memoizedHashCode != 0) { return memoizedHashCode; } int hash = 41; hash = (19 * hash) + getDescriptor().hashCode(); hash = (37 * hash) + TYPE_FIELD_NUMBER; hash = (53 * hash) + type_; hash = (37 * hash) + LANGUAGE_FIELD_NUMBER; hash = (53 * hash) + getLanguage().hashCode(); switch (sourceCase_) { case 2: hash = (37 * hash) + CONTENT_FIELD_NUMBER; hash = (53 * hash) + getContent().hashCode(); break; case 3: hash = (37 * hash) + GCS_CONTENT_URI_FIELD_NUMBER; hash = (53 * hash) + getGcsContentUri().hashCode(); break; case 0: default: } hash = (29 * hash) + unknownFields.hashCode(); memoizedHashCode = hash; return hash; }
@java.lang.Override public boolean equals(final java.lang.Object obj) { if (obj == this) { return true; } if (!(obj instanceof com.google.cloud.language.v1.Document)) { return super.equals(obj); } com.google.cloud.language.v1.Document other = (com.google.cloud.language.v1.Document) obj; boolean result = true; result = result && type_ == other.type_; result = result && getLanguage().equals(other.getLanguage()); result = result && getSourceCase().equals(other.getSourceCase()); if (!result) return false; switch (sourceCase_) { case 2: result = result && getContent().equals(other.getContent()); break; case 3: result = result && getGcsContentUri().equals(other.getGcsContentUri()); break; case 0: default: } result = result && unknownFields.equals(other.unknownFields); return result; }
/** * * * <pre> * The language of the document (if not specified, the language is * automatically detected). Both ISO and BCP-47 language codes are * accepted.<br> * [Language Support](/natural-language/docs/languages) * lists currently supported languages for each API method. * If the language (either specified by the caller or automatically detected) * is not supported by the called API method, an `INVALID_ARGUMENT` error * is returned. * </pre> * * <code>string language = 4;</code> */ public Builder clearLanguage() { language_ = getDefaultInstance().getLanguage(); onChanged(); return this; } /**
@java.lang.Override public Builder newBuilderForType() { return newBuilder(); }
/** * 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 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 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 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 } } }
@java.lang.Override public com.google.cloud.language.v1.Document getDefaultInstanceForType() { return com.google.cloud.language.v1.Document.getDefaultInstance(); }
public Builder mergeFrom(com.google.cloud.language.v1.Document other) { if (other == com.google.cloud.language.v1.Document.getDefaultInstance()) return this; if (other.type_ != 0) { setTypeValue(other.getTypeValue()); if (!other.getLanguage().isEmpty()) { language_ = other.language_; onChanged(); switch (other.getSourceCase()) { case CONTENT: onChanged(); break;
@java.lang.Override public boolean equals(final java.lang.Object obj) { if (obj == this) { return true; } if (!(obj instanceof com.google.cloud.language.v1.ClassifyTextRequest)) { return super.equals(obj); } com.google.cloud.language.v1.ClassifyTextRequest other = (com.google.cloud.language.v1.ClassifyTextRequest) obj; boolean result = true; result = result && (hasDocument() == other.hasDocument()); if (hasDocument()) { result = result && getDocument().equals(other.getDocument()); } result = result && unknownFields.equals(other.unknownFields); return result; }
public static final com.google.protobuf.Descriptors.EnumDescriptor getDescriptor() { return com.google.cloud.language.v1.Document.getDescriptor().getEnumTypes().get(0); }