NameSilo

Spell checker from sql database

Spaceship Spaceship
Watch

tm

Established Member
Impact
25
I have an sql database of words. How do i query this database to find words that are similar to "$q" ? The LIKE function will not work (well at least not the way i tried) - i need it to correct something like 'helo' to 'hello' or 'hllo' to 'hello'.

Rep+ to anybody that can help :)

thanks,

tm
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable DomainsUnstoppable Domains
What kind of like are you using ?
Are you using in wildcard form like this:
PHP:
 like '%search_string%'

Are you looking for a litteral $ ? You may have to escape this.
 
0
•••
sdsinc said:
What kind of like are you using ?
Are you using in wildcard form like this:
PHP:
 like '%search_string%'

Are you looking for a litteral $ ? You may have to escape this.

The user submits a form. the variable of the form is $q. I tried a wilcard like this but i may have got it wrong:
PHP:
LIKE '$q%'
 
0
•••
OK, just to be sure...
If the field in your form is called q PHP would automatically instanciate a variable called $q but this will only work if register_globals is on.
It is best to make code portable and not rely on those PHP settings which may vary from one server to another.
You should assign the variable manually to be sure (if you are not doing so already):

PHP:
$q=$_POST['q'];

If using
PHP:
LIKE '$q'
mySQL will look for the exact word. You said you are looking for similar words.

If you are looking for similar words (and not the exact string) you should use wildcard pattern matching:
Try something like:
PHP:
$query="select blah where field LIKE '%" . $q . "%'";

Since you are using % at the right only the field should *begin* with whatever $q is.
 
0
•••
sdsinc said:
OK, just to be sure...
If the field in your form is called q PHP would automatically instanciate a variable called $q but this will only work if register_globals is on.
It is best to make code portable and not rely on those PHP settings which may vary from one server to another.
You should assign the variable manually to be sure (if you are not doing so already):

PHP:
$q=$_POST['q'];

If using
PHP:
LIKE '$q'
mySQL will look for the exact word. You said you are looking for similar words.

If you are looking for similar words (and not the exact string) you should use wildcard pattern matching:
Try something like:
PHP:
$query="select blah where field LIKE '%" . $q . "%'";

Since you are using % at the right only the field should *begin* with whatever $q is.

yes but say $q was 'helo' that wouldnt find 'hello' in the database, would it?
 
0
•••
No it won't work.
If $q was 'hell' then yes it would find 'hello' in the DB.

On a side note... if you are developing a spell checker you should have a look at full-text indexes (http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html). Using the LIKE function involves a full table scan. You can optimize a lot with full-text but there are drawbacks too (for instance it's not efficient with words less than 3 chars).
 
0
•••
Dynadot — .com Registration $8.99Dynadot — .com Registration $8.99
Appraise.net

We're social

Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back