Skip to content

Commit 00709dd

Browse files
authored
Updated sample
1 parent ee82f8b commit 00709dd

File tree

1 file changed

+51
-34
lines changed

1 file changed

+51
-34
lines changed

java/Search/BingSpellCheckv7.java

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,67 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
14
import java.io.*;
25
import java.net.*;
36
import javax.net.ssl.HttpsURLConnection;
7+
import org.json.*;
8+
9+
/**
10+
* This sample uses the Bing Spell Check v7 API to check spelling of a sentence.
11+
*
12+
* Include library (for pretty print), add jar to a lib folder:
13+
* https://github.com/stleary/JSON-java
14+
*
15+
* Add your subscription key and endpoint to your environment variables.
16+
*
17+
* Build/run from command line:
18+
* javac BingSpellCheck.java -cp .;lib\*
19+
* java -cp .;lib\* BingSpellCheck
20+
*/
421

5-
public class HelloWorld {
22+
public class BingSpellCheck {
623

7-
// Add your Bing Spell Check subscription key to your environment variables.
8-
static String host = System.getenv("BING_SPELL_CHECK_ENDPOINT");
9-
static String path = "/bing/v7.0/spellcheck";
24+
// Or using the generic endpoint https://api.cognitive.microsoft.com is OK too.
25+
static String endpoint = System.getenv("BING_SPELL_CHECK_ENDPOINT") + "/bing/v7.0/spellcheck";
1026

11-
// Add your Bing Spell Check endpoint to your environment variables.
12-
static String key = System.getenv("BING_SPELL_CHECK_SUBSCRIPTION_KEY");
27+
// NOTE: Replace this example key with a valid subscription key.
28+
static String subscriptionKey = System.getenv("BING_SPELL_CHECK_SUBSCRIPTION_KEY");
1329

1430
static String mkt = "en-US";
1531
static String mode = "proof";
1632
static String text = "Hollo, wrld!";
1733

18-
public static void check () throws Exception {
19-
String params = "?mkt=" + mkt + "&mode=" + mode;
20-
URL url = new URL(host + path + params);
21-
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
22-
connection.setRequestMethod("POST");
23-
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
24-
connection.setRequestProperty("Content-Length", "" + text.length() + 5);
25-
connection.setRequestProperty("Ocp-Apim-Subscription-Key", key);
26-
connection.setDoOutput(true);
27-
28-
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
29-
wr.writeBytes("text=" + text);
30-
wr.flush();
31-
wr.close();
32-
33-
BufferedReader in = new BufferedReader(
34-
new InputStreamReader(connection.getInputStream()));
35-
String line;
36-
while ((line = in.readLine()) != null) {
37-
System.out.println(line);
38-
}
39-
in.close();
40-
}
41-
4234
public static void main(String[] args) {
4335
try {
44-
check ();
45-
}
46-
catch (Exception e) {
47-
System.out.println (e);
36+
String params = "?mkt=" + mkt + "&mode=" + mode;
37+
URL url = new URL(endpoint + params);
38+
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
39+
connection.setRequestMethod("POST");
40+
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
41+
connection.setRequestProperty("Content-Length", "" + text.length() + 5);
42+
connection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
43+
connection.setDoOutput(true);
44+
45+
// Optional
46+
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
47+
wr.writeBytes("text=" + text);
48+
wr.flush();
49+
wr.close();
50+
51+
// Get results
52+
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
53+
54+
String line;
55+
StringBuilder sb = new StringBuilder();
56+
while ((line = in.readLine()) != null) {
57+
sb.append(line);
58+
}
59+
// Pretty print
60+
System.out.println((new JSONObject(sb.toString())).toString(4));
61+
62+
in.close();
63+
} catch (Exception e) {
64+
System.out.println(e);
4865
}
4966
}
5067
}

0 commit comments

Comments
 (0)