【问题标题】:"Distance" between data variable in PHPPHP中数据变量之间的“距离”
【发布时间】:2018-10-10 23:47:34
【问题描述】:

是否有任何方法(如果需要,使用库)来规范化任何 PHP 变量(整数、字符串、文件、字节数组等),因此可以通过它们之间的距离来测量这些数据?

我的意思是,F("hello") 应该接近 F("hell")。

但是,不仅适用于字符串,还适用于 AMONG nay 类型的数据。

我想首先将所有内容都传递给二进制文件,但 PHP 位管理并不是那么简单。 在 C++ 中,这可以更容易地完成。

例如,我应该能够计算 f("hello") 和 f(3333) 之间的距离。 (不同的数据类型)。

也许将所有内容都转储到字节数组中?

谢谢

【问题讨论】:

  • 如果您已经尝试过,请向我们展示代码并告诉我们会发生什么。如果你还没有尝试过任何东西,你应该先尝试。
  • PHP 中的字符串几乎已经是字节数组,尽管可能与您习惯使用的 C++ 不完全一样。 php.net/manual/en/…
  • 我将编辑帖子,我想计算任何类型的变量。例如,如果我在内存中有 2 个文件。任何编辑帮助以使这一点变得清晰(英语不是我的第一语言)。测量字符串不是问题,我的问题是对任何变量的通用测量。谢谢

标签: php binary distance metric


【解决方案1】:

Levenshtein function 可能值得研究。

来自 php.net 页面:

<?php
// input misspelled word
$input = 'carrrot';

// array of words to check against
$words  = array('apple','pineapple','banana','orange',
                'radish','carrot','pea','bean','potato');

// no shortest distance found, yet
$shortest = -1;

// loop through words to find the closest
foreach ($words as $word) {

    // calculate the distance between the input word,
    // and the current word
    $lev = levenshtein($input, $word);

    // check for an exact match
    if ($lev == 0) {

        // closest word is this one (exact match)
        $closest = $word;
        $shortest = 0;

        // break out of the loop; we've found an exact match
        break;
    }

    // if this distance is less than the next found shortest
    // distance, OR if a next shortest word has not yet been found
    if ($lev <= $shortest || $shortest < 0) {
        // set the closest match, and shortest distance
        $closest  = $word;
        $shortest = $lev;
    }
}

echo "Input word: $input\n";
if ($shortest == 0) {
    echo "Exact match found: $closest\n";
} else {
    echo "Did you mean: $closest?\n";
}

?>

上面的例子会输出:

输入词:胡萝卜

你的意思是:胡萝卜?

【讨论】:

    猜你喜欢
    • 2015-03-25
    • 2021-05-02
    • 1970-01-01
    • 2010-12-10
    • 2013-10-03
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多