Bing Spell Check API v7的实现返回0 flaggedTokens

时间:2017-06-21 20:41:28

标签: php api spell-checking bing bing-api

我正在尝试实施Bing Spell Check API v7。这是我目前的职能:

# spell check
function bing_spell_check($q, $lang) {
    $param = array();
    $param['appName'] = PRO_NAME;
    $param['text'] = $q;
    $param['setLang'] = $lang;
    $url = 'https://api.cognitive.microsoft.com/bing/v7.0/SpellCheck?'.http_build_query($param);
    $process = curl_init();
    curl_setopt($process, CURLOPT_URL, $url);
    curl_setopt($process, CURLOPT_TIMEOUT, 10);
    curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($process, CURLOPT_HTTPHEADER,
        array(
            'Accept: application/ld+json',
            'Ocp-Apim-Subscription-Key: '.PRO_BING_KEY_SPELL
        )
    );
    $response = curl_exec($process);
    return $response;
}

问题在于这个例子:

print_r(bing_spell_check('en', 'whts yur name?'));

返回:

{  
   "@context":{  
      "@vocab":"http:\/\/bingapis.com\/v7\/schemas\/",
      "s":"http:\/\/schema.org\/",
      "@base":"https:\/\/api.cognitive.microsoft.com\/api\/v7\/"
   },
   "@type":"SpellCheck",
   "flaggedTokens":[
   ]
}

这意味着它没有发现任何错误。我在Bing's test tool中运行了相同的测试,我收到了这个结构:

{
  "_type": "SpellCheck",
  "FlaggedTokens": [
    {
      "Offset": 0,
      "Token": "whts",
      "Type": "UnknownToken",
      "Suggestions": [
        {
          "suggestion": "what's",
          "Score": 0.909352914464075
        },
        {
          "suggestion": "whats",
          "Score": 0.810588859407343
        },
        {
          "suggestion": "what is",
          "Score": 0.680176771139565
        }
      ]
    },
    {
      "Offset": 5,
      "Token": "yur",
      "Type": "UnknownToken",
      "Suggestions": [
        {
          "suggestion": "your",
          "Score": 0.909352914464075
        }
      ]
    }
  ]
}

我错过了什么。有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

这很简单,你只是混淆了你的函数参数。

function bing_spell_check($q, $lang) {}

bing_spell_check('en', 'whts yur name?');

问题就出现了,$q现在是en$lang现在是whts yur name?。因此,当您创建$param数组时,您将语言和文本混合在一起(这将导致拼写检查API无声地失败)。您可以通过更改参数顺序来解决此问题:

function bing_spell_check($lang, $q) {}

现在你的代码

<?php
define(PRO_BING_KEY_SPELL, 'XXX');
define(PRO_NAME, 'XXX');

function bing_spell_check($lang, $q) {
    $param = array();
    $param['appName'] = PRO_NAME;
    $param['text'] = $q;
    $param['setLang'] = $lang;
    $url = 'https://api.cognitive.microsoft.com/bing/v7.0/spellcheck?'.http_build_query($param);
    $process = curl_init();
    curl_setopt($process, CURLOPT_URL, $url);
    curl_setopt($process, CURLOPT_TIMEOUT, 10);
    curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($process, CURLOPT_HTTPHEADER,
        array(
            'Accept: application/ld+json',
            'Ocp-Apim-Subscription-Key: '.PRO_BING_KEY_SPELL
        )
    );
    $response = curl_exec($process);
    return $response;
}

print_r(bing_spell_check('en', 'whts yur name?'));
?>

结果

{
    "@context": {
        "@vocab": "http:\/\/bingapis.com\/v7\/schemas\/",
        "s": "http:\/\/schema.org\/",
        "@base": "https:\/\/api.cognitive.microsoft.com\/api\/v7\/"
    },
    "@type": "SpellCheck",
    "flaggedTokens": [{
        "offset": 0,
        "token": "whts",
        "type": "UnknownToken",
        "suggestions": [{
            "suggestion": "what's",
            "score": 0.909352914464075
        }, {
            "suggestion": "whats",
            "score": 0.810588859407343
        }, {
            "suggestion": "what is",
            "score": 0.680176771139565
        }]
    }, {
        "offset": 5,
        "token": "yur",
        "type": "UnknownToken",
        "suggestions": [{
            "suggestion": "your",
            "score": 0.909352914464075
        }]
    }]
}