use of com.google.cloud.language.spi.v1.LanguageServiceClient in project java-docs-samples by GoogleCloudPlatform.
the class Analyze method analyzeEntitiesFile.
/**
* Identifies entities in the contents of the object at the given GCS {@code path}.
*/
public static void analyzeEntitiesFile(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 to the file to be analyzed
Document doc = Document.newBuilder().setGcsContentUri(gcsUri).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_gcs]
}
use of com.google.cloud.language.spi.v1.LanguageServiceClient in project java-docs-samples by GoogleCloudPlatform.
the class Analyze method entitySentimentFile.
/**
* Identifies the entity sentiments in the the GCS hosted file using the Language Beta API.
*/
public static void entitySentimentFile(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();
AnalyzeEntitySentimentRequest request = AnalyzeEntitySentimentRequest.newBuilder().setDocument(doc).setEncodingType(EncodingType.UTF16).build();
// Detect entity sentiments in the given file
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 entity_sentiment_file]
}
use of com.google.cloud.language.spi.v1.LanguageServiceClient in project java-docs-samples by GoogleCloudPlatform.
the class AnalyzeBeta method classifyFile.
/**
* Detects categories in a GCS hosted file using the Language Beta API.
*/
public static void classifyFile(String gcsUri) throws Exception {
// 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]
}
use of com.google.cloud.language.spi.v1.LanguageServiceClient in project google-cloud-java by GoogleCloudPlatform.
the class AnalyzeSentiment method main.
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());
}
}
use of com.google.cloud.language.spi.v1.LanguageServiceClient in project java-docs-samples by GoogleCloudPlatform.
the class Analyze method analyzeSentimentText.
/**
* Identifies the sentiment in the string {@code text}.
*/
public static Sentiment analyzeSentimentText(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();
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_text]
}
Aggregations