use of com.google.cloud.language.v1beta2.Document in project k-9 by k9mail.
the class SettingsExporterTest method exportPreferences.
private Document exportPreferences(boolean globalSettings, Set<String> accounts) throws Exception {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
SettingsExporter.exportPreferences(RuntimeEnvironment.application, outputStream, globalSettings, accounts);
Document document = parseXML(outputStream.toByteArray());
outputStream.close();
return document;
}
use of com.google.cloud.language.v1beta2.Document 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.v1beta2.Document in project google-cloud-java by GoogleCloudPlatform.
the class LanguageServiceClientTest method analyzeEntitySentimentTest.
@Test
@SuppressWarnings("all")
public void analyzeEntitySentimentTest() {
String language = "language-1613589672";
AnalyzeEntitySentimentResponse expectedResponse = AnalyzeEntitySentimentResponse.newBuilder().setLanguage(language).build();
mockLanguageService.addResponse(expectedResponse);
Document document = Document.newBuilder().build();
EncodingType encodingType = EncodingType.NONE;
AnalyzeEntitySentimentResponse actualResponse = client.analyzeEntitySentiment(document, encodingType);
Assert.assertEquals(expectedResponse, actualResponse);
List<GeneratedMessageV3> actualRequests = mockLanguageService.getRequests();
Assert.assertEquals(1, actualRequests.size());
AnalyzeEntitySentimentRequest actualRequest = (AnalyzeEntitySentimentRequest) actualRequests.get(0);
Assert.assertEquals(document, actualRequest.getDocument());
Assert.assertEquals(encodingType, actualRequest.getEncodingType());
}
use of com.google.cloud.language.v1beta2.Document in project google-cloud-java by GoogleCloudPlatform.
the class LanguageServiceClientTest method analyzeSentimentTest.
@Test
@SuppressWarnings("all")
public void analyzeSentimentTest() {
String language = "language-1613589672";
AnalyzeSentimentResponse expectedResponse = AnalyzeSentimentResponse.newBuilder().setLanguage(language).build();
mockLanguageService.addResponse(expectedResponse);
Document document = Document.newBuilder().build();
AnalyzeSentimentResponse actualResponse = client.analyzeSentiment(document);
Assert.assertEquals(expectedResponse, actualResponse);
List<GeneratedMessageV3> actualRequests = mockLanguageService.getRequests();
Assert.assertEquals(1, actualRequests.size());
AnalyzeSentimentRequest actualRequest = (AnalyzeSentimentRequest) actualRequests.get(0);
Assert.assertEquals(document, actualRequest.getDocument());
}
use of com.google.cloud.language.v1beta2.Document in project google-cloud-java by GoogleCloudPlatform.
the class LanguageServiceClientTest method analyzeSentimentExceptionTest.
@Test
@SuppressWarnings("all")
public void analyzeSentimentExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT);
mockLanguageService.addException(exception);
try {
Document document = Document.newBuilder().build();
client.analyzeSentiment(document);
Assert.fail("No exception raised");
} catch (ApiException e) {
Assert.assertEquals(Status.INVALID_ARGUMENT.getCode(), e.getStatusCode());
}
}
Aggregations