If anyone is interested, I was busy converting the Scratch script above to PHP today, when I realised that PHP actually has a function called levenshtein($str1, $str2) that compares the similarity of two strings for you! With the following function I was able to guess what a user meant by their search and even add sensitivity! ($words has to be an array, by the way, the array can store a list of allowed words.)
function wordMatch($words, $input, $sensitivity){
$shortest = -1;
foreach ($words as $word) {
$lev = levenshtein($input, $word);
if ($lev == 0) {
$closest = $word;
$shortest = 0;
break;
}
if ($lev <= $shortest || $shortest < 0) {
$closest = $word;
$shortest = $lev;
}
}
if($shortest <= $sensitivity){
return $closest;
} else {
return 0;
}
}Last edited by sparks (2012-03-07 07:01:21)
Offline
And you sent me to Wikipedia again.
That's pretty cool: you're checking similarity based on number of edits needed to turn one string into another.
PS. $words looked like swords to me.
I think that'$ one reason I don't like PHP, having to add dollar $ign$ before each of the variable$. Don't you agree, $parks?
Offline
I like it because it's clear what a variable is over a php constant. For example, someone might name a variable var and then wonder why it wouldn't set or echo, it is a constant. Of course, constants should be CAPITALISED to follow common practice, but PHP constants seem to work in lowercase too. I always add a $ before my javascript variables and then have to take it away again
Offline
I use a smart (hey, it's from Apple) editor, Xcode, which highlights constants, so I have it easy.
Offline
Did you post the Scratch script? I'm working on a historical document search engine on Scratch and that would be helpful. Thanks!
Offline
Yes, the Scratch script is linked in the first post. You can see the script image here: 
(url to image of script)
Last edited by sparks (2012-03-07 09:31:11)
Offline
sparks wrote:
Yes, the Scratch script is linked in the first post. You can see the script image here: http://dl.dropbox.com/u/22935223/scriptWithCap.gif
(url to image of script)
Cool, thanks.
Offline
No problem
The new search system is now implemented on course my website
Offline
Hardmath123 wrote:
I use a smart (hey, it's from Apple) editor, Xcode, which highlights constants, so I have it easy.
![]()
Notepad++.
I like the $s too. And Sparks, that happens to me with JS after I've been doing a bunch of PHP, but I forget them when I haven't done PHP in a few hours.
Offline
Speaking of which, I see a lot of JS potential on your site. I see no spammy popups behind my window like I would on a real booking site!
Also, before we get Apple-bashing, I'd like to point out: Xcode can read, run, analyze with breakpoints, auto-complete, code-fold, smart syntax highlight (more languages than I thought existed, though I guess FORTRAN may have been overkill; JS nested in HTML is JS highlighted; imported file upvars and classes are highlighted) and partially debug my code. No, I wouldn't survive on a text editor, thank you very much.
Last edited by Hardmath123 (2012-03-07 10:24:12)
Offline

Yay!
I really do love the awesomeness of the site design, it's so pretty!
Last edited by rookwood101 (2012-03-07 10:38:00)
Offline
sparks wrote:
Glad you like, and I'm not apple bashing
![]()
I don't think I'll get many marks from my University lecturer if I spam his computer when he tries to mark the finished site![]()
Just kidding!
I meant Sci about the Apple bashing.
Offline
Huh? I wasn't. Just saying what I use...
Site looking nice.
Offline
You have lowecase suggested results--I think you want "Manchester", not "manchester".

Offline
That is true. I hadn't bothered to PHP it since for simplicity's sake, names are stored all lowercase. I guess to caputalise the first letter I would have to
$result = strtoupper(substr($result, 0, 1)) . substr($result, 1, strlen($result) - 1);
That's off the top of my head. For all I know there's a function for it
Does php cap lengths for substr? could I just do substr($result, 1, 100)? All this would mean me looking it up
Last edited by sparks (2012-03-07 18:10:44)
Offline
sparks wrote:
Search correction method 3
This will now cut off unruly searches but set the cap too high and sensible searches will still not get through! For example, "Brizzle", a slang word for Bristol would still return Bristol without the cap, but at only 57% match it can easily be filtered out.
My suggestion is to go with method 3, and set it at 85% or higher. But first, check for number or errors, not percentage. If you check for 4 errors, Brizzle could become Bristol. If you had, for example, Brozzle, then the code would go on to check the percentages. Also, professional airlines probably would not cater to slang. (right?)
Another thing you could do would be to check for simple keypress errors (simple to press, not to program
). For example, in place of a "B", check for V, G, H, N, and space, possibly including multiple errors. That might be too hard, especially since some keyboards have a different layout.
Anyway, good luck!
Last edited by LiquidMetal (2012-03-07 20:35:05)
Offline
LiquidMetal wrote:
sparks wrote:
Search correction method 3
This will now cut off unruly searches but set the cap too high and sensible searches will still not get through! For example, "Brizzle", a slang word for Bristol would still return Bristol without the cap, but at only 57% match it can easily be filtered out.My suggestion is to go with method 3, and set it at 85% or higher. But first, check for number or errors, not percentage. If you check for 4 errors, Brizzle could become Bristol. If you had, for example, Brozzle, then the code would go on to check the percentages. Also, professional airlines probably would not cater to slang. (right?)
Another thing you could do would be to check for simple keypress errors (simple to press, not to program). For example, in place of a "B", check for V, G, H, N, and space, possibly including multiple errors. That might be too hard, especially since some keyboards have a different layout.
Anyway, good luck!
Thanks for the suggestions, but I've already implemented a system from above, as I posted
I like the idea of a program that can use close-key sensing to determine actual suggestions, but I think it would take a LOT of work and I still have to HTML the rest of the site and build the flight-booking system by mid-march.
I also added the ucfirst() function to the search suggestions, thanks for the idea! I have now replaced that with a similar function I found called ucwords() which will work better if the airline should ever decided to fly to a place with a two-word name.
Would people who have been following this thread be interested in testing out the booking feature when it is done to suggest improvements?
Last edited by sparks (2012-03-08 06:19:44)
Offline
What if you did this?
#3's scripts
if that returns no results...
#1's scripts
if that returns no results...
if best one from #3 is > 50%
Then say "Did you mean (best one from #3)?"
otherwise...
Say "Did you mean (best from #1)?" // Use the one php thing you found to see what misspelling is closest
That would make it always show a result but always show the best one.
Last edited by PullJosh (2012-03-08 07:42:03)

Offline
sparks wrote:
LiquidMetal wrote:
sparks wrote:
Search correction method 3
This will now cut off unruly searches but set the cap too high and sensible searches will still not get through! For example, "Brizzle", a slang word for Bristol would still return Bristol without the cap, but at only 57% match it can easily be filtered out.My suggestion is to go with method 3, and set it at 85% or higher. But first, check for number or errors, not percentage. If you check for 4 errors, Brizzle could become Bristol. If you had, for example, Brozzle, then the code would go on to check the percentages. Also, professional airlines probably would not cater to slang. (right?)
Another thing you could do would be to check for simple keypress errors (simple to press, not to program). For example, in place of a "B", check for V, G, H, N, and space, possibly including multiple errors. That might be too hard, especially since some keyboards have a different layout.
Anyway, good luck!Thanks for the suggestions, but I've already implemented a system from above, as I posted
I like the idea of a program that can use close-key sensing to determine actual suggestions, but I think it would take a LOT of work and I still have to HTML the rest of the site and build the flight-booking system by mid-march.
I also added the ucfirst() function to the search suggestions, thanks for the idea! I have now replaced that with a similar function I found called ucwords() which will work better if the airline should ever decided to fly to a place with a two-word name.
Would people who have been following this thread be interested in testing out the booking feature when it is done to suggest improvements?
Yeah, sure!
Offline
Hardmath123 wrote:
sparks wrote:
LiquidMetal wrote:
My suggestion is to go with method 3, and set it at 85% or higher. But first, check for number or errors, not percentage. If you check for 4 errors, Brizzle could become Bristol. If you had, for example, Brozzle, then the code would go on to check the percentages. Also, professional airlines probably would not cater to slang. (right?)
Another thing you could do would be to check for simple keypress errors (simple to press, not to program). For example, in place of a "B", check for V, G, H, N, and space, possibly including multiple errors. That might be too hard, especially since some keyboards have a different layout.
Anyway, good luck!Thanks for the suggestions, but I've already implemented a system from above, as I posted
I like the idea of a program that can use close-key sensing to determine actual suggestions, but I think it would take a LOT of work and I still have to HTML the rest of the site and build the flight-booking system by mid-march.
I also added the ucfirst() function to the search suggestions, thanks for the idea! I have now replaced that with a similar function I found called ucwords() which will work better if the airline should ever decided to fly to a place with a two-word name.
Would people who have been following this thread be interested in testing out the booking feature when it is done to suggest improvements?Yeah, sure!
I also
Offline