Php operator that evaluates TRUE on a answer typo -


i busy building elo students in primary education.

some questions in elo 'open', students should type answer in textbox.

suppose there question correct answer "scanner". student makes typo , types "scaner". don't want mark answer incorrect.

so wonder if there operator 'resembles' in php, same like in mariadb sql or double 'tilde' in math.

i quite sure there workarounds 'problem', i'm eager learn.

check out:

http://php.net/manual/en/function.levenshtein.php

the levenshtein algorithm great exact scenario. works typos or brain-fart moments typing "blew" instead of "blue", names, etc.

levenshtein return number. number indicates "distance" between 2 words. i'd in scenario, make maximum distance small number, 2 or 3. way, if there 1 character correction necessary, it'll fine. however, if word "scanner", , input "skammer", not pass valid response.

here few examples:

<?php  $threshold = 2;  $words = array('scanner', 'scaner', 'skanner', 'skammer', 'clammer',     'skaner'); $match = "scanner";  foreach($words $word){     echo levenshtein($match, $word) . "<br>"; } 

the above output following:

0 1 1 3 4 2 

so can see correlation there between closely related words, , not-so closely related words. so, above threshold, if change our code bit can this:

<?php  $threshold = 2;  $words = array('scanner', 'scaner', 'skanner', 'skammer', 'clammer', 'skaner'); $match = "scanner";  foreach($words $word){     if(levenshtein($match, $word) <= $threshold) echo "$word close enough $match! <br>";         else echo "$word not close enough $match! <br>"; } 

we'll response this:

scanner close enough scanner!  scaner close enough scanner!  skanner close enough scanner!  skammer not close enough scanner!  clammer not close enough scanner!  skaner close enough scanner!  

notice how "clammer" distance of 4 "scanner". let me explain bit. distance amount of characters have change in order word match. so, "c" has change, "l" has change, , both "m"s have change. thus, score of 4.

when using this, please consider "s" , "s" 2 totally different characters, absolutely case sensitive. i'd make case-insensitive make absolutely sure "scanner" doesn't marked incorrect answer. so:

<?php  $threshold = 2;  $words = array('scanner', 'scaner', 'skanner', 'skammer', 'clammer', 'skaner'); $match = "scanner";  foreach($words $word){     if(levenshtein(strtolower($match), strtolower($word)) <= $threshold) echo "$word close enough $match! <br>";         else echo "$word not close enough $match! <br>"; } 

words of caution

i highly advise not relying on soundex(). check out example:

<?php  $threshold = 2;  $words = array('spectacular', 'spectacle'); $match = "spectacle";  foreach($words $word){     if(levenshtein(strtolower($match), strtolower($word)) <= $threshold) echo "$word close enough $match! <br>";         else echo "$word not close enough $match! <br>";      echo soundex($word) . "/" . soundex($match) . "<br>"; } 

that example gives result:

spectacular not close enough spectacle!  s123/s123 spectacle close enough spectacle!  s123/s123 

two totally separate words don't sound alike, , have been perfect match according soundex()! while think it's useful function applications, not enough solution kind of application. here, example:

<?php  $threshold = 2;  $words = array('clancy', 'klancy'); $match = "clancy";  foreach($words $word){ if(levenshtein(strtolower($match), strtolower($word)) <= $threshold) echo "$word close enough $match! <br>";         else echo "$word not close enough $match! <br>";      echo soundex($word) . "/" . soundex($match) . "<br>"; } 

output:

clancy close enough clancy!  c452/c452 klancy close enough clancy!  k452/c452 

bottom line: don't rely on soundex() kind of application. end fighting , burnt in process.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -