Count Occurrences
Counts the occurrences of specified keywords in a given text. This node is useful for verifying keyword density, checking if target keywords appear in content, and measuring keyword usage across documents.
Common Properties
- Name - The custom name of the node.
- Color - The custom color of the node.
- Delay Before (sec) - Waits in seconds before executing the node.
- Delay After (sec) - Waits in seconds after executing node.
- Continue On Error - Automation will continue regardless of any error. The default value is false.
If the ContinueOnError property is true, no error is caught when the project is executed, even if a Catch node is used.
Inputs
- Text - Text content to search for keyword occurrences. Can be a string containing article text, web page content, or any text data.
- Keywords - Array of keywords to count in the given text. Example:
["seo", "optimization", "content marketing"]
Output
- Counts - An object containing keyword counts where keys are the keywords and values are their occurrence counts. Example:
{
"seo": 5,
"optimization": 3,
"content marketing": 2
}
How It Works
The Count Occurrences node performs simple but effective keyword counting:
- Preprocesses the input text by converting to lowercase and removing punctuation
- Normalizes each keyword by converting to lowercase
- Counts exact occurrences of each keyword in the processed text
- Returns a dictionary mapping each keyword to its count
The node uses case-insensitive matching and removes punctuation to ensure accurate counting even when keywords appear with different capitalization or adjacent to punctuation marks.
Practical Examples
Example 1: SEO Content Verification
Check if target keywords appear in your content with sufficient frequency:
// Get content from a blog post
msg.text = msg.blogPostContent;
// Define target keywords
msg.keywords = ["SEO optimization", "keyword research", "content strategy"];
// After Count Occurrences node
const counts = msg.counts;
// Check if keywords meet minimum threshold
const minOccurrences = 3;
for (let keyword in counts) {
if (counts[keyword] < minOccurrences) {
console.log(`Warning: "${keyword}" appears only ${counts[keyword]} times`);
} else {
console.log(`✓ "${keyword}" appears ${counts[keyword]} times`);
}
}
Example 2: Competitor Analysis
Compare keyword usage between your content and competitors:
// Count in your content
msg.text = msg.yourContent;
msg.keywords = ["machine learning", "artificial intelligence", "deep learning", "neural networks"];
// After first Count Occurrences
const yourCounts = {...msg.counts};
// Count in competitor content
msg.text = msg.competitorContent;
// Keywords stay the same
// After second Count Occurrences
const competitorCounts = msg.counts;
// Compare
msg.comparison = {};
msg.keywords.forEach(keyword => {
msg.comparison[keyword] = {
yours: yourCounts[keyword],
competitor: competitorCounts[keyword],
difference: yourCounts[keyword] - competitorCounts[keyword]
};
});
Example 3: Multi-Page Keyword Tracking
Track keyword usage across multiple pages:
const pages = [
{ url: "page1.html", content: msg.page1Content },
{ url: "page2.html", content: msg.page2Content },
{ url: "page3.html", content: msg.page3Content }
];
const targetKeywords = ["product feature", "customer support", "free trial"];
msg.pageResults = [];
// Process each page in a loop
for (let page of pages) {
msg.text = page.content;
msg.keywords = targetKeywords;
// After Count Occurrences (in loop)
msg.pageResults.push({
url: page.url,
counts: {...msg.counts}
});
}
// Generate report
msg.pageResults.forEach(result => {
console.log(`\nPage: ${result.url}`);
for (let keyword in result.counts) {
console.log(` ${keyword}: ${result.counts[keyword]}`);
}
});
Example 4: Keyword Density Calculation
Calculate keyword density percentage for SEO analysis:
msg.text = msg.articleContent;
msg.keywords = ["content marketing", "digital strategy"];
// After Count Occurrences
const counts = msg.counts;
// Calculate total words in text
const totalWords = msg.text.split(/\s+/).length;
// Calculate density for each keyword
msg.densities = {};
for (let keyword in counts) {
const keywordWordCount = keyword.split(/\s+/).length;
const occurrences = counts[keyword];
const density = (occurrences * keywordWordCount / totalWords * 100).toFixed(2);
msg.densities[keyword] = {
count: occurrences,
density: `${density}%`
};
}
console.log("Keyword Densities:", msg.densities);
Example 5: Content Quality Check
Verify that important terms appear in different sections of content:
// Extract sections from HTML or markdown
msg.sections = {
title: msg.pageTitle,
headings: msg.h2Headings.join(" "),
introduction: msg.firstParagraph,
body: msg.mainContent
};
msg.keywords = ["data analysis", "business intelligence"];
msg.sectionAnalysis = {};
// Count in each section
for (let section in msg.sections) {
msg.text = msg.sections[section];
// After Count Occurrences
msg.sectionAnalysis[section] = {...msg.counts};
}
// Check coverage
msg.keywords.forEach(keyword => {
const appearances = Object.keys(msg.sectionAnalysis).filter(
section => msg.sectionAnalysis[section][keyword] > 0
);
console.log(`"${keyword}" appears in: ${appearances.join(", ")}`);
});
Tips for Effective Use
-
Text Preparation
- The node automatically removes punctuation and converts to lowercase
- No need to pre-clean your text
- However, removing HTML tags beforehand gives more accurate word counts
-
Keyword Selection
- Use exact phrases as they should appear in content
- Case doesn't matter - "SEO" and "seo" will match the same occurrences
- Include variations separately if needed (e.g., "optimize" and "optimization")
-
Performance
- Very fast even for large texts
- Can process thousands of keywords efficiently
- Consider using loops for multiple documents
-
Result Interpretation
- Zero count means keyword doesn't appear in text at all
- Optimal keyword density for SEO is typically 1-2%
- Context matters - some keywords naturally appear more frequently
-
Integration with Other Nodes
- Use after web scraping to analyze competitor content
- Combine with Gap Analysis to compare keyword usage
- Feed results to conditional nodes for quality checks
Common Errors and Solutions
Issue: Keywords Not Being Counted
Cause: Keywords might contain special characters or formatting that doesn't match the text.
Solution:
- Ensure keywords are plain text without HTML tags
- Check for hidden characters or extra whitespace
- Verify the keyword actually exists in the text
Issue: Count Lower Than Expected
Cause: Keyword appears in different forms (e.g., "optimize" vs "optimization").
Solution:
- Count each variation separately:
["optimize", "optimizes", "optimization"] - Or use Normalize Text node before counting to lemmatize/stem words to their base form
Issue: Phrase Keywords Not Matching
Cause: Text might have extra spaces or line breaks within the phrase.
Solution:
- Normalize whitespace in source text before counting
- Use a Code node to clean text:
msg.text = msg.text.replace(/\s+/g, ' ')
Example: Handling Word Variations
// Instead of counting variations separately
msg.keywords = ["optimize", "optimizes", "optimizing", "optimization"];
// Better: Use Normalize Text (lemmatization) first
// Normalize Text node with operation: Lemmatization
// This converts all variations to base form "optimize"
msg.text = msg.normalizedText; // After normalization
msg.keywords = ["optimize"];
Limitations
-
Exact Matching Only
- Counts exact keyword matches after lowercasing
- Doesn't handle synonyms (use separate keywords for synonyms)
- Doesn't account for word variations (combine with Normalize Text for this)
-
No Context Awareness
- Counts all occurrences regardless of context
- Can't distinguish between different meanings of the same word
- Doesn't understand negations (e.g., "not good" still counts "good")
-
Punctuation Handling
- Punctuation is removed, so "SEO-friendly" becomes "SEO friendly"
- This may cause unexpected matches or misses for hyphenated terms
Performance Considerations
- Very efficient for real-time processing
- Can handle texts up to several MB in size
- Keyword count doesn't significantly impact performance (tested with 1000+ keywords)
- Consider batching if processing hundreds of documents
Related Nodes
- Gap Analysis - Compare keyword counts between multiple texts
- TF-IDF Analysis - More sophisticated keyword extraction
- Frequency Analysis - Extract keywords with their counts from documents
- Normalize Text - Preprocess text to handle word variations