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); } }
/** * Classifies a document into categories. * * <p>Sample code: * * <pre><code> * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) { * Document document = Document.newBuilder().build(); * ClassifyTextResponse response = languageServiceClient.classifyText(document); * } * </code></pre> * * @param document Input document. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final ClassifyTextResponse classifyText(Document document) { ClassifyTextRequest request = ClassifyTextRequest.newBuilder().setDocument(document).build(); return classifyText(request); }
/** * Finds named entities (currently proper names and common nouns) in the text along with entity * types, salience, mentions for each entity, and other properties. * * <p>Sample code: * * <pre><code> * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) { * Document document = Document.newBuilder().build(); * EncodingType encodingType = EncodingType.NONE; * AnalyzeEntitiesResponse response = languageServiceClient.analyzeEntities(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 AnalyzeEntitiesResponse analyzeEntities( Document document, EncodingType encodingType) { AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest.newBuilder() .setDocument(document) .setEncodingType(encodingType) .build(); return analyzeEntities(request); }
/** * Detects categories in text using the Language Beta API. */ public static void classifyText(String text) throws Exception { // [START classify_text] // Instantiate a beta client : com.google.cloud.language.v1beta2.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 classify_text] }
/** * 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); }
/** Constructs an instance of LanguageServiceClient with default settings. */ public static final LanguageServiceClient create() throws IOException { return create(LanguageServiceSettings.newBuilder().build()); }
/** * Finds entities, similar to * [AnalyzeEntities][google.cloud.language.v1beta2.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); }
/** * Analyzes the syntax of the text and provides sentence boundaries and tokenization along with * part of speech tags, dependency trees, and other properties. * * <p>Sample code: * * <pre><code> * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) { * Document document = Document.newBuilder().build(); * EncodingType encodingType = EncodingType.NONE; * AnalyzeSyntaxResponse response = languageServiceClient.analyzeSyntax(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 AnalyzeSyntaxResponse analyzeSyntax(Document document, EncodingType encodingType) { AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest.newBuilder() .setDocument(document) .setEncodingType(encodingType) .build(); return analyzeSyntax(request); }
/** * A convenience method that provides all syntax, sentiment, entity, and classification features * in one call. * * <p>Sample code: * * <pre><code> * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) { * Document document = Document.newBuilder().build(); * AnnotateTextRequest.Features features = AnnotateTextRequest.Features.newBuilder().build(); * EncodingType encodingType = EncodingType.NONE; * AnnotateTextResponse response = languageServiceClient.annotateText(document, features, encodingType); * } * </code></pre> * * @param document Input document. * @param features The enabled features. * @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 AnnotateTextResponse annotateText( Document document, AnnotateTextRequest.Features features, EncodingType encodingType) { AnnotateTextRequest request = AnnotateTextRequest.newBuilder() .setDocument(document) .setFeatures(features) .setEncodingType(encodingType) .build(); return annotateText(request); }
/** * Constructs an instance of LanguageServiceClient, using the given settings. The channels are * created based on the settings passed in, or defaults for any settings that are not set. */ public static final LanguageServiceClient create(LanguageServiceSettings settings) throws IOException { return new LanguageServiceClient(settings); }
/** * Finds named entities (currently proper names and common nouns) in the text along with entity * types, salience, mentions for each entity, and other properties. * * <p>Sample code: * * <pre><code> * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) { * Document document = Document.newBuilder().build(); * AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest.newBuilder() * .setDocument(document) * .build(); * AnalyzeEntitiesResponse response = languageServiceClient.analyzeEntities(request); * } * </code></pre> * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final AnalyzeEntitiesResponse analyzeEntities(AnalyzeEntitiesRequest request) { return analyzeEntitiesCallable().call(request); }
/** * Detects categories in a GCS hosted file using the Language Beta API. */ public static void classifyFile(String gcsUri) throws Exception { // [START classify_file] // Instantiate a beta client : com.google.cloud.language.v1beta2.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 classify_file] } }
@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 } }
@Before public void setUp() throws IOException { serviceHelper.reset(); channelProvider = serviceHelper.createChannelProvider(); LanguageServiceSettings settings = LanguageServiceSettings.newBuilder() .setTransportChannelProvider(channelProvider) .setCredentialsProvider(NoCredentialsProvider.create()) .build(); client = LanguageServiceClient.create(settings); }
@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 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 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 } } }
/** * Constructs an instance of LanguageServiceClient, using the given stub for making calls. This is * for advanced usage - prefer to use LanguageServiceSettings}. */ @BetaApi("A restructuring of stub classes is planned, so this may break in the future") public static final LanguageServiceClient create(LanguageServiceStub stub) { return new LanguageServiceClient(stub); }
/** * Finds named entities (currently proper names and common nouns) in the text along with entity * types, salience, mentions for each entity, and other properties. * * <p>Sample code: * * <pre><code> * try (LanguageServiceClient languageServiceClient = LanguageServiceClient.create()) { * Document document = Document.newBuilder().build(); * AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest.newBuilder() * .setDocument(document) * .build(); * AnalyzeEntitiesResponse response = languageServiceClient.analyzeEntities(request); * } * </code></pre> * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final AnalyzeEntitiesResponse analyzeEntities(AnalyzeEntitiesRequest request) { return analyzeEntitiesCallable().call(request); }
try (LanguageServiceClient language = LanguageServiceClient.create()) { .build(); AnalyzeSentimentResponse response = language.analyzeSentiment(doc); Sentiment sentiment = response.getDocumentSentiment(); if (sentiment != null) {