All pastes #38863 Raw Edit

Anonymous

public php v1 · immutable
#38863 ·published 2006-01-28 01:49 UTC
rendered paste body
// swap(0,1,'the')=htefunction swap($posa,$posb,$word){    $a = $word{$posa};    $b = $word{$posb};    $word{$posa} = $b;    $word{$posb} = $a;    return $word;}//condense('coffee',2)=cofeefunction 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 emptyelements 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<count($freq);$a++){ if(in_array($freq[$a], $val)){ echo "'".$freq[$a]."',"; } }    echo ");\n"; }*/$common['a'] = array('s','z','q');$common['b'] = array('n','g','v');$common['c'] = array('d','v','x');$common['d'] = array('e','s','c','f');$common['f'] = array('r','t','c','d','g','b');$common['g'] = array('r','n','t','h','b','f');$common['h'] = array('n','t','g','b','y','j');$common['i'] = array('o','u','k');$common['j'] = array('i','n','u','m','h','k');$common['k'] = array('i','o','l','m','j');$common['l'] = array('p','m','k');$common['m'] = array('n','k','j');$common['n'] = array('m','h','b');$common['o'] = array('i','l','p');$common['p'] = array('o','l');$common['q'] = array('a','w');$common['r'] = array('e','t','g','f');$common['s'] = array('e','a','d','w','x');$common['t'] = array('r','g','y');$common['u'] = array('i','y','j');$common['v'] = array('c','b','f');$common['w'] = array('e','s','q');$common['x'] = array('s','c','z');$common['y'] = array('t','u','h');$common['z'] = array('s','a','x');# from# http://deafandblind.com/word_frequency.htm# $lfreq = "e t a o i n s r h l d c u m f p g w y b v k x j q z"; // standard$lfreq = explode(" ", "e i s a r n t o l c d u g p m h b y f v k w z x j q"); // based on 45k words$firstletter = explode(" ", "t o a w b c d s f m r h i y e g l n o u j k");$secondletter = explode(" ", "h o e i a u n r t");$lastletter = explode(" ", "e s t d n r y f l o g h a k m p u w");$dualletter = explode(" ", "th he an in er on re ed nd ha at en es of nt ea ti to io le is ou ar as de rt ve");$doubleletter = explode(" ", "ss ee tt ff ll mm oo");if($word){    $wlen = strlen($word);    for($a=0;$a<$wlen;$a++){        $w[] = $word{$a};        if(!($a+1 >= $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<count($repls);$a++){                $tmp = $word;                $tmp{$key} = $repls[$a];                $typos[] = $tmp;                $d++;                if($d>=$depth){$a=100;} // breakout            }    }}return($typos);}