RE: Regular expressions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Sorry, forgot to copy the group on my reply to Richard and John.

>On Tue, November 14, 2006 7:57 pm, John Meyer wrote:
>> Is there a way to make a regular expression to match on a particular 
>> way the letters are arranged?  For instance, if you had a word:
>>
>> THAT
>>
>>
>> It could match on any word in the dictionary that had the form:
>>
>> 1231
>
>I suspect if you worked hard enough at it you could get a regex to do 
>what you want...
>
>But it might be a heck of a lot easier to just build a database like
>this:
>
><?php
>  $path = "/usr/share/web2"; //Webster's 2nd ed. dictionary, public 
>domain, on some distros...
>  $file = fopen($path, 'r') or die("You may need to cp $path to your 
>own dir, as it is not readable here...");
>  while (!feof($file)){
>    $word = fgets($file, 1000000);
>    $code = wordcode($word);
>    //store $code | $word in some convenient way...
>    //perhaps a database
>    //perhaps each $code could be a file with all the $word[s] in it
>    //etc
>  }
>
>  function wordcode($word){
>    $code = array();
>    $result = '';
>    $len = strlen($word);
>    for ($i = 0; $i < $len; $i++){
>      $c = $word[$i]; //or use substr if you hate string as array
syntax
>      if (!isset($code[$c])){
>        $code[$c] = count($code) + 1;
>      }
>      $d = $code[$c];
>      $result .= $d;
>    }
>  }
>?>
>
>NOTE:
>For any word with more then 9 unique characters, you are going to have 
>SERIOUS problems...
>
>Consider this:
>supercalifragilisticexpialidocious
>123456789105711989... 
>
>
>How can you tell that '11' for the 'g' isn't supposed to be '1'
>followed by '1' for two 's' characters in a row?
>supercalifrassilisticexpialidocious
>123456789105711989...
>
>You may need to represent your digits in, say, hex form, or even in 
>base 26 form, which gives you the full alphabet as unique digits.
>
>--
>Some people have a "gift" link here.
>Know what I want?
>I want you to buy a CD from some starving artist.
>http://cdbaby.com/browse/from/lynch
>Yeah, I get a buck. So?


Believe it or not I had to do something like this a while back.  Here is
a function.  I have tweaked it to fit your needs a bit, but the example
works.  It seems to sort of follow what Lynch's psuedocode loosely.

<?php

/*
 *function convertToPattern converts a string into a pattern based on
letter
 *frequency and position
 *
 *  ex:  "that" would convert to abca
 *       "pretty" would convert to abcdde
 */
function convertToPattern($strCandidate){

//Set up the array that holds mapping of character to pattern character	
	$arrHolder = array(
		"a"=>'',
		"b"=>'',
		"c"=>'',
		"d"=>'',
		"e"=>'',
		"f"=>'',
		"g"=>'',
		"h"=>'',
		"i"=>'',
		"j"=>'',
		"k"=>'',
		"l"=>'',
		"m"=>'',
		"n"=>'',
		"o"=>'',
		"p"=>'',
		"q"=>'',
		"r"=>'',
		"s"=>'',
		"t"=>'',
		"u"=>'',
		"v"=>'',
		"w"=>'',
		"x"=>'',
		"y"=>'',
		"z"=>''
	);

	//Now create an array with all of the letters of the alphabet in
	//it for reference
	$arrAlpha = array();
	$idxAlphCreate = 0;

	foreach($arrHolder as $key => $data){
		$arrAlpha[$idxAlphCreate] = $key;
		$idxAlphCreate++;
	}

	//split the string into an array so we can walk it
	$arrCandidate = str_split($strCandidate);
	$cntCandidate = count($arrCandidate);
	$idxCandidate = 0;
	$idxHolder = 0;

	//walk through the array made from the string
	while($idxCandidate < $cntCandidate){
		//get the current char		
		$charCandidate = $arrCandidate[$idxCandidate];
		
		//if the char is already in the array it has been mapped
		//if not, map it to the next available letter
		if(!(in_array($charCandidate, $arrHolder))){
			$arrHolder[$arrAlpha[$idxHolder]] =
$charCandidate; 
			$idxHolder++;
		}
		
		$idxCandidate++;
	}
	
	//initialize our converted string
	$strConverted = "";

	//loop through the array one more time to create our converted
string
	foreach($arrCandidate as $char){
		$arrKeyCheck=array_keys($arrHolder, $char);
		$strConverted.=$arrKeyCheck[0];
		
	}	

	return $strConverted;	
}
//Example
$strA = "that";
$strB = "croc";
$patternA = convertToPattern($strA);
$patternB = convertToPattern($strB);


if($patternA == $patternB){
	echo "Bingo!";
}else{
	echo "Not so much...";
}
?>

HTH,
Jesse R. Castro
Applications Development
Pocket Communications

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux