Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@
import com.azure.ai.textanalytics.models.TextSentiment;
import com.azure.ai.textanalytics.models.AnalyzeSentimentResult;

import java.util.List;

/**
* Sample demonstrate how to analyze sentiment of a text input.
* Sample demonstrates how to analyze the sentiment of an input text.
*/
public class AnalyzeSentiment {
/**
* Main method to invoke this demo about how to analyze sentiment of a text input.
* Main method to invoke this demo about how to analyze the sentiment of an input text.
*
* @param args Unused arguments to the program.
*/
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsClient client = new TextAnalyticsClientBuilder()
.subscriptionKey("subscription-key")
.endpoint("https://servicename.cognitiveservices.azure.com/")
.subscriptionKey("{subscription_key}")
.endpoint("https://{servicename}.cognitiveservices.azure.com/")
.buildClient();

// The text that need be analysed.
Expand All @@ -31,16 +29,15 @@ public static void main(String[] args) {

final TextSentiment documentSentiment = sentimentResult.getDocumentSentiment();
System.out.printf(
"Recognized TextSentiment: %s, Positive Score: %s, Neutral Score: %s, Negative Score: %s.%n",
"Recognized sentiment: %s, positive score: %s, neutral score: %s, negative score: %s.%n",
documentSentiment.getTextSentimentClass(),
documentSentiment.getPositiveScore(),
documentSentiment.getNeutralScore(),
documentSentiment.getNegativeScore());

final List<TextSentiment> sentiments = sentimentResult.getSentenceSentiments();
for (TextSentiment textSentiment : sentiments) {
for (TextSentiment textSentiment : sentimentResult.getSentenceSentiments()) {
System.out.printf(
"Recognized Sentence TextSentiment: %s, Positive Score: %s, Neutral Score: %s, Negative Score: %s.%n",
"Recognized sentence sentiment: %s, positive score: %s, neutral score: %s, negative score: %s.%n",
textSentiment.getTextSentimentClass(),
textSentiment.getPositiveScore(),
textSentiment.getNeutralScore(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.ai.textanalytics;

import com.azure.ai.textanalytics.models.TextSentiment;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* Sample demonstrates how to asynchronously analyze the sentiment of an input text.
* */
public class AnalyzeSentimentAsync {
/**
* Main method to invoke this demo about how to analyze the sentiment of an input text.
*
* @param args Unused arguments to the program.
*/
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder()
.subscriptionKey("{subscription_key}")
.endpoint("https://{servicename}.cognitiveservices.azure.com/")
.buildAsyncClient();

// The text that need be analysed.
String text = "The hotel was dark and unclean.";

client.analyzeSentiment(text).subscribe(
result -> {
final TextSentiment documentSentiment = result.getDocumentSentiment();
System.out.printf(
"Recognized sentiment: %s, positive score: %s, neutral score: %s, negative score: %s.%n",
documentSentiment.getTextSentimentClass(),
documentSentiment.getPositiveScore(),
documentSentiment.getNeutralScore(),
documentSentiment.getNegativeScore());

for (TextSentiment textSentiment : result.getSentenceSentiments()) {
System.out.printf(
"Recognized sentence sentiment: %s, positive score: %s, neutral score: %s, negative score: %s.%n",
textSentiment.getTextSentimentClass(),
textSentiment.getPositiveScore(),
textSentiment.getNeutralScore(),
textSentiment.getNegativeScore());
}
},
error -> System.err.println("There was an error analyzing sentiment of the text." + error),
() -> System.out.println("Sentiment analyzed."));

// The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep
// the thread so the program does not end before the send operation is complete. Using .block() instead of
// .subscribe() will turn this into a synchronous call.
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException ignored) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@
package com.azure.ai.textanalytics;

/**
* Sample demonstrate how to analyze key phrases of a text input.
* Sample demonstrates how to extract the key phrases of an input text.
*/
public class ExtractKeyPhrases {
/**
* Main method to invoke this demo about how to extract key phrases of a text input.
* Main method to invoke this demo about how to extract the key phrases of an input text.
*
* @param args Unused arguments to the program.
*/
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsClient client = new TextAnalyticsClientBuilder()
.subscriptionKey("subscription-key")
.endpoint("https://servicename.cognitiveservices.azure.com/")
.subscriptionKey("{subscription_key}")
.endpoint("https://{servicename}.cognitiveservices.azure.com/")
.buildClient();

// The text that need be analysed.
String text = "My cat might need to see a veterinarian.";

for (String keyPhrase : client.extractKeyPhrases(text).getKeyPhrases()) {
System.out.printf("Recognized Phrases: %s.%n", keyPhrase);
System.out.printf("Recognized phrases: %s.%n", keyPhrase);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.ai.textanalytics;

import java.util.concurrent.TimeUnit;

/**
* Sample demonstrates how to asynchronously extract the key phrases of an input text.
*/
public class ExtractKeyPhrasesAsync {
/**
* Main method to invoke this demo about how to extract the key phrases of an input text.
*
* @param args Unused arguments to the program.
*/
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder()
.subscriptionKey("{subscription_key}")
.endpoint("https://{servicename}.cognitiveservices.azure.com/")
.buildAsyncClient();

// The text that need be analysed.
String text = "My cat might need to see a veterinarian.";

client.extractKeyPhrases(text).subscribe(
result -> {
for (String keyPhrase : result.getKeyPhrases()) {
System.out.printf("Recognized phrases: %s.%n", keyPhrase);
}
},
error -> System.err.println("There was an error extracting key phrases of the text." + error),
() -> System.out.println("Key phrases extracted."));

// The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep
// the thread so the program does not end before the send operation is complete. Using .block() instead of
// .subscribe() will turn this into a synchronous call.
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException ignored) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,34 @@
import com.azure.ai.textanalytics.models.DetectLanguageResult;
import com.azure.ai.textanalytics.models.DetectedLanguage;

import java.util.List;

/**
* Sample demonstrate how to detect language of a text input.
* Sample demonstrates how to detect the language of an input text.
*/
public class HelloWorld {
/**
* Main method to invoke this demo about how to detect language of a text input.
* Main method to invoke this demo about how to detect the language of an input text.
*
* @param args Unused arguments to the program.
*/
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsClient client = new TextAnalyticsClientBuilder()
.subscriptionKey("subscription-key")
.endpoint("https://servicename.cognitiveservices.azure.com/")
.subscriptionKey("{subscription_key}")
.endpoint("https://{servicename}.cognitiveservices.azure.com/")
.buildClient();

// The text that need be analysed.
String text = "hello world";

final DetectLanguageResult detectLanguageResult = client.detectLanguage(text, "US");
final DetectedLanguage detectedDocumentLanguage = detectLanguageResult.getPrimaryLanguage();
System.out.printf("Detected Primary Language: %s, ISO 6391 Name: %s, Score: %s.%n",
detectedDocumentLanguage.getName(),
detectedDocumentLanguage.getIso6391Name(),
detectedDocumentLanguage.getScore());
final DetectedLanguage detectedPrimaryLanguage = detectLanguageResult.getPrimaryLanguage();
System.out.printf("Detected primary language: %s, ISO 6391 name: %s, score: %s.%n",
detectedPrimaryLanguage.getName(),
detectedPrimaryLanguage.getIso6391Name(),
detectedPrimaryLanguage.getScore());

final List<DetectedLanguage> detectedLanguages = detectLanguageResult.getDetectedLanguages();
for (DetectedLanguage detectedLanguage : detectedLanguages) {
System.out.printf("Other detected languages: %s, ISO 6391 Name: %s, Score: %s.%n",
for (DetectedLanguage detectedLanguage : detectLanguageResult.getDetectedLanguages()) {
System.out.printf("Another detected language: %s, ISO 6391 name: %s, score: %s.%n",
detectedLanguage.getName(),
detectedLanguage.getIso6391Name(),
detectedLanguage.getScore());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
import java.util.concurrent.TimeUnit;

/**
* Sample demonstrate how to detect language of a text input in asynchronously call.
* Sample demonstrates how to asynchronously detect the language of an input text.
*/
public class HelloWorldAsync {
/**
* Main method to invoke this demo about how to detect language of a text input.
* Main method to invoke this demo about how to detect the language of an input text.
*
* @param args Unused arguments to the program.
*/
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder()
.subscriptionKey("subscription-key")
.endpoint("https://servicename.cognitiveservices.azure.com/")
.subscriptionKey("{subscription_key}")
.endpoint("https://{servicename}.cognitiveservices.azure.com/")
.buildAsyncClient();

// The text that need be analysed.
Expand All @@ -29,8 +29,14 @@ public static void main(String[] args) {
client.detectLanguage(text).subscribe(
result -> {
final DetectedLanguage primaryLanguage = result.getPrimaryLanguage();
System.out.printf("Detected Language: %s, ISO 6391 Name: %s, Score: %s.%n",
System.out.printf("Detected primary language: %s, ISO 6391 name: %s, score: %s.%n",
primaryLanguage.getName(), primaryLanguage.getIso6391Name(), primaryLanguage.getScore());
for (DetectedLanguage detectedLanguage : result.getDetectedLanguages()) {
System.out.printf("Another detected language: %s, ISO 6391 name: %s, score: %s.%n",
detectedLanguage.getName(),
detectedLanguage.getIso6391Name(),
detectedLanguage.getScore());
}
},
error -> System.err.println("There was an error detecting language of the text." + error),
() -> System.out.println("Language detected."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@
import com.azure.ai.textanalytics.models.NamedEntity;

/**
* Sample demonstrate how to recognize entities of a text input.
* Sample demonstrates how to recognize the entities of an input text.
*/
public class RecognizeEntities {
/**
* Main method to invoke this demo about how to recognize entities of a text input.
* Main method to invoke this demo about how to recognize the entities of an input text.
*
* @param args Unused arguments to the program.
*/
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsClient client = new TextAnalyticsClientBuilder()
.subscriptionKey("subscription-key")
.endpoint("https://servicename.cognitiveservices.azure.com/")
.subscriptionKey("{subscription_key}")
.endpoint("https://{servicename}.cognitiveservices.azure.com/")
.buildClient();

// The text that need be analysed.
String text = "Satya Nadella is the CEO of Microsoft";

for (NamedEntity entity : client.recognizeEntities(text).getNamedEntities()) {
System.out.printf(
"Recognized NamedEntity: %s, NamedEntity Type: %s, NamedEntity Subtype: %s, Offset: %s, Length: %s, Score: %s.%n",
"Recognized entity: %s, entity type: %s, entity subtype: %s, offset: %s, length: %s, score: %s.%n",
entity.getText(),
entity.getType(),
entity.getSubtype() == null || entity.getSubtype().isEmpty() ? "N/A" : entity.getSubtype(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.ai.textanalytics;

import com.azure.ai.textanalytics.models.NamedEntity;

import java.util.concurrent.TimeUnit;

/**
* Sample demonstrates how to asynchronously recognize the entities of an input text.
*/
public class RecognizeEntitiesAsync {
/**
* Main method to invoke this demo about how to recognize the entities of an input text.
*
* @param args Unused arguments to the program.
*/
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder()
.subscriptionKey("{subscription_key}")
.endpoint("https://{servicename}.cognitiveservices.azure.com/")
.buildAsyncClient();

// The text that need be analysed.
String text = "Satya Nadella is the CEO of Microsoft";

client.recognizeEntities(text).subscribe(
result -> {
for (NamedEntity entity : result.getNamedEntities()) {
System.out.printf(
"Recognized entity: %s, entity type: %s, entity subtype: %s, offset: %s, length: %s, score: %s.%n",
entity.getText(),
entity.getType(),
entity.getSubtype() == null || entity.getSubtype().isEmpty() ? "N/A" : entity.getSubtype(),
entity.getOffset(),
entity.getLength(),
entity.getScore());
}
},
error -> System.err.println("There was an error recognizing entities of the text." + error),
() -> System.out.println("Entities recognized."));

// The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep
// the thread so the program does not end before the send operation is complete. Using .block() instead of
// .subscribe() will turn this into a synchronous call.
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException ignored) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@
import com.azure.ai.textanalytics.models.LinkedEntity;

/**
* Sample demonstrate how to recognize linked entities of a text input.
* Sample demonstrates how to recognize the linked entities of an input text.
*/
public class RecognizeLinkedEntities {
/**
* Main method to invoke this demo about how to recognize linked entities of a text input.
* Main method to invoke this demo about how to recognize the linked entities of an input text.
*
* @param args Unused arguments to the program.
*/
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
TextAnalyticsClient client = new TextAnalyticsClientBuilder()
.subscriptionKey("subscription-key")
.endpoint("https://servicename.cognitiveservices.azure.com/")
.subscriptionKey("{subscription_key}")
.endpoint("https://{servicename}.cognitiveservices.azure.com/")
.buildClient();

// The text that need be analysed.
String text = "Old Faithful is a geyser at Yellowstone Park.";

for (LinkedEntity linkedEntity : client.recognizeLinkedEntities(text).getLinkedEntities()) {
System.out.printf("Recognized Linked NamedEntity: %s, URL: %s, Data Source: %s.%n",
System.out.printf("Recognized linked entity: %s, URL: %s, data source: %s.%n",
linkedEntity.getName(),
linkedEntity.getUrl(),
linkedEntity.getDataSource());
Expand Down
Loading