Bing 拼写检查的实现 API v7 returns 0 flaggedTokens

Implementation of Bing Spell Check API v7 returns 0 flaggedTokens

我正在尝试实施 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?'));

Returns:

{  
   "@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
        }
      ]
    }
  ]
}

我是不是漏掉了什么。有任何想法吗?谢谢!

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

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
        }]
    }]
}