I have two files which I want to read line by line (the first contains a word per line and the second a sentence per line).
The goal is to calculate the number of the sentences from file 2
containing a word being in file 1
.
Here is my code:
open( my $words, '<:utf8', 'test' ) or die "Unable to open for read: $!"; `#test file is the file that contain my words`
open( my $sentences, '<:utf8', 'sentences' ) or die "Unable to open for read: $!"; `#sentences fila that contain one sentence per line`
open my $fh_resultat, ">:utf8", 'result';
my $word;
#i want to calculate the number of sentences from my $sentences that containe word from my file $words
while( defined( $word = <$words> ) ) {
chomp $word ;
$word =~ s/^s*|s*$//g;
my $nb = 0;
my $idf;
my $ph;
while (defined ( $ph = <$sentences> ) ){
my @tab = split(/ /, $ph);
chomp @tab ;
foreach my $val(@tab) {
if($word eq $val){
$nb = $nb + 1;
last;
}
}
}
print $fh_resultat "$word:$nb
";
}
but the processing is only performed for the first word of the first file!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…