/** * 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); }
@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 classifyTextTest() { ClassifyTextResponse expectedResponse = ClassifyTextResponse.newBuilder().build(); mockLanguageService.addResponse(expectedResponse); Document document = Document.newBuilder().build(); ClassifyTextResponse actualResponse = client.classifyText(document); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockLanguageService.getRequests(); Assert.assertEquals(1, actualRequests.size()); ClassifyTextRequest actualRequest = (ClassifyTextRequest) actualRequests.get(0); Assert.assertEquals(document, actualRequest.getDocument()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
/** * 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 categories in a GCS hosted file using the Language Beta API. */ public static void classifyFile(String gcsUri) throws Exception { // [START language_classify_gcs] // Instantiate the Language client com.google.cloud.language.v1.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 language_classify_gcs] }
/** * 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); }