|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
1 | 4 | import java.io.*; |
2 | 5 | import java.net.*; |
3 | 6 | 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 | + */ |
4 | 21 |
|
5 | | -public class HelloWorld { |
| 22 | +public class BingSpellCheck { |
6 | 23 |
|
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"; |
10 | 26 |
|
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"); |
13 | 29 |
|
14 | 30 | static String mkt = "en-US"; |
15 | 31 | static String mode = "proof"; |
16 | 32 | static String text = "Hollo, wrld!"; |
17 | 33 |
|
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 | | - |
42 | 34 | public static void main(String[] args) { |
43 | 35 | 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); |
48 | 65 | } |
49 | 66 | } |
50 | 67 | } |
0 commit comments