Analyze Keyword Frequency and Density in Text
Analyze keyword frequency and density in any text. Learn how to extract word frequencies, calculate keyword density percentages, and identify the most common terms for SEO and content optimization.
Detailed Explanation
Keyword Frequency Analysis
Keyword frequency analysis counts how often each word or phrase appears in a text. It is a foundational technique in SEO, content optimization, and natural language processing (NLP).
Basic Word Frequency
The simplest frequency counter tokenizes text and tallies occurrences:
function getWordFrequency(text) {
const words = text.toLowerCase().split(/\s+/);
const freq = {};
for (const word of words) {
const clean = word.replace(/[^a-z0-9'-]/g, "");
if (clean) freq[clean] = (freq[clean] || 0) + 1;
}
return freq;
}
This converts to lowercase for case-insensitive comparison, strips punctuation (keeping hyphens and apostrophes for compound words and contractions), and builds a frequency map.
Keyword Density
Keyword density is the percentage of total words that a specific keyword represents:
density = (keyword_count / total_words) * 100
For SEO, target keyword density typically ranges from 1% to 3%. Below 1% may signal under-optimization; above 3% risks being flagged as keyword stuffing.
N-gram Analysis
Single-word frequency misses multi-word phrases. N-gram analysis extracts sequences of N consecutive words:
function getNgrams(words, n) {
const ngrams = {};
for (let i = 0; i <= words.length - n; i++) {
const gram = words.slice(i, i + n).join(" ");
ngrams[gram] = (ngrams[gram] || 0) + 1;
}
return ngrams;
}
Bigrams (n=2) catch phrases like "machine learning" or "best practices". Trigrams (n=3) find longer phrases like "search engine optimization".
Stop Word Filtering
High-frequency words like "the", "is", "and" dominate raw frequency counts but carry little meaning. Filtering stop words reveals the actual content keywords.
TF-IDF: Beyond Raw Frequency
For comparing keyword importance across documents, TF-IDF (Term Frequency-Inverse Document Frequency) weighs a word's frequency in one document against how common it is across all documents. Words that appear frequently in one document but rarely in others score highest.
Use Case
SEO professionals use keyword frequency analysis to optimize content for target search terms, ensuring proper keyword density without stuffing. Content marketers identify overused words to improve variety, and academic researchers analyze text corpora to discover dominant themes and terminology patterns.