|
15 | 15 |
|
16 | 16 | public class TextAnalytics { |
17 | 17 | /** |
18 | | - * The API Subscription Key that was generated when you created a cogntiive |
| 18 | + * The API Subscription Key that was generated when you created a cognitive |
19 | 19 | * services resource in Azure. Make sure to change this. |
20 | 20 | */ |
21 | 21 | private static final String subscriptionKey = "enter-your-key-here"; |
@@ -62,41 +62,70 @@ public static String callTextAnalyticsService(URL serviceUrl, String text) { |
62 | 62 | } |
63 | 63 |
|
64 | 64 | public static void main(String[] args) { |
65 | | - /* For language detection your input documents don't have to specify language */ |
66 | | - Documents documents = new Documents(); |
67 | | - documents.add("1", "This is a document written in English."); |
68 | | - documents.add("2", "Este es un document escrito en Español."); |
69 | | - documents.add("3", "这是一个用中文写的文件"); |
70 | | - |
71 | | - String text = new Gson().toJson(documents); |
72 | | - |
73 | | - String languageResult = LanguageDetection.DetectLanguage(host, subscriptionKey, text); |
74 | | - System.out.println("LANGUAGE RESPONSE:"); |
75 | | - System.out.println(languageResult); |
76 | | - |
77 | 65 | /* |
78 | 66 | * For sentiment, keyphrase, and entity recognition your input documents must |
79 | 67 | * specify language so that the proper model is used. If a language isn't |
80 | | - * specified, we assume english is the document language. |
| 68 | + * specified, we assume english is the document language. For language |
| 69 | + * detection, you need not specify a language. |
81 | 70 | */ |
82 | | - Documents multiLanguageDocuments = new Documents(); |
83 | | - multiLanguageDocuments.add("1", "I love Seattle. It's a great city.", "en"); |
84 | | - multiLanguageDocuments.add("2", "Este clima me encanta. Esta muy soleado hoy en Buenos Aires.", "es"); |
85 | 71 |
|
86 | | - String multiLanguageText = new Gson().toJson(multiLanguageDocuments); |
| 72 | + /* Sentiment Analysis */ |
| 73 | + Documents multiLanguageSentimentDocuments = new Documents(); |
| 74 | + multiLanguageSentimentDocuments.add("1", "I had the best day of my life.", "en"); |
| 75 | + multiLanguageSentimentDocuments.add("2", "This was a waste of my time. The speaker put me to sleep.", "en"); |
| 76 | + multiLanguageSentimentDocuments.add("3", "No tengo dinero ni nada que dar...", "es"); |
| 77 | + multiLanguageSentimentDocuments.add("4", |
| 78 | + "L'hotel veneziano era meraviglioso. È un bellissimo pezzo di architettura.", "it"); |
| 79 | + |
| 80 | + String multiLanguageSentimentText = new Gson().toJson(multiLanguageSentimentDocuments); |
87 | 81 |
|
88 | | - String sentimentResult = SentimentAnalysis.AnalyzeSentiment(host, subscriptionKey, multiLanguageText); |
| 82 | + String sentimentResult = SentimentAnalysis.AnalyzeSentiment(host, subscriptionKey, multiLanguageSentimentText); |
89 | 83 | System.out.println("SENTIMENT RESPONSE:"); |
90 | 84 | System.out.println(sentimentResult); |
| 85 | + System.out.println(); |
91 | 86 |
|
92 | | - String keyPhraseExtractionResult = KeyPhraseExtraction.DetectKeyPhrases(host, subscriptionKey, |
93 | | - multiLanguageText); |
94 | | - System.out.println("KEY PHRASE RESPONSE:"); |
95 | | - System.out.println(keyPhraseExtractionResult); |
| 87 | + /* For language detection your input documents don't have to specify language */ |
| 88 | + Documents languageDetectionDocuments = new Documents(); |
| 89 | + languageDetectionDocuments.add("1", "This is a document written in English."); |
| 90 | + languageDetectionDocuments.add("2", "Este es un document escrito en Español."); |
| 91 | + languageDetectionDocuments.add("3", "这是一个用中文写的文件"); |
| 92 | + |
| 93 | + String languageDetectionText = new Gson().toJson(languageDetectionDocuments); |
96 | 94 |
|
97 | | - String entityRecognitionResult = EntityRecognition.DetectEntities(host, subscriptionKey, multiLanguageText); |
| 95 | + String languageResult = LanguageDetection.DetectLanguage(host, subscriptionKey, languageDetectionText); |
| 96 | + System.out.println("LANGUAGE RESPONSE:"); |
| 97 | + System.out.println(languageResult); |
| 98 | + System.out.println(); |
| 99 | + |
| 100 | + /* Entity Recognition */ |
| 101 | + Documents multiLanguageNerDocuments = new Documents(); |
| 102 | + multiLanguageNerDocuments.add("1", |
| 103 | + "Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975, to develop and sell BASIC interpreters for the Altair 8800.", |
| 104 | + "en"); |
| 105 | + multiLanguageNerDocuments.add("2", |
| 106 | + "La sede principal de Microsoft se encuentra en la ciudad de Redmond, a 21 kilómetros de Seattle.", |
| 107 | + "es"); |
| 108 | + |
| 109 | + String multiLanguageNerText = new Gson().toJson(multiLanguageNerDocuments); |
| 110 | + String entityRecognitionResult = EntityRecognition.DetectEntities(host, subscriptionKey, multiLanguageNerText); |
98 | 111 | System.out.println("ENTITY RECOGNITION RESPONSE:"); |
99 | 112 | System.out.println(entityRecognitionResult); |
| 113 | + System.out.println(); |
| 114 | + |
| 115 | + /* KeyPhrase Extraction */ |
| 116 | + Documents multiLanguageKpeDocuments = new Documents(); |
| 117 | + multiLanguageKpeDocuments.add("1", "猫は幸せ", "ja"); |
| 118 | + multiLanguageKpeDocuments.add("2", "Fahrt nach Stuttgart und dann zum Hotel zu Fu.", "de"); |
| 119 | + multiLanguageKpeDocuments.add("3", "My cat might need to see a veterinarian.", "en"); |
| 120 | + multiLanguageKpeDocuments.add("4", "A mi me encanta el fútbol!", "es"); |
| 121 | + |
| 122 | + String multiLanguageKpeText = new Gson().toJson(multiLanguageKpeDocuments); |
| 123 | + |
| 124 | + String keyPhraseExtractionResult = KeyPhraseExtraction.DetectKeyPhrases(host, subscriptionKey, |
| 125 | + multiLanguageKpeText); |
| 126 | + System.out.println("KEY PHRASE RESPONSE:"); |
| 127 | + System.out.println(keyPhraseExtractionResult); |
| 128 | + System.out.println(); |
100 | 129 | } |
101 | 130 | } |
102 | 131 |
|
|
0 commit comments