Skip to content

Commit 3689ecf

Browse files
authored
Updated the structure
1 parent 28b74f4 commit 3689ecf

File tree

1 file changed

+38
-37
lines changed

1 file changed

+38
-37
lines changed

dotnet/Search/BingSpellCheckv7.cs

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,79 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Newtonsoft.Json;
15
using System;
26
using System.Collections.Generic;
3-
using System.Linq;
47
using System.Net.Http;
58
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+
*/
817

9-
namespace SpellCheckSample
18+
namespace BingSpellCheck
1019
{
1120
class Program
1221
{
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");
1525
static string path = "/bing/v7.0/spellcheck?";
1626

1727
// For a list of available markets, go to:
1828
// 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";
2030

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!";
2534

2635
// These properties are used for optional headers (see below).
2736
// static string ClientId = "<Client ID from Previous Response Goes Here>";
28-
//static string ClientId = "2325577A61966D252A475CD760C96C03";
2937
// static string ClientIp = "999.999.999.999";
3038
// static string ClientLocation = "+90.0000000000000;long: 00.0000000000000;re:100.000000000000";
3139

32-
async static void SpellCheck()
40+
public async static Task Main(string[] args)
3341
{
3442
HttpClient client = new HttpClient();
35-
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", key);
43+
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
3644

3745
// The following headers are optional, but it is recommended they be treated as required.
3846
// 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);
4250

4351
HttpResponseMessage response = new HttpResponseMessage();
44-
string uri = host + path + params_;
52+
string uri = endpoint + path;
4553

4654
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));
4858

4959
using (FormUrlEncodedContent content = new FormUrlEncodedContent(values))
5060
{
5161
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
5262
response = await client.PostAsync(uri, content);
5363
}
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
6264

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+
// }
6472

65-
//deserialize the JSON response from the API
73+
string contentString = await response.Content.ReadAsStringAsync();
74+
//Deserialize the JSON response from the API
6675
dynamic jsonObj = JsonConvert.DeserializeObject(contentString);
6776
Console.WriteLine(jsonObj);
68-
6977
}
70-
71-
static void Main(string[] args)
72-
{
73-
SpellCheck();
74-
Console.ReadLine();
75-
}
76-
7778
}
7879
}

0 commit comments

Comments
 (0)