Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- foreach $file (glob "poets/*.txt") {
- $poet = $file;
- $poet =~ s/_/ /g;
- $poet =~ s/\.txt//g;
- $poet =~ s/poets\///g;
- ($freq, $total) = &getFreq($file, $ARGV[0]);
- printf ("%4d/%6d = %.9f %s\n", $freq, $total, $freq/$total, $poet);
- }
- sub getFreq{
- my ($fileName, $keyWord) = @_;
- chomp $keyWord;
- open (F, "$fileName") or return 0;
- my $wordCount = 0;
- my $totalWords = 0;
- while (my $line = <F>){
- chomp($line);
- $line =~ tr/A-Z/a-z/;
- $line =~ s/[^a-z]/ /g;
- $line =~ s/ +/ /g;
- my @words = split (' ', $line);
- foreach $word (@words){
- if ($word eq $keyWord){
- $wordCount ++;
- }
- $totalWords ++;
- }
- }
- return ($wordCount, $totalWords);
- close(F);
- }
Add Comment
Please, Sign In to add comment