// swap(0,1,'the')=hte function swap($posa,$posb,$word){ $a = $word{$posa}; $b = $word{$posb}; $word{$posa} = $b; $word{$posb} = $a; return $word; } //condense('coffee',2)=cofee function condense($word,$pos){ $len = strlen($word); for($a=0;$a<$len;$a++){ if($a==$pos){ $a++; } // skip ahead one.. $ret .= $word{$a}; } return $ret; } /** * Trims an array from empty elements. * * @param $a the array to trim. * @return a new array with the empty elements removed. */ function array_trim($a) { $j = 0; for ($i = 0; $i < count($a);$i++) { if ($a[$i] != "") { $b[$j++] = $a[$i]; } } return $b; } /* note that the algorithm in the following function was written by the generous joeldg on http://lucifer.intercosmos.net/?view=SNIP&item=115 which is where i found it */ function generateTypos ($word, $depth=4) { // depth defines just how far to go with generating typos.. //$depth = 4; ## Physical errors // all keyboard specific typos based on QWERTY keyboards // letters near keyboard edge and corners have fewer // letters in order of most commonly used within english language # center sdfghjk # top wertyuio # bottom xcvbn # corners qpzm # left qaz # right plm /* created with : $freq = explode(" ", $lfreq); while(list($key,$val)=each($common)){ reset($freq); echo "$"."common['".$key."'] = array("; for($a=0;$a= $wlen)){ $w2[$a] = $word{$a}.$word{$a+1}; } } ############## most common typos ############## // first dual letters.. while(list($key,$val)=each($dualletter)){ if(in_array($val, $w2)){ $arrval = array_search ($val, $w2); $typos[] = swap($arrval, $arrval+1, $word); } } // second double letters. while(list($key,$val)=each($doubleletter)){ if(in_array($val, $w2)){ $arrval = array_search ($val, $w2); $typos[] = condense($word, $arrval); } } ############## ############## ############### //now lets generate possible mechanical typos. silly fingers not finging.. while(list($key,$val)=each($w)){ $repls = $common[$val]; $d=0; for($a=0;$a=$depth){$a=100;} // breakout } } } return($typos); }