Search in sources :

Example 6 with LanguageServiceClient

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

the class Analyze method analyzeEntitiesText.

/**
 * Identifies entities in the string {@code text}.
 */
public static void analyzeEntitiesText(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();
        AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest.newBuilder().setDocument(doc).setEncodingType(EncodingType.UTF16).build();
        AnalyzeEntitiesResponse response = language.analyzeEntities(request);
        // Print the response
        for (Entity entity : response.getEntitiesList()) {
            System.out.printf("Entity: %s", entity.getName());
            System.out.printf("Salience: %.3f\n", entity.getSalience());
            System.out.println("Metadata: ");
            for (Map.Entry<String, String> entry : entity.getMetadataMap().entrySet()) {
                System.out.printf("%s : %s", entry.getKey(), entry.getValue());
            }
            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("Type: %s\n\n", mention.getType());
            }
        }
    }
// [END analyze_entities_text]
}
Also used : LanguageServiceClient(com.google.cloud.language.v1.LanguageServiceClient) Entity(com.google.cloud.language.v1.Entity) AnalyzeEntitiesRequest(com.google.cloud.language.v1.AnalyzeEntitiesRequest) EntityMention(com.google.cloud.language.v1.EntityMention) AnalyzeEntitiesResponse(com.google.cloud.language.v1.AnalyzeEntitiesResponse) Document(com.google.cloud.language.v1.Document) Map(java.util.Map)

Example 7 with LanguageServiceClient

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

the class Analyze method classifyFile.

/**
 * Detects categories in a GCS hosted file using the Language Beta API.
 */
public static void classifyFile(String gcsUri) throws Exception {
    // 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 classify_file]
}
Also used : LanguageServiceClient(com.google.cloud.language.v1.LanguageServiceClient) ClassifyTextResponse(com.google.cloud.language.v1.ClassifyTextResponse) Document(com.google.cloud.language.v1.Document) ClassificationCategory(com.google.cloud.language.v1.ClassificationCategory) ClassifyTextRequest(com.google.cloud.language.v1.ClassifyTextRequest)

Example 8 with LanguageServiceClient

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

the class Analyze method analyzeSentimentFile.

/**
 * Gets {@link Sentiment} from the contents of the GCS hosted file.
 */
public static Sentiment analyzeSentimentFile(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();
        AnalyzeSentimentResponse response = language.analyzeSentiment(doc);
        Sentiment sentiment = response.getDocumentSentiment();
        if (sentiment == null) {
            System.out.println("No sentiment found");
        } else {
            System.out.printf("Sentiment magnitude : %.3f\n", sentiment.getMagnitude());
            System.out.printf("Sentiment score : %.3f\n", sentiment.getScore());
        }
        return sentiment;
    }
// [END analyze_sentiment_file]
}
Also used : LanguageServiceClient(com.google.cloud.language.v1.LanguageServiceClient) AnalyzeSentimentResponse(com.google.cloud.language.v1.AnalyzeSentimentResponse) Document(com.google.cloud.language.v1.Document) Sentiment(com.google.cloud.language.v1.Sentiment)

Example 9 with LanguageServiceClient

use of com.google.cloud.language.spi.v1.LanguageServiceClient 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 10 with LanguageServiceClient

use of com.google.cloud.language.spi.v1.LanguageServiceClient 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

Document (com.google.cloud.language.v1.Document)12 LanguageServiceClient (com.google.cloud.language.v1.LanguageServiceClient)12 Entity (com.google.cloud.language.v1.Entity)4 EntityMention (com.google.cloud.language.v1.EntityMention)4 Sentiment (com.google.cloud.language.v1.Sentiment)4 Document (com.google.cloud.language.v1beta2.Document)3 LanguageServiceClient (com.google.cloud.language.v1beta2.LanguageServiceClient)3 AnalyzeEntitiesRequest (com.google.cloud.language.v1.AnalyzeEntitiesRequest)2 AnalyzeEntitiesResponse (com.google.cloud.language.v1.AnalyzeEntitiesResponse)2 AnalyzeEntitySentimentRequest (com.google.cloud.language.v1.AnalyzeEntitySentimentRequest)2 AnalyzeEntitySentimentResponse (com.google.cloud.language.v1.AnalyzeEntitySentimentResponse)2 AnalyzeSentimentResponse (com.google.cloud.language.v1.AnalyzeSentimentResponse)2 AnalyzeSyntaxRequest (com.google.cloud.language.v1.AnalyzeSyntaxRequest)2 AnalyzeSyntaxResponse (com.google.cloud.language.v1.AnalyzeSyntaxResponse)2 ClassificationCategory (com.google.cloud.language.v1.ClassificationCategory)2 ClassifyTextRequest (com.google.cloud.language.v1.ClassifyTextRequest)2 ClassifyTextResponse (com.google.cloud.language.v1.ClassifyTextResponse)2 Token (com.google.cloud.language.v1.Token)2 ClassificationCategory (com.google.cloud.language.v1beta2.ClassificationCategory)2 ClassifyTextRequest (com.google.cloud.language.v1beta2.ClassifyTextRequest)2