Wednesday, April 13, 2011

How to Count Word Frequency in a String Using Perl

use strict;
use warnings;

# 1. Define the input string
my $sentence = "The cat that saw the black cat is a calico.";

# 2. Clean the string by removing common punctuation marks globally (/g)
$sentence =~ s/[.,?;:'"(){}\[\]]//g; 

# 3. Split the cleaned sentence by spaces into an array of individual words
my @words = split(/ /, $sentence);

# 4. Initialize an empty hash to keep track of word counts
my %counts;

# 5. Iterate through the array to populate the hash
foreach my $word (@words) {
    # lc() converts the word to lowercase so "The" and "the" count as the same word.
    # We then increment the count for that specific word in the hash.
    $counts{lc($word)}++;
}

# 6. Iterate over the unique keys in the hash to print the final tallies
foreach my $word (keys %counts) {
   print "$word = $counts{$word}\n";
}

No comments: