【发布时间】:2016-09-26 04:21:18
【问题描述】:
我一直在使用这个 perl 脚本(感谢 Jeff Schaller)来匹配两个单独 csv 文件的标题字段中的 3 个或更多单词。 这里的原始问题:
我还根据 meuh 的建议添加了一些异常功能:
#!/bin/perl
my @csv2 = ();
open CSV2, "<csv2" or die;
@csv2=<CSV2>;
close CSV2;
my %csv2hash = ();
for (@csv2) {
chomp;
my ($title) = $_ =~ /^.+?,\s*([^,]+?),/; #/ match the title
$csv2hash{$_} = $title;
}
open CSV1, "<csv1" or die;
while (<CSV1>) {
chomp;
my ($title) = $_ =~ /^.+?,\s*([^,]+?),/; #/ match the title
my @titlewords = split /\s+/, $title; #/ get words
my @new; #add exception words which shouldn't be matched
foreach my $t (@titlewords){
push(@new, $t) if $t !~ /^(and|if|where)$/i;
}
@titlewords = @new;
my $desired = 3;
my $matched = 0;
foreach my $csv2 (keys %csv2hash) {
my $count = 0;
my $value = $csv2hash{$csv2};
foreach my $word (@titlewords) {
++$count if $value =~ /\b$word\b/i;
last if $count >= $desired;
}
if ($count >= $desired) {
print "$csv2\n";
++$matched;
}
}
print "$_\n" if $matched;
}
close CSV1;
在我的测试过程中,我发现我想要调整的一个问题是,如果 csv2 包含单个常用词,例如 the,如果在 csv1 中重复三次或更多次,则会找到三个正匹配.澄清一下:
如果 csv1 包含:
1216454,the important people feel the same way as the others, 15445454, 45445645
^ 即上面的行中有三个the 不满足
如果 csv2 包含:
14564564,the tallest man on earth,546456,47878787
^ 即这一行中有一个the 的实例
然后我希望只有一个单词被归类为匹配,并且没有输出(基于我想要的匹配单词数量 - 3),因为其中一个文件中只有一个匹配单词的实例。
但是如果:
csv1 包含:
1216454,the important people feel the same way as the others,15445454, 45445645
和 csv2 包含:
15456456,the only way the man can sing the blues,444545,454545
然后,由于每个单词中有三个匹配的单词(即每个标题中单词 the 的 3 个实例,那么我希望根据我想要的匹配单词数量为 3 或更多,从而生成输出:
1216454,the important people feel the same way as the others,15445454, 45445645
15456456,the only way the man can sing the blues,444545,454545
我想修改脚本,以便如果 csv 中有一个单词的一个实例,而另一个 csv 中有同一个单词的多个实例,那么它被归类为只有一个匹配项。但是,如果在两个文件中都有单词 the 的 3 个实例,那么它仍应归类为三个匹配项。基本上我希望匹配是逐字逐句的。
除了这个之外,关于剧本的一切都是完美的,所以我宁愿不完全回到绘图板上,因为我对除此之外的一切都很满意。
我希望我已经解释过了,如果有人需要任何澄清,请告诉我。
【问题讨论】:
-
请edit您的问题,并给我们一个您需要解析的输入和您希望看到的输出的示例。指向您之前的问题的链接也将有所帮助。我认为您想要的是仅计算唯一匹配项,而忽略相同单词的重复。对吗?
标签: text-processing perl