如果名字拼写错误,请在列表中查找该名字
我有一个姓名列表,一些代码会检查该人是否存在,如果存在,则执行一些操作。
我的问题是我想处理名称输入错误的情况。即我有一个姓名列表
Bob
Frank
Tom
Tim
John
如果我输入 Joohn,我希望它询问我是否指的是 John。如果我输入 Tm,我会被问到是否是 Tim,如果我说不是,它会询问我是否是 Tom...等等。
以前有人做过类似的事情吗?
I've got a list of names which some code checks against to see if the person exists, and if so do some stuff..
My issue is that I want to handle the case of the name being entered incorrectly.. I.e. I have a list of names
Bob
Frank
Tom
Tim
John
If I type in Joohn, I want it to ask me if I meant John. If I type Tm, I get asked if I meant Tim, if I say no, it asks if i meant Tom.. Etc..
Has anyone done something like this before?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一件事是查看列表,看看是否有完全匹配的内容。如果不是,则计算用户输入的单词与列表中每个元素之间的 Levenshtein 距离 。您建议选择最低的那个。
The first thing would be to look in the list to see if you have an exact match. If not you calculate the Levenshtein distance between the word entered by the user and every element in the list. You suggest the one that has the lowest.
如果您不想局限于人员列表并希望搜索常用名称,您可以使用 Google API 服务提交拼写检查请求并收到针对查询的建议拼写更正作为回报:
http://www.sitepoint.com/blogs/2004/03/10 /用谷歌检查你的拼写/
If you dont want to be limited to a list of people and want to search for common names you can use Google API service to submit spell check requests and receive in return a suggested spell correction for the query:
http://www.sitepoint.com/blogs/2004/03/10/check-your-spelling-with-google/
如果名称未出现在列表中,您可以使用 Levenshtein 距离 和 Soundex 确定列表中最接近的条目并提出建议
If the name doesn't appear in the list, you can use algorithms like Levenshtein distance and Soundex to determine which entries in the list are closest and suggest those