Given an array of a series of long numbers (equal length) - 0101101111, 0111101111, 0101101101, etc.
Find closest match to 0101101001
OK, that's the short version. The long version is a form with 11 Yes(1) and No(0) questions (product finder). Since we know that the database may not contain an exact match to the n^r solutions = 2048, we need to find the closest match or, possibly, the closest two or three matches.
I'm guessing we need to compare each number in each position and rank the result, but I'm stuck on the solution - if I am even going about it in the right direction.
Thanks!!
I did a quick check against popnoodle answer below... using $lookslike=11100110011; running foreach, generated sql and ran in phpMyAdmin against a table of 6 "answers".
Answers were ranked as follows:
Answer -- rank
11100110011 -- 11
11100110010 -- 10
11100110000 -- 9
00000111010 -- 6
00000111111 -- 6
01101100110 -- 6
*edit for ranking explanation - 11 of 11 matches, 10 of 11 matches, 9 of 11 matches, 6 of 11 matches...
Very nice.
Added modified PHP manual example of multiple choice answers:
<?php
// Client Answers
$input = 'abcdbcdabcd';
// array of answers to check against
$answers = array('abcdbddabcd', 'abcbbcdabcd', 'abcdbcccccd');
// no shortest distance found, yet
$shortest = -1;
// loop through words to find the closest
foreach ($answers as $answer) {
// calculate the distance between the input word,
// and the current word
$lev = levenshtein($input, $answer);
// check for an exact match
if ($lev == 0) {
// closest word is this one (exact match)
$closest = $answer;
$shortest = 0;
// break out of the loop; we've found an exact match
break;
}
// if this distance is less than the next found shortest
// distance, OR if a next shortest word has not yet been found
if ($lev <= $shortest || $shortest < 0) {
// set the closest match, and shortest distance
$closest = $answer;
$shortest = $lev;
}
}
echo "Input answer: $input\n" . "<br />";
if ($shortest == 0) {
echo "Exact match found: $closest\n" . "<br />";
} else {
echo "Possible Alternative: $closest?\n" . "<br />";
echo "Levenshtein Distance: $lev\n";
}
?>
Returns:
Input answer: abcdbcdabcd
Possible Alternative: abcbbcdabcd?
Levenshtein Distance: 3