检测拼写错误的单词
我有一份机场名称列表,我的用户可以输入一个机场名称来选择它进行进一步处理。
您将如何处理拼写错误的名字并提出建议列表?
I have a list of airport names and my users have the possibility to enter one airport name to select it for futher processing.
How would you handle misspelled names and present a list of suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
查找Levenshtein 距离,以根据给定的用户输入匹配正确的名称。
Look up Levenshtein distances to match a correct name against a given user input.
http://norvig.com/spell- Correct.html
做了像levenshtein这样的事情,但是,因为他没有一路走下去,所以效率更高
http://norvig.com/spell-correct.html
does something like levenshtein but, because he doesnt go all the way, its more efficient
在代码中使用拼写检查。 单词列表应仅包含机场的正确拼写。
这不是一个好方法。 您应该选择提供自动完成选项的控件,或者像其他人建议的那样使用下拉菜单。
如果您的技术支持,请使用 AJAX。
Employ spell check in your code. The list of words should contain only correct spellings of airports.
This is not a great way to do this. You should either go for a control that provides auto complete option or a drop down as someone else suggested.
Use AJAX if your technology supports.
我知道这不是您所要求的,但如果这是一个应用程序,其中获得正确的机场很重要(例如预订机票),那么您可能需要一个确认阶段以确保您拥有正确的机场。 例如,曾有人拿到错误的悉尼门票。
I know its not what you asked, but if this is an application where getting the right airport is important (e.g. booking tickets) then you might want to have a confirmation stage to make sure you have the right one. There have been cases of people getting tickets for the wrong Sydney, for instance.
让用户从机场名称列表中进行选择可能比让他们自己输入机场名称更好。 这样就不会犯任何错误。
It may be better to let the user select from the list of airport names instead of letting them type in their own. No mistakes can be made that way.
虽然它不会立即起作用,但您可以跟踪拼写错误,并在输入正确的名称时查看他们最终输入的名称。 这样您就可以跟踪最常见的拼写错误,并提供最佳选择。
While it won't help right away, you could keep track of typos, and see which name they finally enter when a correct name is entered. That way you can track most common typos, and offer the best options.
添加到 Kevin 的建议中,如果您使用具有 javascript 自动完成功能的输入框,这可能是两全其美的。 例如 jquery autocomplete
编辑:丹麦人打败了我:(
Adding to Kevin's suggestion, it might be a best of both worlds if you use an input box with javascript autocomplete. such as jquery autocomplete
edit: danish beat me :(
您可能可以使用现有的拼写检查库。 做好这类事情的代码并不简单。 如果您确实想自己编写此内容,则可能需要查看字典树。
一种可能有效的方法是生成一个可能的错误单词及其更正的巨大列表(这里有一个 Python 中的实现),您可以缓存它以获得更高的性能。
There may be an existing spell-check library you can use. The code to do this sort of thing well is non-trivial. If you do want to write this yourself, you might want to look at dictionary trie's.
One method that may work is to just generate a huge list of possible error words and their corrections (here's an implementation in Python), which you could cache for greater performance.