MYSQL 搜索合适的词 |修复拼写错误

MYSQL search for right words | fixing spelling errors

我有一个 table dictionary,其中包含一个单词列表,例如:

   ID|word
   ---------
    1|hello
    2|google
    3|similar
    ...

所以我想如果有人写这样的文字

"helo iam looking for simlar engines for gogle".

现在我想检查每个单词是否存在于数据库中,如果不存在则应该 给我这个词的相似词。例如:helo = hello, simlar = similar, gogle = google。 好吧,我想修正拼写错误。在我的数据库中,我有一本包含所有英语单词的完整词典。我找不到任何对我有帮助的 mysql 函数。 LIKE 对我的情况没有帮助。

有一个函数可以大致满足您的需求,但它很密集并且会减慢查询速度。你可能可以在你的情况下使用,我以前使用过它。它叫做 Levenshtein。你可以在这里得到它 How to add levenshtein function in mysql?

您可以使用soundex()功能进行语音比较

您的查询应该是这样的:

select * from table where soundex(word) like soundex('helo');

这将 return 你排在 hello

你想做的事情叫做模糊搜索。您可以使用 MySQL 中的 SOUNDEX 函数,记录在此处:

http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_soundex

您的查询如下所示:

SELECT * FROM dictionary 其中 SOUNDEX(word) = SOUNDEX(:yourSearchTerm)

...您的搜索词绑定到 :yourSearchTerm 参数值的位置。

下一步将尝试在 MySQL 中实施和使用 Levenshtein 函数。此处描述了一种:

http://www.artfulsoftware.com/infotree/qrytip.php?id=552

The Levenshtein distance between two strings is the minimum number of operations needed to transform one string into the other, where an operation may be insertion, deletion or substitution of one character.

您还可以考虑研究旨在进行全文搜索的数据库,例如原生提供此功能的 Elastic Search:

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html