Google 自定义搜索自动拼写检查
我们在对 Google 自定义搜索的 XML 结果中的查询进行自动拼写检查时遇到问题。
拼写错误的查询会返回拼写正确的结果,例如 socer
变为 soccer
并返回正确的结果。在 Google.com 上,可以选择使用查询字符串中的 nfpr=1
来搜索原始查询的结果。然而,这在 Google 自定义搜索中不起作用,而且我无法找到任何其他方法来搜索错误的拼写。
We're having a problem with the automatic spell checking on queries in the XML results of the Google Custom Search.
Queries which are spelled incorrectly return results with the correct spelling e.g. socer
becomes soccer
and returns the correct results. On Google.com there is the option to then search for results on the original query using nfpr=1
in the query string. However this doesn't work in the Google Custom search, and I've been unable to find any other way to search for the incorrect spelling.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于标准 google 搜索,可以通过将参数 &nfpr=1 添加到查询 url 来避免此行为。
不幸的是,对于基于 AJAX API 的自定义搜索,这是不可能的。我发现的唯一方法是使用 javascript 来解析用户的查询,然后使用正则表达式在每个尚未引用的单词周围加上引号。例如,如果收到的关键字是
“bmw z4”manual,
您可以将其更改为
“bmw z4”“manual”
,它具有相同的效果,只是它会禁用自动更正。不幸的是,如果您想处理高级逻辑语法的所有特殊情况(AND、OR、|、- 等),您的正则表达式会变得有点复杂。
我自己,我只是解析 Google 的响应,看看是否发生了这种情况,如果是,则通知用户如何防止它(通过在有问题的单词周围加上引号)。
For a standard google search this behavior can be avoided by adding the argument &nfpr=1 to the query url.
For a custom search based on the AJAX API, this unfortunately isn't possible. The only way I've found is to use javascript to parse the user's query, then use a regular expression to put quotes around each single word that is not yet quoted. So for example, if the keywords received are
"bmw z4" manual
you would change that to
"bmw z4" "manual"
which has the same effect, except that it disables the auto-correction. Unfortunately if you want to deal with all the special cases of advanced logical syntax (AND, OR, |, -, etc.), your regexp gets a bit complex.
Myself, I just parse the response from Google to see if this is happening, and if so notify the user how to prevent it (by putting quotes around the offending word(s)).