Bing 拼写检查 API v7 的重音和编码问题
Issue with accents and encoding with Bing Spell Check API v7
所以我尝试在 PHP 中使用 Bing 的拼写检查 API,但我遇到了重音符号和其他特殊字符未被解码的问题正确地,创建了许多原始文本中没有的错误并弄乱了偏移量。
我的实现非常简单 - 它主要基于他们在文档中提供的示例。我不确定我是否应该做一些不同的事情,或者他们如何解码这些特殊字符是否存在问题(这似乎不太可能 - 我搞砸的可能性更大......!)
代码如下:
$host = 'https://api.cognitive.microsoft.com';
$path = '/bing/v7.0/spellcheck?';
$data = array (
'mkt' => $lang,
'mode' => 'proof',
'text' => urlencode($text)
);
$encodedData = http_build_query($data);
$key = 'subscription key redacted for obvious reasons';
$headers = "Content-type: application/x-www-form-urlencoded\r\n" .
"Ocp-Apim-Subscription-Key: $key\r\n";
if (isset($_SERVER['REMOTE_ADDR']))
$headers .= "X-MSEdge-ClientIP: " . $_SERVER['REMOTE_ADDR'] . "\r\n";
$options = array (
'http' => array (
'header' => $headers,
'method' => 'POST',
'content' => $encodedData
)
);
$context = stream_context_create ($options);
$result = file_get_contents ($host . $path, false, $context);
if ($result === FALSE) {
# Handle error
}
$decodedResult = json_decode($result, true);
例如,如果我尝试拼写检查以下字符串:
d'institution
$encodedData 变为以下内容:
mkt=fr-CA&method=proof&text=d%25E2%2580%2599institutions
我从 API 得到的结果如下:
array(2) {
["_type"]=>
string(10) "SpellCheck"
["flaggedTokens"]=>
array(1) {
[0]=>
array(4) {
["offset"]=>
int(8)
["token"]=>
string(14) "99institutions"
["type"]=>
string(12) "UnknownToken"
["suggestions"]=>
array(2) {
[0]=>
array(2) {
["suggestion"]=>
string(15) "99 institutions"
["score"]=>
float(0.93191315174102)
}
[1]=>
array(2) {
["suggestion"]=>
string(14) "99 institution"
["score"]=>
float(0.6518044080768)
}
}
}
}
}
如您所见,解码似乎有问题,因为 % 被编码了两次,显然只解码了一次。现在,如果我在 $data 中设置 'text' 的值时删除 url_encode(),它对撇号可以正常工作,但对重音符号不起作用。例如,以下字符串:
Responsabilité
被 API 解释为
Responsabilité
其中 returns 一个错误。
这很可能是我忽略的简单问题,但我已经为此苦苦挣扎了很长一段时间,希望能得到任何帮助。
谢谢,
-埃米尔
[ 编辑 ] 好吧,一如既往......当有疑问时,假设你错了。 API 建议更改常规字母的所有重音,因为即使指定的语言是法语,它仍然会以英语给出建议,而不是返回一个空数组。至于那些似乎没有被解码的口音,好吧......我是 var_dump-ing 没有任何文档类型集的数据,所以当然它会在没有正确编码的情况下显示。抱歉 - 最后,只需删除 urlencode() 就可以了!
根据 docs:
The API supports two proofing modes, Proof and Spell. The default mode is Proof. The Proof spelling mode provides the most comprehensive checks, but it's available only in the en-US (English-United States) market. For all other markets, set the mode query parameter to Spell. The Spell mode finds most spelling mistakes but doesn't find some of the grammar errors that Proof catches (for example, capitalization and repeated words).
所以我尝试在 PHP 中使用 Bing 的拼写检查 API,但我遇到了重音符号和其他特殊字符未被解码的问题正确地,创建了许多原始文本中没有的错误并弄乱了偏移量。
我的实现非常简单 - 它主要基于他们在文档中提供的示例。我不确定我是否应该做一些不同的事情,或者他们如何解码这些特殊字符是否存在问题(这似乎不太可能 - 我搞砸的可能性更大......!)
代码如下:
$host = 'https://api.cognitive.microsoft.com';
$path = '/bing/v7.0/spellcheck?';
$data = array (
'mkt' => $lang,
'mode' => 'proof',
'text' => urlencode($text)
);
$encodedData = http_build_query($data);
$key = 'subscription key redacted for obvious reasons';
$headers = "Content-type: application/x-www-form-urlencoded\r\n" .
"Ocp-Apim-Subscription-Key: $key\r\n";
if (isset($_SERVER['REMOTE_ADDR']))
$headers .= "X-MSEdge-ClientIP: " . $_SERVER['REMOTE_ADDR'] . "\r\n";
$options = array (
'http' => array (
'header' => $headers,
'method' => 'POST',
'content' => $encodedData
)
);
$context = stream_context_create ($options);
$result = file_get_contents ($host . $path, false, $context);
if ($result === FALSE) {
# Handle error
}
$decodedResult = json_decode($result, true);
例如,如果我尝试拼写检查以下字符串:
d'institution
$encodedData 变为以下内容:
mkt=fr-CA&method=proof&text=d%25E2%2580%2599institutions
我从 API 得到的结果如下:
array(2) {
["_type"]=>
string(10) "SpellCheck"
["flaggedTokens"]=>
array(1) {
[0]=>
array(4) {
["offset"]=>
int(8)
["token"]=>
string(14) "99institutions"
["type"]=>
string(12) "UnknownToken"
["suggestions"]=>
array(2) {
[0]=>
array(2) {
["suggestion"]=>
string(15) "99 institutions"
["score"]=>
float(0.93191315174102)
}
[1]=>
array(2) {
["suggestion"]=>
string(14) "99 institution"
["score"]=>
float(0.6518044080768)
}
}
}
}
}
如您所见,解码似乎有问题,因为 % 被编码了两次,显然只解码了一次。现在,如果我在 $data 中设置 'text' 的值时删除 url_encode(),它对撇号可以正常工作,但对重音符号不起作用。例如,以下字符串:
Responsabilité
被 API 解释为
Responsabilité
其中 returns 一个错误。
这很可能是我忽略的简单问题,但我已经为此苦苦挣扎了很长一段时间,希望能得到任何帮助。
谢谢,
-埃米尔
[ 编辑 ] 好吧,一如既往......当有疑问时,假设你错了。 API 建议更改常规字母的所有重音,因为即使指定的语言是法语,它仍然会以英语给出建议,而不是返回一个空数组。至于那些似乎没有被解码的口音,好吧......我是 var_dump-ing 没有任何文档类型集的数据,所以当然它会在没有正确编码的情况下显示。抱歉 - 最后,只需删除 urlencode() 就可以了!
根据 docs:
The API supports two proofing modes, Proof and Spell. The default mode is Proof. The Proof spelling mode provides the most comprehensive checks, but it's available only in the en-US (English-United States) market. For all other markets, set the mode query parameter to Spell. The Spell mode finds most spelling mistakes but doesn't find some of the grammar errors that Proof catches (for example, capitalization and repeated words).