in reply to Removing common words

Here's somewhere to start. Completely untested code, I just threw this together in the reply textarea :). It may not work at all or it may have glitches. Give it a shot if you will, change whatever you may wish. I'd say that the definition of a "word" your regex uses could use some fixing.

my @banned = qw( a at be for and to of in the as i it are is am on an you me ); push( @banned, 0..10, 'a'..'z' ); my %banned; @banned{@banned} = undef; for my $file (0 .. 20000) { open( my $in, '<', '/var/www/data/' . $file . '.txt' ) or die "open failed: $!"; open( my $out, '>', '/var/www/sorted/' . $file . '.txt' ) or die "open failed: $!"; my %seen; while ( chomp( my $line = <$in> ) ) { for my $word ( split( /\W/, $line ) ) { $word = lc( $word ); ++$seen{$word} if ( exists( $banned{$word} ) ); } } my @frequent = ( sort { $seen{$b} <=> $seen{$a} } keys %seen )[0..50]; print $out join("\n", @frequent); close( $in ) or die "close failed: $!"; close( $out ) or die "close failed: $!"; }

Replies are listed 'Best First'.
Re^2: Removing common words
by Anonymous Monk on Apr 04, 2004 at 05:35 UTC

    Also, if any file has the potential of containing less than 50 unique words not contained within @banned, you will need to adjust the 0..50 splice to avoid undefined entries. Grepping for defined values will do it.

Re^2: Removing common words
by Anonymous Monk on Apr 04, 2004 at 05:28 UTC

    *Exasperated sigh*. I had to try it out. I got it right it seems all for the most important part. The one line should obviously read ++$seen{$word} unless ( exists( $banned{$word} ) );. Note the 'unless', not 'if'.