0

我正在尝试使用 CodeIgniter 和 Data Mapper 将两个字符串与 PHP 和 MySQL 进行比较,我找到了similar_textPHP 的函数并在此函数中使用它:

$v = new Vendedor();
$v -> get();
foreach ($v as $vv) {
    $n = $vv -> name . " " . $vv -> lasta_name1 . " " . $vv -> last_name2;
    $vv -> sorteo -> get();
    similar_text($n, $name, $sim);
    if ($sim > 20) {
        $vv -> boleto -> get();
        $ven[$i] = array(
            'id' => $vv -> id, 
            'nombre' => $n, 
            'telefono' => $vv -> phone, 
            'mail' => $vv -> email,  
        );
        $i++;
    }
}

return $ven;

有人相当于similar_textData Mapper 吗?太感谢了!

4

1 回答 1

0

在 PHP 的函数中实现的算法similar_text()在 MySQL 中不可用。mysql 中有soundex可以与如下表达式一起使用:

... where name sounds like "some input"

在 sql 中,所以在你的 PHP 代码中,这看起来像:

$v = new Vendedor();
$v->where('CONCAT(name, " ", lasta_name1, " ", lasta_name2) sounds like ', $name);
// ...

但 soundex 并不真正适用于非英文文本。您也可以尝试各种 levenshtein 实现,请参见thisthis

这两者的性能仍然很差,因为数据库仍然必须比较每一行,所以你在这方面并没有真正赢得太多,但是你改变了相似性算法,这可能不行。

如果您决定保留该similar_text()功能,则可以在数组上实现分页,array_slice()最好只创建一次订单并将信息保存在某个缓存中(memcached、纯文件、apc 缓存...),以便使用此输入的后续调用可以无需重新计算订单即可提供服务,因此速度更快。我想象这样的事情(缓存部分是可选的):

function get_similar_vevendors($name, $offset, $limit) {
    // Imaginary cache library, 
    // it would return the previously cached search results under a key 
    // created from the name
    if (Cache::has('similar_vevendors_'.$name)) {
        $ven = Cache::get('similar_vevendors_'.$name)
    } else {
        // your orignal code here, filling $ven array filled with similar Vendedor's
        // ...
        // saving the created similar vevendors array in cache
        Cache::store('similar_vevendors_'.$name, $ven);
    }

    // return both the count of the results for pagination 
    // and a acting as the current "page" 
    return array(count($ven), array_slice($ven, $offset, $limit));
}

这样,您可以使用与在 SQL 中使用的数组相同的参数进行分页,并且可以使用以下内容初始化 CI 的分页库:

// $offset is coming from the links generated by the pager
list($total_rows, $paged_vens) = get_similar_vevendors($name, $offset, $items_per_page);
$this->pagination->initialize(array(
    'per_page'   => $items_per_page,
    'total_rows' => $total_rows,
));
于 2013-04-18T20:01:10.040 回答