【问题标题】:Implementation of Bing Spell Check API v7 returns 0 flaggedTokensBing Spell Check API v7 的实现返回 0 flaggedTokens
【发布时间】:2017-06-21 20:41:28
【问题描述】:

我正在尝试实现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
        }
      ]
    }
  ]
}

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 不敢相信我没有注意到这样一个愚蠢的错误。这可能是凌晨 3 点编程的结果呵呵。答案立即接受。谢谢!
    • @andufo 没问题!很高兴能帮到你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    相关资源
    最近更新 更多