Guest User

Untitled

a guest
Oct 18th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.71 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. foreach $file (glob "poets/*.txt") {
  4.     $poet = $file;
  5.     $poet =~ s/_/ /g;
  6.     $poet =~ s/\.txt//g;
  7.     $poet =~ s/poets\///g;
  8.     ($freq, $total) = &getFreq($file, $ARGV[0]);
  9.     printf ("%4d/%6d = %.9f %s\n", $freq, $total, $freq/$total, $poet);
  10. }
  11.  
  12.  
  13. sub getFreq{
  14.     my ($fileName, $keyWord) = @_;
  15.     chomp $keyWord;
  16.     open (F, "$fileName") or return 0;
  17.     my $wordCount = 0;
  18.     my $totalWords = 0;
  19.     while (my $line = <F>){
  20.         chomp($line);
  21.         $line =~ tr/A-Z/a-z/;
  22.         $line =~ s/[^a-z]/ /g;
  23.         $line =~ s/ +/ /g;
  24.         my @words = split (' ', $line);
  25.         foreach $word (@words){
  26.             if ($word eq $keyWord){
  27.                 $wordCount ++;
  28.             }
  29.             $totalWords ++;
  30.         }
  31.     }
  32.     return ($wordCount, $totalWords);
  33.     close(F);
  34. }
Add Comment
Please, Sign In to add comment