Re: Regular expressions

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

 



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?

-- 
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