Skip to content

Commit 30faa6c

Browse files
authored
Sample updated
1 parent 72d2d75 commit 30faa6c

File tree

1 file changed

+70
-70
lines changed
  • java/Language/TextAnalytics/src/main/java/com/microsoft/azure/textanalytics/samples

1 file changed

+70
-70
lines changed

java/Language/TextAnalytics/src/main/java/com/microsoft/azure/textanalytics/samples/TextAnalytics.java

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
/**
2-
* Copyright (c) Microsoft Corporation. All rights reserved.
3-
* Licensed under the MIT License. See License.txt in the project root for
4-
* license information.
5-
*/
6-
7-
package com.microsoft.azure.textanalytics.samples;
1+
// Copyright (c) Microsoft Corporation. All rights reserved. MIT License.
2+
// See License.txt in the project root for license information.
83

94
import java.io.*;
105
import java.net.*;
@@ -13,63 +8,34 @@
138

149
import com.google.gson.Gson;
1510

16-
public class TextAnalytics {
17-
/**
18-
* The API Subscription Key that was generated when you created a cognitive
19-
* services resource in Azure. Make sure to change this.
20-
*/
21-
private static final String subscriptionKey = "enter-your-key-here";
22-
/**
23-
* The host of the cognitive services service. We will then append the Text
24-
* Analytics service path to this to generate the URI against which we will make
25-
* requests. Please make sure to enter the region to match the Azure region in
26-
* which your cognitive services resource was deployed to.
27-
*/
28-
private static final String host = "https://<region>.api.cognitive.microsoft.com";
29-
30-
public static void addAuthenticationHeader(HttpsURLConnection connection) {
31-
connection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
32-
}
33-
34-
public static String callTextAnalyticsService(URL serviceUrl, String text) {
35-
try {
36-
byte[] encoded_input;
37-
encoded_input = text.getBytes("UTF-8");
38-
39-
HttpsURLConnection connection = (HttpsURLConnection) serviceUrl.openConnection();
40-
connection.setRequestMethod("POST");
41-
connection.setRequestProperty("Content-Type", "application/json");
42-
addAuthenticationHeader(connection);
43-
connection.setDoOutput(true);
44-
45-
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
46-
wr.write(encoded_input, 0, encoded_input.length);
47-
wr.flush();
48-
wr.close();
49-
50-
StringBuilder response = new StringBuilder();
51-
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
52-
String line;
53-
while ((line = in.readLine()) != null) {
54-
response.append(line);
55-
}
56-
in.close();
11+
/**
12+
* This sample uses the Text Analytics API to analyze text for: sentiment,
13+
* language detection, entity recognition, and key phrase extraction.
14+
*
15+
* Use this library: https://github.com/google/gson
16+
* If running from the command line, add the gson jar to a lib folder.
17+
*
18+
* Add your subscription key and endpoint to your environment variables.
19+
*
20+
* Download the project and run in an IDE, or build/run this file from the command line:
21+
* javac TextAnalytics.java -encoding utf-8 -cp .;lib\*
22+
* java -cp .;lib\* TextAnalytics
23+
*/
5724

58-
return response.toString();
59-
} catch (Exception e) {
60-
return e.getMessage();
61-
}
62-
}
25+
public class TextAnalytics {
26+
// The API subscription key and endpoint generated when you created an Azure Cognitive Services resource.
27+
static String subscriptionKey = System.getenv("TEXT_ANALYTICS_SUBSCRIPTION_KEY");
28+
static String endpoint = System.getenv("TEXT_ANALYTICS_ENDPOINT");
6329

6430
public static void main(String[] args) {
65-
/*
31+
/**
6632
* For sentiment, keyphrase, and entity recognition your input documents must
6733
* specify language so that the proper model is used. If a language isn't
6834
* specified, we assume english is the document language. For language
6935
* detection, you need not specify a language.
7036
*/
7137

72-
/* Sentiment Analysis */
38+
// Sentiment Analysis
7339
Documents multiLanguageSentimentDocuments = new Documents();
7440
multiLanguageSentimentDocuments.add("1", "I had the best day of my life.", "en");
7541
multiLanguageSentimentDocuments.add("2", "This was a waste of my time. The speaker put me to sleep.", "en");
@@ -79,25 +45,25 @@ public static void main(String[] args) {
7945

8046
String multiLanguageSentimentText = new Gson().toJson(multiLanguageSentimentDocuments);
8147

82-
String sentimentResult = SentimentAnalysis.AnalyzeSentiment(host, subscriptionKey, multiLanguageSentimentText);
48+
String sentimentResult = SentimentAnalysis.AnalyzeSentiment(endpoint, subscriptionKey, multiLanguageSentimentText);
8349
System.out.println("SENTIMENT RESPONSE:");
8450
System.out.println(sentimentResult);
8551
System.out.println();
8652

87-
/* For language detection your input documents don't have to specify language */
53+
// For language detection your input documents don't have to specify language
8854
Documents languageDetectionDocuments = new Documents();
8955
languageDetectionDocuments.add("1", "This is a document written in English.");
9056
languageDetectionDocuments.add("2", "Este es un document escrito en Español.");
9157
languageDetectionDocuments.add("3", "这是一个用中文写的文件");
9258

9359
String languageDetectionText = new Gson().toJson(languageDetectionDocuments);
9460

95-
String languageResult = LanguageDetection.DetectLanguage(host, subscriptionKey, languageDetectionText);
61+
String languageResult = LanguageDetection.DetectLanguage(endpoint, subscriptionKey, languageDetectionText);
9662
System.out.println("LANGUAGE RESPONSE:");
9763
System.out.println(languageResult);
9864
System.out.println();
9965

100-
/* Entity Recognition */
66+
// Entity Recognition
10167
Documents multiLanguageNerDocuments = new Documents();
10268
multiLanguageNerDocuments.add("1",
10369
"Microsoft was founded by Bill Gates and Paul Allen on April 4, 1975, to develop and sell BASIC interpreters for the Altair 8800.",
@@ -107,12 +73,12 @@ public static void main(String[] args) {
10773
"es");
10874

10975
String multiLanguageNerText = new Gson().toJson(multiLanguageNerDocuments);
110-
String entityRecognitionResult = EntityRecognition.DetectEntities(host, subscriptionKey, multiLanguageNerText);
76+
String entityRecognitionResult = EntityRecognition.DetectEntities(endpoint, subscriptionKey, multiLanguageNerText);
11177
System.out.println("ENTITY RECOGNITION RESPONSE:");
11278
System.out.println(entityRecognitionResult);
11379
System.out.println();
11480

115-
/* KeyPhrase Extraction */
81+
// KeyPhrase Extraction
11682
Documents multiLanguageKpeDocuments = new Documents();
11783
multiLanguageKpeDocuments.add("1", "猫は幸せ", "ja");
11884
multiLanguageKpeDocuments.add("2", "Fahrt nach Stuttgart und dann zum Hotel zu Fu.", "de");
@@ -121,12 +87,46 @@ public static void main(String[] args) {
12187

12288
String multiLanguageKpeText = new Gson().toJson(multiLanguageKpeDocuments);
12389

124-
String keyPhraseExtractionResult = KeyPhraseExtraction.DetectKeyPhrases(host, subscriptionKey,
90+
String keyPhraseExtractionResult = KeyPhraseExtraction.DetectKeyPhrases(endpoint, subscriptionKey,
12591
multiLanguageKpeText);
12692
System.out.println("KEY PHRASE RESPONSE:");
12793
System.out.println(keyPhraseExtractionResult);
12894
System.out.println();
12995
}
96+
97+
public static void addAuthenticationHeader(HttpsURLConnection connection) {
98+
connection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
99+
}
100+
101+
public static String callTextAnalyticsService(URL serviceUrl, String text) {
102+
try {
103+
byte[] encoded_input;
104+
encoded_input = text.getBytes("UTF-8");
105+
106+
HttpsURLConnection connection = (HttpsURLConnection) serviceUrl.openConnection();
107+
connection.setRequestMethod("POST");
108+
connection.setRequestProperty("Content-Type", "application/json");
109+
addAuthenticationHeader(connection);
110+
connection.setDoOutput(true);
111+
112+
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
113+
wr.write(encoded_input, 0, encoded_input.length);
114+
wr.flush();
115+
wr.close();
116+
117+
StringBuilder response = new StringBuilder();
118+
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
119+
String line;
120+
while ((line = in.readLine()) != null) {
121+
response.append(line);
122+
}
123+
in.close();
124+
125+
return response.toString();
126+
} catch (Exception e) {
127+
return e.getMessage();
128+
}
129+
}
130130
}
131131

132132
class Document {
@@ -161,11 +161,11 @@ public void add(String id, String text, String language) {
161161
}
162162

163163
class LanguageDetection {
164-
public static String DetectLanguage(String host, String subscriptionKey, String text) {
164+
public static String DetectLanguage(String endpoint, String subscriptionKey, String text) {
165165
String path = "/text/analytics/v2.1/languages";
166166

167167
try {
168-
URL serviceUrl = new URL(host + path);
168+
URL serviceUrl = new URL(endpoint + path);
169169
String response = TextAnalytics.callTextAnalyticsService(serviceUrl, text);
170170
return response;
171171
} catch (Exception e) {
@@ -175,11 +175,11 @@ public static String DetectLanguage(String host, String subscriptionKey, String
175175
}
176176

177177
class SentimentAnalysis {
178-
public static String AnalyzeSentiment(String host, String subscriptionKey, String text) {
178+
public static String AnalyzeSentiment(String endpoint, String subscriptionKey, String text) {
179179
String path = "/text/analytics/v2.1/sentiment";
180180

181181
try {
182-
URL serviceUrl = new URL(host + path);
182+
URL serviceUrl = new URL(endpoint + path);
183183
String response = TextAnalytics.callTextAnalyticsService(serviceUrl, text);
184184
return response;
185185
} catch (Exception e) {
@@ -189,11 +189,11 @@ public static String AnalyzeSentiment(String host, String subscriptionKey, Strin
189189
}
190190

191191
class KeyPhraseExtraction {
192-
public static String DetectKeyPhrases(String host, String subscriptionKey, String text) {
192+
public static String DetectKeyPhrases(String endpoint, String subscriptionKey, String text) {
193193
String path = "/text/analytics/v2.1/keyPhrases";
194194

195195
try {
196-
URL serviceUrl = new URL(host + path);
196+
URL serviceUrl = new URL(endpoint + path);
197197
String response = TextAnalytics.callTextAnalyticsService(serviceUrl, text);
198198
return response;
199199
} catch (Exception e) {
@@ -203,11 +203,11 @@ public static String DetectKeyPhrases(String host, String subscriptionKey, Strin
203203
}
204204

205205
class EntityRecognition {
206-
public static String DetectEntities(String host, String subscriptionKey, String text) {
206+
public static String DetectEntities(String endpoint, String subscriptionKey, String text) {
207207
String path = "/text/analytics/v2.1/entities";
208208

209209
try {
210-
URL serviceUrl = new URL(host + path);
210+
URL serviceUrl = new URL(endpoint + path);
211211
String response = TextAnalytics.callTextAnalyticsService(serviceUrl, text);
212212
return response;
213213
} catch (Exception e) {

0 commit comments

Comments
 (0)