Call: (480) 382-8464

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


#!/usr/bin/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}++;
    $total++;
    }
  }
}

# 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";
}

__DATA__
save your text to test for keyword density below this line

Leave a Reply

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

Main Offers/Services

Hosting and website related services offered by Digital Crunch:



Managed VPS Hosting

Server Updates, Module/Software Installs, Firewall, Security, Hosting Management, Wordpress - let us manage it for you

Hosted Email

Setup and Maintain Business Class, Secure Email Hosting including spam protection and 25GB of storage space

Linux Consulting

Server Updates, Module/Software Installs, Firewall, Security, Hosting, Wordpress and Apps, anything related to Linux

Email Marketing Services

We broadcast your content to your list, capture leads for you, follow up on schedules, make more sales for you

Website Maintenance

Content updates, menu changes, graphics changes, adding content, getting links, building traffic, building sales funnels

Hosting Tutorials

Tutorials we've written for other customers that like to get their hands dirty and learn about hosting