gpt4 book ai didi

php - Smith–Waterman 用于 PHP 中的字符串?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:04:01 28 4
gpt4 key购买 nike

我需要 smith-waterman 在 php 中,你知道是否已经有一个实现吗?

我需要这个算法来进行邻近搜索 ( Function that returns affinity between texts? )

如果没有我会努力做到的。

谢谢

最佳答案

我对 SimMetrics java code for SmithWatermanGotoh 进行了逆向工程- 我发现 SmithWatermanGotoh 比 SmithWaterman 更有效,结果非常相似:

class SmithWatermanGotoh 
{
private $gapValue;
private $substitution;

/**
* Constructs a new Smith Waterman metric.
*
* @param gapValue
* a non-positive gap penalty
* @param substitution
* a substitution function
*/
public function __construct($gapValue=-0.5,
$substitution=null)
{
if($gapValue > 0.0) throw new Exception("gapValue must be <= 0");
//if(empty($substitution)) throw new Exception("substitution is required");
if (empty($substitution)) $this->substitution = new SmithWatermanMatchMismatch(1.0, -2.0);
else $this->substitution = $substitution;
$this->gapValue = $gapValue;
}

public function compare($a, $b)
{
if (empty($a) && empty($b)) {
return 1.0;
}

if (empty($a) || empty($b)) {
return 0.0;
}

$maxDistance = min(mb_strlen($a), mb_strlen($b))
* max($this->substitution->max(), $this->gapValue);
return $this->smithWatermanGotoh($a, $b) / $maxDistance;
}

private function smithWatermanGotoh($s, $t)
{
$v0 = [];
$v1 = [];
$t_len = mb_strlen($t);
$max = $v0[0] = max(0, $this->gapValue, $this->substitution->compare($s, 0, $t, 0));

for ($j = 1; $j < $t_len; $j++) {
$v0[$j] = max(0, $v0[$j - 1] + $this->gapValue,
$this->substitution->compare($s, 0, $t, $j));

$max = max($max, $v0[$j]);
}

// Find max
for ($i = 1; $i < mb_strlen($s); $i++) {
$v1[0] = max(0, $v0[0] + $this->gapValue, $this->substitution->compare($s, $i, $t, 0));

$max = max($max, $v1[0]);

for ($j = 1; $j < $t_len; $j++) {
$v1[$j] = max(0, $v0[$j] + $this->gapValue, $v1[$j - 1] + $this->gapValue,
$v0[$j - 1] + $this->substitution->compare($s, $i, $t, $j));

$max = max($max, $v1[$j]);
}

for ($j = 0; $j < $t_len; $j++) {
$v0[$j] = $v1[$j];
}
}

return $max;
}
}

class SmithWatermanMatchMismatch
{
private $matchValue;
private $mismatchValue;

/**
* Constructs a new match-mismatch substitution function. When two
* characters are equal a score of <code>matchValue</code> is assigned. In
* case of a mismatch a score of <code>mismatchValue</code>. The
* <code>matchValue</code> must be strictly greater then
* <code>mismatchValue</code>
*
* @param matchValue
* value when characters are equal
* @param mismatchValue
* value when characters are not equal
*/
public function __construct($matchValue, $mismatchValue) {
if($matchValue <= $mismatchValue) throw new Exception("matchValue must be > matchValue");

$this->matchValue = $matchValue;
$this->mismatchValue = $mismatchValue;
}

public function compare($a, $aIndex, $b, $bIndex) {
return ($a[$aIndex] === $b[$bIndex] ? $this->matchValue
: $this->mismatchValue);
}

public function max() {
return $this->matchValue;
}

public function min() {
return $this->mismatchValue;
}
}

$str1 = "COELACANTH";
$str2 = "PELICAN";
$o = new SmithWatermanGotoh();
echo $o->compare($str1, $str2);

关于php - Smith–Waterman 用于 PHP 中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4898705/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com