Choose a file to view:
index.php  word.php  functions.php


<?PHP
/* Copyright (c) 2011 Brad Greco [...]
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 
*/

// connect to database
mysql_connect("localhost""*******""*******");
@
mysql_select_db("bgreco") or die( "Unable to select database");

/* periodic( string $word, array $elements )
 * if $long is true, it will try to use more one-character symbols
 * Returns $word spelled using periodic table symbols
 * Returns false if this is impossible
 */
function periodic($string, &$array$long false) {
  if(
in_array_nocase($string$array) && $long)
    return 
elementCase($string$array);
  if(!
$string)
    return 
false
  
$size1 1;
  
$size2 2;
  if(
$long) {
    
$size1 2;
    
$size2 1;
  }
  
$h1 substr($string0$size1);
  
$h2 substr($string0$size2);
  
$t1 substr($string$size1);
  
$t2 substr($string$size2);
  
  
$r1 periodic($t1$array$long);
  if(
$r1 && in_array_nocase($h1$array))
    return 
elementCase($h1$array).$r1;
  
$r2 periodic($t2$array$long);
  if(
$r2 && in_array_nocase($h2$array))
    return 
elementCase($h2$array).$r2;
  if(
in_array_nocase($string$array) && !$long)
    return 
elementCase($string$array);
  return 
false;
}

function 
save($phrase) {
     
mysql_query(sprintf('INSERT INTO `spelled` (phrase) VALUES ("%s") ON DUPLICATE KEY UPDATE count=count+1'mysql_real_escape_string($phrase)));
}

function 
countspelled($phrase) {
    
$result mysql_query(sprintf('SELECT count FROM `spelled` WHERE phrase="%s"'mysql_real_escape_string($phrase)));
    return 
mysql_result($result0);
}

// function taken from the PHP reference manual
function in_array_nocase($search, &$array) {
  
$search strtolower($search);
  foreach (
$array as $item)
    if (
strtolower($item) == $search)
      return 
TRUE;
  return 
FALSE;
}

function 
elementCase($string, &$array) {
  foreach( 
$array as $item) {
    if( 
strtolower($item) == strtolower($string))
      return 
$item;
  }
}

function 
best_matches($word) {
    
$min_distance 999;
    
$best_matches = array();

    
$result mysql_query(sprintf("SELECT words FROM bgreco.metaphone WHERE metaphone = '%s';"mysql_real_escape_string(metaphone($word))));
    if(!
$result)
        return;

    if(!
mysql_num_rows($result))
        return 
$best_matches;

    
$matches explode(','mysql_result($result0));
    foreach(
$matches as $match) {
        
$distance levenshtein(strtolower($word), strtolower($match));
        if(
$distance <= $min_distance) {
            if(
$distance $min_distance)
                
$best_matches = array();
            
array_push($best_matches$match);
            
$min_distance $distance;
        }
    }
    return 
$best_matches;
}


function 
elements() {
  
$elements = array();
  
$result mysql_query("SELECT symbol FROM periodic");
  while(
$row mysql_fetch_row($result))
    
$elements[] = $row[0];
  return 
$elements;
}


?>