chdir("c:/perl/normalized");
$docid=0;
use List::MoreUtils qw( uniq );
my %hash = ();
@files = <*>;
foreach $file (@files)
{
$docid++;
open (input, $file);
while (<input>)
{
open (output,'>>c:/perl/postinglist/total');
chomp;
(@words) = split(" ");
foreach $word (@words)
{
push @{ $hash{$word} }, $docid;
}
}
}
foreach $key (sort keys %hash)
{
$size = scalar (@{$hash{$key}});
print output "Term: $key, Frequency:$size, Document(s):", join(" ", uniq @{ $hash{$key} }), "\n";
}
close (input);
close (output);
join(" ", uniq @{ $hash{$key} })
出力が次のようになる前に:
Term:of Frequency:35 Document(s): 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 4 4 4 4 5 6 6 7 7 7 7 7 7 7 7 7
ドキュメントは、周波数が配布された場所を示しています
Term:of Frequency:35 Document(s):1 2 3 4 5 6 7
ここまでは問題ありません...重複を削除するためのカウンターを保持したいのですが、そのような私の新しい出力は
Term:of Frequency:35 Document(s) of: 1(10) 2(7) 3(2) 4(4) 5(1) 6(2) 7(9)
それが値(カウンター)です
ソースコードにいくつかの変更を加えることで、自分の問題を修正できました
chdir("c:/perl/normalized");
$docid=0;
my %hash = ();
@files = <*>;
foreach $file (@files)
{$counter=0;
$docid++;
open (input, $file);
while (<input>)
{
open (output,'>>c:/perl/tokens/total');
chomp;
(@words) = split(" ");
foreach $word (@words)
{
push @{ $hash{$word}{$docid}},$counter;
@{$hash{$word}{$docid}}[$counter]++;
}
}
}
foreach my $line (sort keys %hash) {
print output "Term:$line \n";
foreach my $elem (sort keys %{$hash{$line}}) {
print output" Doc:$elem " . "freq:".@{$hash{$line}->{$elem}} . "\n";
}
}
close (input);
close (output);