数组上的 perl String::Approx

发布于 2024-11-07 20:51:04 字数 1373 浏览 3 评论 0原文

我正在使用 String::Approx 来查找两个最相似的匹配-其他列表中的项目数组。我惊喜地发现您可以使用 amatch() 来比较数组与数组,尽管该功能没有文档记录;我准备编写自己的函数来做到这一点。我更惊讶地发现元素的顺序并不重要。但是,尽管 amatch() 工作完美,但我在使用 adist() 时遇到了困难。考虑以下程序:

#! /usr/bin/perl

use String::Approx qw (amatch adist);

@matches = qw();
%matchhash = qw();
@matchstr = qw(cat dog);
@poss = (['rat', 'hog'],
     ['gnat', 'frog'],
     ['giraffe', 'elephant'],
     ['dig', 'bat'],
     ['catatonic', 'doggone'],
     ['care', 'dog'],
     ['care', 'ding'],
     ['hawk', 'shark']);

@matches = grep { amatch (@matchstr, @$_) } @poss;

foreach $k (@matches)
{
    $dist = adist( @matchstr, @$k );
    print "@matchstr has a difference from @$k of $dist \n";
}

这是它的输出:

cat dog has a difference from rat hog of 3
cat dog has a difference from gnat frog of 3
cat dog has a difference from dig bat of 3 
cat dog has a difference from catatonic doggone of 3
cat dog has a difference from care dog of 3
cat dog has a difference from care ding of 3

因此,它似乎选择了正确的答案(它忽略了 ['giraffe', 'elephant']['hawk', ' shark']),但它无法告诉我距离。最终目标是按距离对比赛进行排序,并选择最像 @matchstr 的一场。 amatch() 是否真的像我想象的那样工作,或者我只是使用了太简单的输入?为什么 amatch() 不起作用?

I'm using String::Approx to find the most alike match for a two-item array out of a list of others. I was pleasantly surprised to find that you can use amatch() to compare an array to an array although that feature is not documented; I was prepared to write my own function to do that. I was further surprised to see that the order of the elements doesn't matter. But, even though amatch() works flawlessly, I'm having a difficult time with adist(). Consider the following program:

#! /usr/bin/perl

use String::Approx qw (amatch adist);

@matches = qw();
%matchhash = qw();
@matchstr = qw(cat dog);
@poss = (['rat', 'hog'],
     ['gnat', 'frog'],
     ['giraffe', 'elephant'],
     ['dig', 'bat'],
     ['catatonic', 'doggone'],
     ['care', 'dog'],
     ['care', 'ding'],
     ['hawk', 'shark']);

@matches = grep { amatch (@matchstr, @$_) } @poss;

foreach $k (@matches)
{
    $dist = adist( @matchstr, @$k );
    print "@matchstr has a difference from @$k of $dist \n";
}

And here's what it outputs:

cat dog has a difference from rat hog of 3
cat dog has a difference from gnat frog of 3
cat dog has a difference from dig bat of 3 
cat dog has a difference from catatonic doggone of 3
cat dog has a difference from care dog of 3
cat dog has a difference from care ding of 3

So, it appears to be picking the right answers (it ignored ['giraffe', 'elephant'] and ['hawk', 'shark']), but it can't tell me the distance. The end goal is to order the matches by distance and pick the one most like @matchstr. Is amatch() actually working as well as I think it is, or am I just using too simple of input? Why isn't amatch() working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云仙小弟 2024-11-14 20:51:04

您不能将数组作为第一个参数传递给 amatch 或 adist 并让它按您的预期工作。

数组被解压成列表,所以 amatch 看到的是类似 amatch( 'cat', 'dog', 'rat', 'hog' ) 的东西,这当然不是你想要的。

您必须创建支持数组引用作为第一个参数的新版本的 amatch 和 adist。然后,您需要将 subs 调用为 my_amatch(\@matchstr, @$_)

You can not pass an array as the first argument to amatch or adist and have it work as you are expecting.

Arrays are unpacked into lists, so what amatch sees is something like amatch( 'cat', 'dog', 'rat', 'hog' ) which is of course not what you intended.

You will have to create new versions of amatch and adist that support an array reference as the first argument. You will then need to call the subs as my_amatch(\@matchstr, @$_)

落花随流水 2024-11-14 20:51:04

amatch 并没有按照你的想法去做。

如果将 qw(cat dog) 更改为 qw(cat zzz),您会得到相同的结果。

然后,如果将“hawk”、“shark”更改为“hawk”、“zzz”,您仍然会得到相同的结果。

看起来只是与“猫”进行比较。

amatch isnt' doing what you think it is.

If you change qw(cat dog) to qw(cat zzz) you get the same results.

Then, if you change "hawk", "shark" to "hawk", "zzz" you still get the same results.

It looks like it's only doing the comparison with "cat".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文