Perl Keyword Density Checker

Not so much now, but keyword density used to be a very large factor in determining how well a website ranks. This perl script counts words and tells you what their percentage is:

Keyword Density Checker in Perl

use strict;
use warnings;
# Make a word frequency count
my %seen = ();
my $total = 0;
while (<DATA>) {
  while ( /(\w['\w-]*)/g ) {
    my $word = $1;
    unless (($word eq 'h3') or ($word eq 'p') or ($word eq 'h2') or ($word eq 'li') or ($word eq 'ul') or ($word eq 'ol') or ($word eq 'a')) {
    $seen{lc $1}++;
# output hash in a descending numeric sort of its values
print "TOTAL WORDS: $total\n";
foreach my $word ( sort { $seen{$b} <=> $seen{$a} } keys %seen) {
  my $percent = $seen{$word}/$total;
  $percent = sprintf("%.2f", $percent);
  my $appears = $seen{$word};
  printf "\t$percent\t$appears\t$word\n";
save your text to test for keyword density below this line

2 replies on “Perl Keyword Density Checker”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.