2017-06-21 3 views
0

J'essaye d'implémenter le Bing Spell Check API v7. Ceci est ma fonction actuelle:L'implémentation de l'API Bing Spell Check v7 renvoie 0 flaggedTokens

# 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; 
} 

Le problème est que cet exemple:

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

Retours:

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

Ce qui signifie qu'il n'a pas trouvé d'erreurs. J'ai couru la même épreuve dans Bing's test tool, et j'ai reçu cette structure à la place:

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

Am quelque chose que je manque. Des idées? Merci!

Répondre

0

C'est assez simple, vous venez de mélanger vos paramètres de fonction. Vous avez

function bing_spell_check($q, $lang) {} 

et

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

C'est là que le problème commence, $q est maintenant en et $lang est maintenant whts yur name?. Ainsi, lorsque vous créez votre tableau $param, la langue et le texte sont mélangés (ce qui provoque l'échec de l'API de vérification orthographique). Vous pouvez simplement corriger cela en changeant le paramètre ordre:

function bing_spell_check($lang, $q) {} 

maintenant votre code

<?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?')); 
?> 

résultats dans

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

peut pas croire que je ne l'ai pas remarqué une telle erreur stupide. C'est probablement le résultat de la programmation à 3h hehe. Réponse acceptée instantanément. Merci! – andufo

+0

@andufo Pas de problème! Je suis content d'avoir pu vous aider. –