-2

I am writing a small program where I want to extract user input strings from MySQL database. But I am not looking for exact matching strings. For example

If user types amazon then it should extract amazon from the database. But if user types amazonn or amazo or amozon it should still retrive amazon because it is the nearest word to the misspelled word.

This is something similar to google suggestions while searching. Is there anyway to define a function like this in PHP? Do i have to import dictionary and compare with it?

Protomen
  • 9,471
  • 9
  • 57
  • 124
hnvasa
  • 830
  • 4
  • 13
  • 26
  • So "retrive" should return "retrieve"? – danronmoon Jan 05 '15 at 17:32
  • There's no easy way to do it properly, I suggest looking into things like http://sphinxsearch.com/about/sphinx/ – MSTannu Jan 05 '15 at 17:33
  • I am a newbie and stumbled upon `%$search%` the following command...but this also does not solve my problem of misspelled words...its just corrects incomplete words... – hnvasa Jan 05 '15 at 17:34
  • 1
    http://php.net/manual/en/function.levenshtein.php – danronmoon Jan 05 '15 at 17:35
  • Looks like you'll have to have a dictionary for create "queries". – Protomen Jan 05 '15 at 17:35
  • so for example I should have an english dictionary and i compare words with it? how does this work in real? – hnvasa Jan 05 '15 at 17:37
  • @hnvasa It would not be a specific English dictionary, but a spellcheck (using a dictionary) that look similar words your writing and mount the querie unfortunately have no ready example. This would be a bit complex to assemble now. If I understand your question. – Protomen Jan 05 '15 at 17:45
  • Take a look to [MySQL Full-Text search functions](http://dev.mysql.com/doc/refman/5.5/en/fulltext-search.html), they may be what you're looking for. *Be sure to read the full chapter* – Barranka Jan 05 '15 at 17:50
  • This is really discouraging for beginners asking questions on the forum when your genuine questions are down-voted :( – hnvasa Jan 05 '15 at 17:53
  • @hnvasa Your question is good, but it seems you could have done a little more research. Please take a look to MySQL full-text search functions (link provided in my previous comment), and try them... if your problem is solved, answer your own question. If it's only "partially" solved, edit your question and include your attempted solutions and a brief description on why they didn't work. You'll get much better answers if you add more research to your question – Barranka Jan 05 '15 at 17:57
  • 1
    The question does make sense, but a downvote could be because it is too broad or because having a function available in PHP (in this case, a Levenshtein function) isn't the best way of approaching the problem. Yes, Levenshtein will work but only if you can run a query to grab all of the near matches first. That is why a full-text search with synonyms (i.e., misspellings) might be a better approach. – David Faber Jan 05 '15 at 18:00
  • @DavidFaber I think it is "too large", what he wants is simple, he wants the words sought to be corrected before making a query, it does not need to supply a function, since we can consider that he did not know where to start. Correct me if I'm wrong :) – Protomen Jan 05 '15 at 18:58

1 Answers1

2

You could use strpos() in two ways
How do I check if a string contains a specific word in PHP? Search for the DB string in the string that the user inputted.
And the other way around:
Search for the string that the user inputted in the DB string.

The problem with this is that you only cover differences at the outside of the string such as amazo or amazonn but not amozon

Or you could use levenshtein() and similar_text() as stated in the comments.
Good example code with suggestions:
https://codereview.stackexchange.com/questions/27173/php-spell-checker-with-suggestions-for-misspelled-words

That should be covered in an entirely different way...
I think you want both checks but everything is going to be difficult without a dicitonary so I suggest importing one ;)

Community
  • 1
  • 1
Mramaa
  • 400
  • 2
  • 13
  • Example in http://codereview.stackexchange.com/questions/27173/php-spell-checker-with-suggestions-for-misspelled-words is good, but performance is bad. But also yes it is a good answer +1 – Protomen Jan 05 '15 at 17:48
  • I know that performance is an issue but I wanted to keep things simple. Complex code as an example seemed a bit odd ;) – Mramaa Jan 05 '15 at 17:49
  • thanks for the different techniques mentioned..in `levenshtein()` it compares the misspelled string with the correct string, but where do i get the correct string in first place? The query will be user input...! – hnvasa Jan 05 '15 at 17:51
  • You will need to use a dictionary... I mentioned that. – Mramaa Jan 05 '15 at 17:52
  • 1
    okay...i will be trying that soon and reverting back with results!! – hnvasa Jan 05 '15 at 17:55
  • @hnvasa Made some progress yet? – Mramaa Jan 07 '15 at 18:36