|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using Newtonsoft.Json; |
1 | 5 | using System; |
2 | 6 | using System.Collections.Generic; |
3 | | -using System.Linq; |
4 | 7 | using System.Net.Http; |
5 | 8 | using System.Net.Http.Headers; |
6 | | -using System.Text; |
7 | | -using Newtonsoft.Json; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +/* |
| 12 | + * This sample uses the Azure Bing Spell Check API to check the spelling of a query. |
| 13 | + * It then offers suggestions for corrections. |
| 14 | + * Bing Spell Check API: |
| 15 | + * https://docs.microsoft.com/en-us/rest/api/cognitiveservices-bingsearch/bing-spell-check-api-v7-reference |
| 16 | + */ |
8 | 17 |
|
9 | | -namespace SpellCheckSample |
| 18 | +namespace BingSpellCheck |
10 | 19 | { |
11 | 20 | class Program |
12 | 21 | { |
13 | | - // Add your Azure Bing Spell Check endpoint to your environment variables. |
14 | | - static string host = Environment.GetEnvironmentVariable("BING_SPELL_CHECK_ENDPOINT"); |
| 22 | + // Add your Azure Bing Spell Check key and endpoint to your environment variables. |
| 23 | + static string subscriptionKey = Environment.GetEnvironmentVariable("BING_SPELL_CHECK_SUBSCRIPTION_KEY"); |
| 24 | + static string endpoint = Environment.GetEnvironmentVariable("BING_SPELL_CHECK_ENDPOINT"); |
15 | 25 | static string path = "/bing/v7.0/spellcheck?"; |
16 | 26 |
|
17 | 27 | // For a list of available markets, go to: |
18 | 28 | // https://docs.microsoft.com/rest/api/cognitiveservices/bing-autosuggest-api-v7-reference#market-codes |
19 | | - static string params_ = "mkt=en-US&mode=proof"; |
| 29 | + static string market = "en-US"; |
20 | 30 |
|
21 | | - // Add your Azure Bing Spell Check subscription key to your environment variables. |
22 | | - static string key = Environment.GetEnvironmentVariable("BING_SPELL_CHECK_SUBSCRIPTION_KEY"); |
23 | | - //text to be spell-checked |
24 | | - static string text = "Hollo, wrld!"; |
| 31 | + static string mode = "proof"; |
| 32 | + |
| 33 | + static string query = "Hollo, wrld!"; |
25 | 34 |
|
26 | 35 | // These properties are used for optional headers (see below). |
27 | 36 | // static string ClientId = "<Client ID from Previous Response Goes Here>"; |
28 | | - //static string ClientId = "2325577A61966D252A475CD760C96C03"; |
29 | 37 | // static string ClientIp = "999.999.999.999"; |
30 | 38 | // static string ClientLocation = "+90.0000000000000;long: 00.0000000000000;re:100.000000000000"; |
31 | 39 |
|
32 | | - async static void SpellCheck() |
| 40 | + public async static Task Main(string[] args) |
33 | 41 | { |
34 | 42 | HttpClient client = new HttpClient(); |
35 | | - client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", key); |
| 43 | + client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey); |
36 | 44 |
|
37 | 45 | // The following headers are optional, but it is recommended they be treated as required. |
38 | 46 | // These headers help the service return more accurate results. |
39 | | - //client.DefaultRequestHeaders.Add("X-Search-Location", ClientLocation); |
40 | | - //client.DefaultRequestHeaders.Add("X-MSEdge-ClientID", ClientId); |
41 | | - //client.DefaultRequestHeaders.Add("X-MSEdge-ClientIP", ClientIp); |
| 47 | + // client.DefaultRequestHeaders.Add("X-Search-Location", ClientLocation); |
| 48 | + // client.DefaultRequestHeaders.Add("X-MSEdge-ClientID", ClientId); |
| 49 | + // client.DefaultRequestHeaders.Add("X-MSEdge-ClientIP", ClientIp); |
42 | 50 |
|
43 | 51 | HttpResponseMessage response = new HttpResponseMessage(); |
44 | | - string uri = host + path + params_; |
| 52 | + string uri = endpoint + path; |
45 | 53 |
|
46 | 54 | List<KeyValuePair<string, string>> values = new List<KeyValuePair<string, string>>(); |
47 | | - values.Add(new KeyValuePair<string, string>("text", text)); |
| 55 | + values.Add(new KeyValuePair<string, string>("mkt", market)); |
| 56 | + values.Add(new KeyValuePair<string, string>("mode", mode)); |
| 57 | + values.Add(new KeyValuePair<string, string>("text", query)); |
48 | 58 |
|
49 | 59 | using (FormUrlEncodedContent content = new FormUrlEncodedContent(values)) |
50 | 60 | { |
51 | 61 | content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); |
52 | 62 | response = await client.PostAsync(uri, content); |
53 | 63 | } |
54 | | - //Get the client ID header from your request |
55 | | - string client_id; |
56 | | - if (response.Headers.TryGetValues("X-MSEdge-ClientID", out IEnumerable<string> header_values)) |
57 | | - { |
58 | | - client_id = header_values.First(); |
59 | | - Console.WriteLine("Client ID: " + client_id); |
60 | | - } |
61 | | - //print the response |
62 | 64 |
|
63 | | - string contentString = await response.Content.ReadAsStringAsync(); |
| 65 | + // Get the client ID header from your request (optional) |
| 66 | + // string client_id; |
| 67 | + // if (response.Headers.TryGetValues("X-MSEdge-ClientID", out IEnumerable<string> header_values)) |
| 68 | + // { |
| 69 | + // client_id = header_values.First(); |
| 70 | + // Console.WriteLine("Client ID: " + client_id); |
| 71 | + // } |
64 | 72 |
|
65 | | - //deserialize the JSON response from the API |
| 73 | + string contentString = await response.Content.ReadAsStringAsync(); |
| 74 | + //Deserialize the JSON response from the API |
66 | 75 | dynamic jsonObj = JsonConvert.DeserializeObject(contentString); |
67 | 76 | Console.WriteLine(jsonObj); |
68 | | - |
69 | 77 | } |
70 | | - |
71 | | - static void Main(string[] args) |
72 | | - { |
73 | | - SpellCheck(); |
74 | | - Console.ReadLine(); |
75 | | - } |
76 | | - |
77 | 78 | } |
78 | 79 | } |
0 commit comments