|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
1 | 4 | 'use strict'; |
2 | 5 |
|
3 | | -let https = require ('https'); |
| 6 | +const request = require ('request') |
4 | 7 |
|
5 | | -// Add your Bing Spell Check endpoint to your environment variables. |
6 | | -let host = process.env['BING_SPELL_CHECK_ENDPOINT'] |
7 | | -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 | + */ |
8 | 13 |
|
9 | | -// Add your Bing Spell Check subscription key to your environment variables. |
10 | | -// Also note v5 and v7 require separate subscription keys. |
| 14 | +// Add your Bing Spell Check key and endpoint (host) to your environment variables. |
| 15 | +// Note v5 and v7 require separate subscription keys. |
11 | 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" |
12 | 22 |
|
13 | 23 | // These values are used for optional headers (see below). |
14 | 24 | // let CLIENT_ID = "<Client ID from Previous Response Goes Here>"; |
15 | 25 | // let CLIENT_IP = "999.999.999.999"; |
16 | 26 | // let CLIENT_LOCATION = "+90.0000000000000;long: 00.0000000000000;re:100.000000000000"; |
17 | 27 |
|
18 | | -let mkt = "en-US"; |
19 | | -let mode = "proof"; |
20 | | -let text = "Hollo, wrld!"; |
21 | | -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 | +} |
22 | 37 |
|
23 | 38 | let request_params = { |
24 | | - method : 'POST', |
25 | | - hostname : host, |
26 | | - path : path + query_string, |
27 | | - headers : { |
28 | | - 'Content-Type' : 'application/x-www-form-urlencoded', |
29 | | - 'Content-Length' : text.length + 5, |
30 | | - 'Ocp-Apim-Subscription-Key' : key, |
31 | | -// 'X-Search-Location' : CLIENT_LOCATION, |
32 | | -// 'X-MSEdge-ClientID' : CLIENT_ID, |
33 | | -// 'X-MSEdge-ClientIP' : CLIENT_ID, |
34 | | - } |
35 | | -}; |
36 | | - |
37 | | -let response_handler = function (response) { |
38 | | - let body = ''; |
39 | | - response.on ('data', function (d) { |
40 | | - body += d; |
41 | | - }); |
42 | | - response.on ('end', function () { |
43 | | - console.log (body); |
44 | | - }); |
45 | | - response.on ('error', function (e) { |
46 | | - console.log ('Error: ' + e.message); |
47 | | - }); |
48 | | -}; |
49 | | - |
50 | | -let req = https.request (request_params, response_handler); |
51 | | -req.write ("text=" + text); |
52 | | -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