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.
 */

function db_connect() {
    return new PDO('mysql:host=localhost;dbname=bgreco', '*******', '*******');
}

/* 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($string, 0, $size1);
    $h2 = substr($string, 0, $size2);
    $t1 = substr($string, $size1);
    $t2 = substr($string, $size2);

    if (in_array_nocase($h1, $array)) {
        if ($r1 = periodic($t1, $array, $long)) {
            return elementCase($h1, $array).$r1;
        }
    }
    if (in_array_nocase($h2, $array)) {
        if ($r2 = periodic($t2, $array, $long)) {
            return elementCase($h2, $array).$r2;
        }
    }
    if(in_array_nocase($string, $array) && !$long)
        return elementCase($string, $array);
    return false;
}

function save($db, $phrase) {
    $db->prepare('INSERT INTO `spelled` (phrase) VALUES (?) ON DUPLICATE KEY UPDATE count=count+1')->execute(array($phrase));
}

function countspelled($db, $phrase) {
    $query = $db->prepare('SELECT count FROM `spelled` WHERE phrase = ?');
    $query->execute(array($phrase));
    return $query->fetchColumn();
}

// 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($db, $word) {
    $min_distance = 999;
    $best_matches = array();

    $query = $db->prepare('SELECT words FROM `metaphone` WHERE metaphone = ?');
    $query->execute(array(metaphone($word)));
    $match_list = $query->fetchColumn();

    if ($match_list) {
        $matches = explode(',', $match_list);
        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;
    } else {
        return [];
    }
}


function elements($db, $symbols_only = false) {
    if ($symbols_only)
        return $db->query('SELECT symbol FROM periodic')->fetchAll(PDO::FETCH_COLUMN);
    else
        return array_map('current', $db->query('SELECT symbol, name, number, mass FROM periodic')->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC));
}


?>
prepare('INSERT INTO `spelled` (phrase) VALUES (?) ON DUPLICATE KEY UPDATE count=count+1')->execute(array($phrase)); } function countspelled($db, $phrase) { $query = $db->prepare('SELECT count FROM `spelled` WHERE phrase = ?'); $query->execute(array($phrase)); return $query->fetchColumn(); } // 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($db, $word) { $min_distance = 999; $best_matches = array(); $query = $db->prepare('SELECT words FROM `metaphone` WHERE metaphone = ?'); $query->execute(array(metaphone($word))); $match_list = $query->fetchColumn(); if ($match_list) { $matches = explode(',', $match_list); 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; } else { return []; } } function elements($db, $symbols_only = false) { if ($symbols_only) return $db->query('SELECT symbol FROM periodic')->fetchAll(PDO::FETCH_COLUMN); else return array_map('current', $db->query('SELECT symbol, name, number, mass FROM periodic')->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC)); } ?>