use of com.google.cloud.language.v1.Sentiment 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.v1.Sentiment in project java-docs-samples by GoogleCloudPlatform.
the class AnalyzeBetaIT method analyzeSentiment_returnPositiveGerman.
@Test
public void analyzeSentiment_returnPositiveGerman() throws Exception {
Sentiment sentiment = AnalyzeBeta.analyzeSentimentText("Ich hatte die schönste Erfahrung mit euch allen.", "DE");
assertThat(sentiment.getMagnitude()).isGreaterThan(0.0F);
assertThat(sentiment.getScore()).isGreaterThan(0.0F);
}
use of com.google.cloud.language.v1.Sentiment in project java-docs-samples by GoogleCloudPlatform.
the class AnalyzeIT method analyzeSentimentFile_returnPositiveFile.
@Test
public void analyzeSentimentFile_returnPositiveFile() throws Exception {
Sentiment sentiment = Analyze.analyzeSentimentFile("gs://" + BUCKET + "/natural-language/" + "sentiment/bladerunner-pos.txt");
assertThat(sentiment.getMagnitude()).isGreaterThan(0.0F);
assertThat(sentiment.getScore()).isGreaterThan(0.0F);
}
use of com.google.cloud.language.v1.Sentiment in project java-docs-samples by GoogleCloudPlatform.
the class AnalyzeIT method analyzeSentiment_returnNeutralFile.
@Test
public void analyzeSentiment_returnNeutralFile() throws Exception {
Sentiment sentiment = Analyze.analyzeSentimentFile("gs://" + BUCKET + "/natural-language/" + "sentiment/bladerunner-neutral.txt");
assertThat(sentiment.getMagnitude()).isGreaterThan(1.0F);
// TODO sentiment score for netural sample appears to be zero now.
// assertThat((double)sentiment.getScore()).isGreaterThan(0.0);
}
use of com.google.cloud.language.v1.Sentiment in project java-docs-samples by GoogleCloudPlatform.
the class AnalyzeIT method analyzeSentiment_returnNegative.
@Test
public void analyzeSentiment_returnNegative() throws Exception {
Sentiment sentiment = Analyze.analyzeSentimentFile("gs://" + BUCKET + "/natural-language/" + "sentiment/bladerunner-neg.txt");
assertThat(sentiment.getMagnitude()).isGreaterThan(0.0F);
assertThat(sentiment.getScore()).isLessThan(0.0F);
}
Aggregations