如何在PHP中找到拼写错误的单词?

时间:2021-10-01 19:15:00

I'll explain my problem:

我会解释我的问题:

I have a database table called country. It has two columns: ID and name.

我有一个名为country的数据库表。它有两列:ID和名称。

When I want to search for 'paris', but misspelled the word: 'pares' ('e' instead of 'i'), I won't get any result from DB.

当我想搜索'paris'时,却拼错了单词:'pares'('e'而不是'i'),我不会从DB获得任何结果。

I want the the system to suggest similar words that could help in the search.

我希望系统能够建议可以帮助搜索的类似单词。

So, I am looking for help writing a script that makes suggestions from the DB that contain similar words like: paris, paredes, ... etc.

所以,我正在寻找帮助,编写一个脚本,从DB中提出包含类似词语的建议:paris,paredes,...等。

5 个解决方案

#1


20  

In PHP you should use metaphone it is more accurate than soundex.

在PHP中你应该使用metaphone它比soundex更准确。

But your problem is getting the data from the database. You've not mentioned the DB. In MySQL you can make use of the SOUNDEX function. You just need to change your where clause in the query from

但是你的问题是从数据库中获取数据。你没有提到过DB。在MySQL中,您可以使用SOUNDEX功能。您只需要在查询中更改where子句

...where city = '$input_city'

to

... where soundex(city) = soundex('$input_city')

or even better you can use SOUNDS LIKE operator as

甚至可以更好地使用SOUNDS LIKE运算符作为

... where city sounds like '$input_city'

#2


9  

soundex will return a numerical code for a word that represents its sound. Words that sound similar will have the same soundex code. You could have a table with words and their soundex codes that you could use to look up similar sounding words. You could then sort them using their levenshtein distance.

soundex将返回表示其声音的单词的数字代码。听起来相似的单词将具有相同的soundex代码。您可以拥有一个包含单词及其soundex代码的表格,您可以使用它来查找类似的发音单词。然后你可以使用他们的levenshtein距离对它们进行排序。

If you're looking for something simpler and you just want to handle typos in your DB queries, you can do

如果您正在寻找更简单的东西而且您只想在数据库查询中处理拼写错误,那么您可以这样做

select * from country where city SOUNDS LIKE 'Paris' instead of select * from country where city='Paris'

从城市SOUNDS LIKE'巴黎'中选择*而不是从城市='巴黎'的国家/地区选择*

#3


4  

Basically you need to check similarity against a valid array of names when you got no results from your db.

基本上,当您的数据库没有结果时,您需要检查有效数组名称的相似性。

My idea:

  • User searching some name
  • 用户搜索某个名字

  • No exact results
  • 没有确切的结果

  • Fetch all names from db
  • 从db获取所有名称

  • Using levenshtein calculate the most exact tip for user to return
  • 使用levenshtein计算用户返回的最准确的提示

#4


2  

If you're using MySQL, you'll want to use a MATCH() AGAINST() statement, where MATCH() is given a comma-delimited list of FULLTEXT columns and AGAINST() is given your string to match against. The statement returns the relevance of your match (between 0 and 1) which you can use to determine whether or not to return rows.

如果您正在使用MySQL,则需要使用MATCH()AGAINST()语句,其中MATCH()被赋予逗号分隔的FULLTEXT列列表,并且AGAINST()被赋予您要匹配的字符串。该语句返回匹配的相关性(介于0和1之间),您可以使用它来确定是否返回行。

More info on the MySQL site.

有关MySQL站点的更多信息。

Edit: the sound suggestions are good ideas, however certain misspellings will completely change the pronunciation of a word and thus you may not be able to provide good suggestions if you use that method.

编辑:声音建议是好主意,但是某些拼写错误会完全改变单词的发音,因此如果您使用该方法,您可能无法提供好的建议。

#5


2  

Since most of the PHP internal methods are already covered, you can also take a look at the Yahoo Boss Spelling Suggestion Service, its quite usefull -> http://developer.yahoo.com/search/boss/boss_guide/Spelling_Suggest.html

由于大多数PHP内部方法已经涵盖,您还可以查看Yahoo Boss Spelling Suggestion Service,它非常有用 - > http://developer.yahoo.com/search/boss/boss_guide/Spelling_Suggest.html

#1


20  

In PHP you should use metaphone it is more accurate than soundex.

在PHP中你应该使用metaphone它比soundex更准确。

But your problem is getting the data from the database. You've not mentioned the DB. In MySQL you can make use of the SOUNDEX function. You just need to change your where clause in the query from

但是你的问题是从数据库中获取数据。你没有提到过DB。在MySQL中,您可以使用SOUNDEX功能。您只需要在查询中更改where子句

...where city = '$input_city'

to

... where soundex(city) = soundex('$input_city')

or even better you can use SOUNDS LIKE operator as

甚至可以更好地使用SOUNDS LIKE运算符作为

... where city sounds like '$input_city'

#2


9  

soundex will return a numerical code for a word that represents its sound. Words that sound similar will have the same soundex code. You could have a table with words and their soundex codes that you could use to look up similar sounding words. You could then sort them using their levenshtein distance.

soundex将返回表示其声音的单词的数字代码。听起来相似的单词将具有相同的soundex代码。您可以拥有一个包含单词及其soundex代码的表格,您可以使用它来查找类似的发音单词。然后你可以使用他们的levenshtein距离对它们进行排序。

If you're looking for something simpler and you just want to handle typos in your DB queries, you can do

如果您正在寻找更简单的东西而且您只想在数据库查询中处理拼写错误,那么您可以这样做

select * from country where city SOUNDS LIKE 'Paris' instead of select * from country where city='Paris'

从城市SOUNDS LIKE'巴黎'中选择*而不是从城市='巴黎'的国家/地区选择*

#3


4  

Basically you need to check similarity against a valid array of names when you got no results from your db.

基本上,当您的数据库没有结果时,您需要检查有效数组名称的相似性。

My idea:

  • User searching some name
  • 用户搜索某个名字

  • No exact results
  • 没有确切的结果

  • Fetch all names from db
  • 从db获取所有名称

  • Using levenshtein calculate the most exact tip for user to return
  • 使用levenshtein计算用户返回的最准确的提示

#4


2  

If you're using MySQL, you'll want to use a MATCH() AGAINST() statement, where MATCH() is given a comma-delimited list of FULLTEXT columns and AGAINST() is given your string to match against. The statement returns the relevance of your match (between 0 and 1) which you can use to determine whether or not to return rows.

如果您正在使用MySQL,则需要使用MATCH()AGAINST()语句,其中MATCH()被赋予逗号分隔的FULLTEXT列列表,并且AGAINST()被赋予您要匹配的字符串。该语句返回匹配的相关性(介于0和1之间),您可以使用它来确定是否返回行。

More info on the MySQL site.

有关MySQL站点的更多信息。

Edit: the sound suggestions are good ideas, however certain misspellings will completely change the pronunciation of a word and thus you may not be able to provide good suggestions if you use that method.

编辑:声音建议是好主意,但是某些拼写错误会完全改变单词的发音,因此如果您使用该方法,您可能无法提供好的建议。

#5


2  

Since most of the PHP internal methods are already covered, you can also take a look at the Yahoo Boss Spelling Suggestion Service, its quite usefull -> http://developer.yahoo.com/search/boss/boss_guide/Spelling_Suggest.html

由于大多数PHP内部方法已经涵盖,您还可以查看Yahoo Boss Spelling Suggestion Service,它非常有用 - > http://developer.yahoo.com/search/boss/boss_guide/Spelling_Suggest.html