Example #1
0
function find_similar_pictures($md5, $cvec, $threshold = PUZZLE_CVEC_SIMILARITY_THRESHOLD)
{
    $compressed_cvec = puzzle_compress_cvec($cvec);
    $words = split_into_words($cvec);
    $dbh = new PDO(DB_DSN);
    $dbh->beginTransaction();
    $sql = 'SELECT DISTINCT(signature_id) AS signature_id FROM words ' . 'WHERE pos_and_word IN (';
    $coma = FALSE;
    foreach ($words as $u => $word) {
        if ($coma === TRUE) {
            $sql .= ',';
        }
        $sql .= $dbh->quote(chr($u) . puzzle_compress_cvec($word));
        $coma = TRUE;
    }
    $sql .= ')';
    $res_words = $dbh->query($sql);
    $scores = array();
    $st = $dbh->prepare('SELECT compressed_signature, picture_id ' . 'FROM signatures WHERE id = :id');
    while (($signature_id = $res_words->fetchColumn()) !== FALSE) {
        $st->execute(array(':id' => $signature_id));
        $row = $st->fetch();
        $found_compressed_signature = $row['compressed_signature'];
        $picture_id = $row['picture_id'];
        $found_cvec = puzzle_uncompress_cvec($found_compressed_signature);
        $distance = puzzle_vector_normalized_distance($cvec, $found_cvec);
        if ($distance < $threshold && $distance > 0.0) {
            $scores[$picture_id] = $distance;
        }
    }
    $sql = 'SELECT url FROM sentpictures WHERE picture_id IN (';
    $coma = FALSE;
    foreach ($scores as $picture_id => $score) {
        if ($coma === TRUE) {
            $sql .= ',';
        }
        $sql .= $dbh->quote($picture_id);
        $coma = TRUE;
    }
    $sql .= ')';
    $urls = array();
    if (!empty($scores)) {
        $res_urls = $dbh->query($sql);
        while (($url = $res_urls->fetchColumn()) !== FALSE) {
            array_push($urls, $url);
        }
    }
    $dbh->commit();
    return $urls;
}
Example #2
0
function update_search_index($pid, $msg)
{
    global $db;
    $words = split_into_words($msg, true);
    $db->query('DELETE FROM `#^search_index` WHERE post_id=' . $pid) or error('Failed to delete existing search stuff', __FILE__, __LINE__, $db->error());
    $q = array();
    $locations = array();
    foreach ($words as $key => $word) {
        if (isset($locations[$word])) {
            $locations[$word][] = $key;
        } else {
            $locations[$word] = array($key);
        }
    }
    foreach ($locations as $word => $curlocs) {
        if (trim($word) != '') {
            $q[] = '(' . $pid . ',\'' . $db->escape(strtolower($word)) . '\',\'' . $db->escape(implode(',', $curlocs)) . '\')';
        }
    }
    if (!empty($q)) {
        $query = new DBMassInsert('search_index', array('post_id', 'word', 'locations'), $q, 'Failed to insert search engine data');
        $query->commit();
    }
}
Example #3
0
 function __construct($message, $time, $id)
 {
     $this->mwords = split_into_words(strtolower($message));
     $this->time = $time;
     $this->id = $id;
 }