MYSQL search for right words | fixing spelling errors

瘦欲@ 提交于 2019-12-17 16:31:35

问题


I have a table dictionary which contains a list of words Like:

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

so i want if somebody writes a text like

"helo iam looking for simlar engines for gogle".

Now I want to check every word if it exists in the database, if not it should get me the similar word for the word. For example: helo = hello, simlar = similar, gogle = google. Well, i want to fix the spelling errors. In my database i have a full dictionary of all english words. I coudn't find any mysql function which helps me. LIKE isn't helpfull in my situation.


回答1:


There is a function that does roughly want you want, but it's intensive and will slow queries down. You might be able to use in your circumstances, I have used it before. It's called Levenshtein. You can get it here How to add levenshtein function in mysql?




回答2:


you can use soundex() function for comparing phonetically

your query should be something like:

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

and this will return you the hello row




回答3:


What you want to do is called a fuzzy search. You could use the SOUNDEX function in MySQL, documented here:

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

You query would look like:

SELECT * FROM dictionary where SOUNDEX(word) = SOUNDEX(:yourSearchTerm)

... where your search term is bound to the :yourSearchTerm parameter value.

A next step would be to try implementing and making use of a Levenshtein function in MySQL. One is described here:

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.

You might also consider looking into databases that are aimed at full text searching, such as Elastic Search, which provides this natively:

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



来源:https://stackoverflow.com/questions/35212058/mysql-search-for-right-words-fixing-spelling-errors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!