|
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 | 'use strict'; |
5 | 5 |
|
6 | | -let https = require ('https'); |
| 6 | +const request = require ('request') |
7 | 7 |
|
8 | | -let host = 'api.cognitive.microsoft.com'; |
9 | | -let path = '/bing/v7.0/spellcheck'; |
| 8 | +/** |
| 9 | + * This sample uses the Bing Spell Check API to check the spelling of a sentence. |
| 10 | + * It returns the identified misspellings with suggestions for correctly spelled words |
| 11 | + * Plus, a score of what are deemed to be the best matches for suggestions. |
| 12 | + */ |
10 | 13 |
|
11 | | -/* NOTE: Replace this example key with a valid subscription key (see the Prequisites section above). Also note v5 and v7 require separate subscription keys. */ |
12 | | -let key = 'ENTER KEY HERE'; |
| 14 | +// Add your Bing Spell Check key and endpoint (host) to your environment variables. |
| 15 | +// Note v5 and v7 require separate subscription keys. |
| 16 | +let key = process.env['BING_SPELL_CHECK_SUBSCRIPTION_KEY'] |
| 17 | +let endpoint = process.env['BING_SPELL_CHECK_ENDPOINT'] + '/bing/v7.0/spellcheck/' |
| 18 | + |
| 19 | +let text = "Hollo, wrld!" |
| 20 | +let mode = "proof" |
| 21 | +let mkt = "en-US" |
13 | 22 |
|
14 | 23 | // These values are used for optional headers (see below). |
15 | 24 | // let CLIENT_ID = "<Client ID from Previous Response Goes Here>"; |
16 | 25 | // let CLIENT_IP = "999.999.999.999"; |
17 | 26 | // let CLIENT_LOCATION = "+90.0000000000000;long: 00.0000000000000;re:100.000000000000"; |
18 | 27 |
|
19 | | -let mkt = "en-US"; |
20 | | -let mode = "proof"; |
21 | | -let text = "Hollo, wrld!"; |
22 | | -let query_string = "?mkt=" + mkt + "&mode=" + mode; |
| 28 | +let headers = { |
| 29 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 30 | + 'Content-Length': text.length + 5, |
| 31 | + 'Ocp-Apim-Subscription-Key': key |
| 32 | + // Optional Headers |
| 33 | + // 'X-Search-Location' : CLIENT_LOCATION, |
| 34 | + // 'X-MSEdge-ClientID' : CLIENT_ID, |
| 35 | + // 'X-MSEdge-ClientIP' : CLIENT_ID, |
| 36 | +} |
23 | 37 |
|
24 | 38 | let request_params = { |
25 | | - method : 'POST', |
26 | | - hostname : host, |
27 | | - path : path + query_string, |
28 | | - headers : { |
29 | | - 'Content-Type' : 'application/x-www-form-urlencoded', |
30 | | - 'Content-Length' : text.length + 5, |
31 | | - 'Ocp-Apim-Subscription-Key' : key, |
32 | | -// 'X-Search-Location' : CLIENT_LOCATION, |
33 | | -// 'X-MSEdge-ClientID' : CLIENT_ID, |
34 | | -// 'X-MSEdge-ClientIP' : CLIENT_ID, |
35 | | - } |
36 | | -}; |
37 | | - |
38 | | -let response_handler = function (response) { |
39 | | - let body = ''; |
40 | | - response.on ('data', function (d) { |
41 | | - body += d; |
42 | | - }); |
43 | | - response.on ('end', function () { |
44 | | - console.log (body); |
45 | | - }); |
46 | | - response.on ('error', function (e) { |
47 | | - console.log ('Error: ' + e.message); |
48 | | - }); |
49 | | -}; |
50 | | - |
51 | | -let req = https.request (request_params, response_handler); |
52 | | -req.write ("text=" + text); |
53 | | -req.end (); |
| 39 | + method: 'POST', |
| 40 | + url: endpoint, |
| 41 | + headers: headers, |
| 42 | + qs: { |
| 43 | + mode: mode, |
| 44 | + mkt: mkt, |
| 45 | + text: text |
| 46 | + }, |
| 47 | + json: true |
| 48 | +} |
| 49 | + |
| 50 | +request(request_params, function (error, response, body) { |
| 51 | + console.error('error:', error) |
| 52 | + console.log('statusCode:', response && response.statusCode) |
| 53 | + |
| 54 | + // Print suggestions for each misspelled word (token) |
| 55 | + body.flaggedTokens.forEach(token => { |
| 56 | + console.log(token) |
| 57 | + }) |
| 58 | +}) |
0 commit comments