Forum Moderators: coopster

Message Too Old, No Replies

search function with hint

search function with hint

         

xbl01234

5:30 am on Feb 10, 2008 (gmt 0)

10+ Year Member



Hi;
I'd like to ask you that how i can create a search function with hint when the user misspelling some word.

For example:
i type a string like the "ful text serch mysql" in google, and it will ask me
"Did you mean: full text search mysql?"

what king of knowledge can help me to do that? Thanks

darrenG

12:27 pm on Feb 10, 2008 (gmt 0)

10+ Year Member



Well, Im no king of knowledge. But to start you off, you will need a dictionary - a text file full of words for example. Straight away google has an advantage over you and I, with a dirty great index of just about every word ever published on a website.

You then split the search term into individual words, I would propose the explode function, using ' ' as the delimeter.

Next use each keyword to construct a simple regular expression that will return either an exact match, or a very close match.

If all keywords have an exact match, do not suggest. If one or more does not have an exact match, suggest the very near match.

So in pseudo-pseudocode we:

Get searchTerm
array keywords = explode(searchTerm, ' ')
array patterns = makeSimplePatterns(keywords)
array matches = findSuitableMatches(patterns, dictionary)

For each matches

If match == keyword

suggest = false

else

suggest = true
break out of for loop

end if

end for

newSearchTerm = implode(' ', matches)
suggestion = 'Did you mean: ' + newSearchTerm

search()

That isn't perfect, but might give you something to think about...

xbl01234

6:51 am on Feb 11, 2008 (gmt 0)

10+ Year Member



Thanks for your reply.

phranque

10:10 am on Feb 11, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you might find the php pspell function man page [php.net] interesting.

lammert

5:20 pm on Feb 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The levenshtein algoritm is commonly used to see how close two strings are based on the minumum number of replace, insert and delete operations necessary to convert one string in the other.

The function is a standard part of PHP [php.net].

If you have a dictionary of words, you can compare your word with the words in the list. The one which has the lowest levensthein value can be your search hint. Depending of the size of the dictionary and the speed of your server this may take noticable time though.