我一直在使用这个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),因为在其中一个文件中只有一个匹配单词的实例。 / p>
但是如果:
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中有同一个单词的多个实例,那么它只被归类为一个匹配。但是,如果在两个文件中都有3个单词the
的实例,那么它仍然应该被归类为三个匹配。基本上我希望匹配是逐字逐句的。
关于脚本以外的所有内容都是完美的,所以我不想完全回到绘图板,因为我对除此之外的所有内容感到满意。
我希望我已经解释好了,如果有人需要任何澄清,请告诉我。
答案 0 :(得分:1)
如果你只想计算唯一匹配,你可以使用哈希而不是列表来收集csv1
中的单词,就像你对csv2
一样,然后计算出现的次数每个单独的单词:
#!/usr/bin/env 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 %words;
$words{$_}++ for split /\s+/, $title; #/ get words
## Collect unique words
my @titlewords = keys(%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) {
my @matches = ( $value=~/\b$word\b/ig );
my $numIncsv2 = scalar(@matches);
@matches = ( $title=~/\b$word\b/ig );
my $numIncsv1 = scalar(@matches);
++$count if $value =~ /\b$word\b/i;
if ($count >= $desired || ($numIncsv1 >= $desired && $numIncsv2 >= $desired)) {
$count = $desired+1;
last;
}
}
if ($count >= $desired) {
print "$csv2\n";
++$matched;
}
}
print "$_\n" if $matched;
}
close CSV1;