Search in sources :

Example 1 with AnalyzeSyntaxRequest

use of com.google.cloud.language.v1.AnalyzeSyntaxRequest in project google-cloud-java by GoogleCloudPlatform.

the class LanguageServiceClientTest method analyzeSyntaxTest.

@Test
@SuppressWarnings("all")
public void analyzeSyntaxTest() {
    String language = "language-1613589672";
    AnalyzeSyntaxResponse expectedResponse = AnalyzeSyntaxResponse.newBuilder().setLanguage(language).build();
    mockLanguageService.addResponse(expectedResponse);
    Document document = Document.newBuilder().build();
    EncodingType encodingType = EncodingType.NONE;
    AnalyzeSyntaxResponse actualResponse = client.analyzeSyntax(document, encodingType);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockLanguageService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    AnalyzeSyntaxRequest actualRequest = (AnalyzeSyntaxRequest) actualRequests.get(0);
    Assert.assertEquals(document, actualRequest.getDocument());
    Assert.assertEquals(encodingType, actualRequest.getEncodingType());
}
Also used : AnalyzeSyntaxRequest(com.google.cloud.language.v1beta2.AnalyzeSyntaxRequest) EncodingType(com.google.cloud.language.v1beta2.EncodingType) Document(com.google.cloud.language.v1beta2.Document) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) AnalyzeSyntaxResponse(com.google.cloud.language.v1beta2.AnalyzeSyntaxResponse) Test(org.junit.Test)

Example 2 with AnalyzeSyntaxRequest

use of com.google.cloud.language.v1.AnalyzeSyntaxRequest in project google-cloud-java by GoogleCloudPlatform.

the class LanguageServiceClientTest method analyzeSyntaxTest.

@Test
@SuppressWarnings("all")
public void analyzeSyntaxTest() {
    String language = "language-1613589672";
    AnalyzeSyntaxResponse expectedResponse = AnalyzeSyntaxResponse.newBuilder().setLanguage(language).build();
    mockLanguageService.addResponse(expectedResponse);
    Document document = Document.newBuilder().build();
    EncodingType encodingType = EncodingType.NONE;
    AnalyzeSyntaxResponse actualResponse = client.analyzeSyntax(document, encodingType);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockLanguageService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    AnalyzeSyntaxRequest actualRequest = (AnalyzeSyntaxRequest) actualRequests.get(0);
    Assert.assertEquals(document, actualRequest.getDocument());
    Assert.assertEquals(encodingType, actualRequest.getEncodingType());
}
Also used : AnalyzeSyntaxRequest(com.google.cloud.language.v1.AnalyzeSyntaxRequest) EncodingType(com.google.cloud.language.v1.EncodingType) Document(com.google.cloud.language.v1.Document) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) AnalyzeSyntaxResponse(com.google.cloud.language.v1.AnalyzeSyntaxResponse) Test(org.junit.Test)

Example 3 with AnalyzeSyntaxRequest

use of com.google.cloud.language.v1.AnalyzeSyntaxRequest in project java-docs-samples by GoogleCloudPlatform.

the class Analyze method analyzeSyntaxText.

/**
 * from the string {@code text}.
 */
public static List<Token> analyzeSyntaxText(String text) throws Exception {
    // 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();
        AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest.newBuilder().setDocument(doc).setEncodingType(EncodingType.UTF16).build();
        // analyze the syntax in the given text
        AnalyzeSyntaxResponse response = language.analyzeSyntax(request);
        // print the response
        for (Token token : response.getTokensList()) {
            System.out.printf("\tText: %s\n", token.getText().getContent());
            System.out.printf("\tBeginOffset: %d\n", token.getText().getBeginOffset());
            System.out.printf("Lemma: %s\n", token.getLemma());
            System.out.printf("PartOfSpeechTag: %s\n", token.getPartOfSpeech().getTag());
            System.out.printf("\tAspect: %s\n", token.getPartOfSpeech().getAspect());
            System.out.printf("\tCase: %s\n", token.getPartOfSpeech().getCase());
            System.out.printf("\tForm: %s\n", token.getPartOfSpeech().getForm());
            System.out.printf("\tGender: %s\n", token.getPartOfSpeech().getGender());
            System.out.printf("\tMood: %s\n", token.getPartOfSpeech().getMood());
            System.out.printf("\tNumber: %s\n", token.getPartOfSpeech().getNumber());
            System.out.printf("\tPerson: %s\n", token.getPartOfSpeech().getPerson());
            System.out.printf("\tProper: %s\n", token.getPartOfSpeech().getProper());
            System.out.printf("\tReciprocity: %s\n", token.getPartOfSpeech().getReciprocity());
            System.out.printf("\tTense: %s\n", token.getPartOfSpeech().getTense());
            System.out.printf("\tVoice: %s\n", token.getPartOfSpeech().getVoice());
            System.out.println("DependencyEdge");
            System.out.printf("\tHeadTokenIndex: %d\n", token.getDependencyEdge().getHeadTokenIndex());
            System.out.printf("\tLabel: %s\n\n", token.getDependencyEdge().getLabel());
        }
        return response.getTokensList();
    }
// [END analyze_syntax_text]
}
Also used : LanguageServiceClient(com.google.cloud.language.v1.LanguageServiceClient) AnalyzeSyntaxRequest(com.google.cloud.language.v1.AnalyzeSyntaxRequest) Token(com.google.cloud.language.v1.Token) Document(com.google.cloud.language.v1.Document) AnalyzeSyntaxResponse(com.google.cloud.language.v1.AnalyzeSyntaxResponse)

Example 4 with AnalyzeSyntaxRequest

use of com.google.cloud.language.v1.AnalyzeSyntaxRequest in project java-docs-samples by GoogleCloudPlatform.

the class Analyze method analyzeSyntaxFile.

/**
 * Get the syntax of the GCS hosted file.
 */
public static List<Token> analyzeSyntaxFile(String gcsUri) throws Exception {
    // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
    try (LanguageServiceClient language = LanguageServiceClient.create()) {
        Document doc = Document.newBuilder().setGcsContentUri(gcsUri).setType(Type.PLAIN_TEXT).build();
        AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest.newBuilder().setDocument(doc).setEncodingType(EncodingType.UTF16).build();
        // analyze the syntax in the given text
        AnalyzeSyntaxResponse response = language.analyzeSyntax(request);
        // print the response
        for (Token token : response.getTokensList()) {
            System.out.printf("\tText: %s\n", token.getText().getContent());
            System.out.printf("\tBeginOffset: %d\n", token.getText().getBeginOffset());
            System.out.printf("Lemma: %s\n", token.getLemma());
            System.out.printf("PartOfSpeechTag: %s\n", token.getPartOfSpeech().getTag());
            System.out.printf("\tAspect: %s\n", token.getPartOfSpeech().getAspect());
            System.out.printf("\tCase: %s\n", token.getPartOfSpeech().getCase());
            System.out.printf("\tForm: %s\n", token.getPartOfSpeech().getForm());
            System.out.printf("\tGender: %s\n", token.getPartOfSpeech().getGender());
            System.out.printf("\tMood: %s\n", token.getPartOfSpeech().getMood());
            System.out.printf("\tNumber: %s\n", token.getPartOfSpeech().getNumber());
            System.out.printf("\tPerson: %s\n", token.getPartOfSpeech().getPerson());
            System.out.printf("\tProper: %s\n", token.getPartOfSpeech().getProper());
            System.out.printf("\tReciprocity: %s\n", token.getPartOfSpeech().getReciprocity());
            System.out.printf("\tTense: %s\n", token.getPartOfSpeech().getTense());
            System.out.printf("\tVoice: %s\n", token.getPartOfSpeech().getVoice());
            System.out.println("DependencyEdge");
            System.out.printf("\tHeadTokenIndex: %d\n", token.getDependencyEdge().getHeadTokenIndex());
            System.out.printf("\tLabel: %s\n\n", token.getDependencyEdge().getLabel());
        }
        return response.getTokensList();
    }
// [END analyze_syntax_file]
}
Also used : LanguageServiceClient(com.google.cloud.language.v1.LanguageServiceClient) AnalyzeSyntaxRequest(com.google.cloud.language.v1.AnalyzeSyntaxRequest) Token(com.google.cloud.language.v1.Token) Document(com.google.cloud.language.v1.Document) AnalyzeSyntaxResponse(com.google.cloud.language.v1.AnalyzeSyntaxResponse)

Aggregations

AnalyzeSyntaxRequest (com.google.cloud.language.v1.AnalyzeSyntaxRequest)3 AnalyzeSyntaxResponse (com.google.cloud.language.v1.AnalyzeSyntaxResponse)3 Document (com.google.cloud.language.v1.Document)3 LanguageServiceClient (com.google.cloud.language.v1.LanguageServiceClient)2 Token (com.google.cloud.language.v1.Token)2 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)2 Test (org.junit.Test)2 EncodingType (com.google.cloud.language.v1.EncodingType)1 AnalyzeSyntaxRequest (com.google.cloud.language.v1beta2.AnalyzeSyntaxRequest)1 AnalyzeSyntaxResponse (com.google.cloud.language.v1beta2.AnalyzeSyntaxResponse)1 Document (com.google.cloud.language.v1beta2.Document)1 EncodingType (com.google.cloud.language.v1beta2.EncodingType)1