|
1 | | -/*Copyright (c) Microsoft Corporation. All rights reserved. |
2 | | -Licensed under the MIT License.*/ |
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
3 | 3 |
|
4 | 4 | import java.io.*; |
5 | 5 | import java.net.*; |
6 | 6 | import javax.net.ssl.HttpsURLConnection; |
| 7 | +import org.json.*; |
7 | 8 |
|
8 | | -public class HelloWorld { |
| 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 BingSpellCheckv7.java -cp .;lib\* |
| 19 | + * java -cp .;lib\* BingSpellCheckv7 |
| 20 | + */ |
9 | 21 |
|
10 | | - static String host = "https://api.cognitive.microsoft.com"; |
11 | | - static String path = "/bing/v7.0/spellcheck"; |
| 22 | +public class BingSpellCheckv7 { |
| 23 | + |
| 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"; |
12 | 26 |
|
13 | 27 | // NOTE: Replace this example key with a valid subscription key. |
14 | | - static String key = "ENTER KEY HERE"; |
| 28 | + static String subscriptionKey = System.getenv("BING_SPELL_CHECK_SUBSCRIPTION_KEY"); |
15 | 29 |
|
16 | 30 | static String mkt = "en-US"; |
17 | 31 | static String mode = "proof"; |
18 | 32 | static String text = "Hollo, wrld!"; |
19 | 33 |
|
20 | | - public static void check () throws Exception { |
21 | | - String params = "?mkt=" + mkt + "&mode=" + mode; |
22 | | - URL url = new URL(host + path + params); |
23 | | - HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); |
24 | | - connection.setRequestMethod("POST"); |
25 | | - connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); |
26 | | - connection.setRequestProperty("Content-Length", "" + text.length() + 5); |
27 | | - connection.setRequestProperty("Ocp-Apim-Subscription-Key", key); |
28 | | - connection.setDoOutput(true); |
29 | | - |
30 | | - DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); |
31 | | - wr.writeBytes("text=" + text); |
32 | | - wr.flush(); |
33 | | - wr.close(); |
34 | | - |
35 | | - BufferedReader in = new BufferedReader( |
36 | | - new InputStreamReader(connection.getInputStream())); |
37 | | - String line; |
38 | | - while ((line = in.readLine()) != null) { |
39 | | - System.out.println(line); |
40 | | - } |
41 | | - in.close(); |
42 | | - } |
43 | | - |
44 | 34 | public static void main(String[] args) { |
45 | 35 | try { |
46 | | - check (); |
47 | | - } |
48 | | - catch (Exception e) { |
49 | | - 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); |
50 | 65 | } |
51 | 66 | } |
52 | 67 | } |
0 commit comments