私はこのPerlスクリプト(Jeff Schallerのおかげで)を使って2つのCSVファイルのタイトルフィールドに3つ以上の単語をマッチさせています。ここ 元の質問:1つの問題は、私は私がある微調整したいことを見つけた、私のテスト中perlスクリプトを修正して、単語ベースで単語に一致させるようにしました。
#!/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;
:
は、私はまた、meuhからのアドバイス、次のいくつかの例外機能を追加しましたcsv2にthe
のような共通の単語が1つ含まれている場合、csv1に3回以上複製されていれば、3つの正の一致が見つかります。明確にする:
をCSV1が含まれている場合:
1216454,the important people feel the same way as the others, 15445454, 45445645
^すなわちCSV2が含まれている場合the
の3 insatncesは、上記の行
にあります
は14564564,the tallest man on earth,546456,47878787
^すなわち、1つありますこの行のthe
のインスタンス
次に、ファイルの1つに一致する単語のインスタンスが1つしかないため、マッチングとして分類される単語は1つだけとし、出力する必要はありません。
しかし場合:
1216454,the important people feel the same way as the others,15445454, 45445645
とCSV2が含まれる:
CSV1が含ま
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
I次いで、CSV内のワード、及び他のCSV内の同じ単語の複数のインスタンスの1つのインスタンスが存在する場合には、一方のみ一致として分類されるようにスクリプトを修正したい
。しかし、両方のファイルにthe
という単語が3つ存在する場合、3つの一致として分類されます。基本的には、マッチを単語ベースの単語にしたいと思います。 これ以外のスクリプトに関するものはすべて完璧ですので、これ以外のすべてに満足しているので、私は描画ボードに完全に戻っていません。 私はそれを説明したことを願っています。
あなたの質問を編集して、解析する必要がある入力と表示したい出力の例を教えてください。コンテキストに関する以前の質問へのリンクも役立ちます。あなたが望むのは、同じ単語の繰り返しを無視して、ユニークなマッチだけを数えることです。あれは正しいですか? – terdon