In the practice of AnQiCMS content management, we often need to perform refined analysis and optimization of website content.Among them, calculating the number of times a specific keyword appears in an article, title, or dataset is a fundamental and important requirement.Whether it is to evaluate SEO keyword density, analyze content quality, or conduct data statistics, AnQiCMS provides convenient and efficient solutions.This article will delve into how to take advantage of the built-in functions of AnQiCMS to easily achieve this goal.

Core Tools:countFilter Parsing

AnQiCMS's template engine provides a namedcountA practical filter, which is the core tool we use to solve the problem of keyword frequency counting.countThe filter design is simple and can efficiently count the frequency of a specified keyword in a string or array (slice/array).

Its basic usage is very intuitive:

{{ obj|count:关键词 }}

Here are theobjRepresents the string or array you want to count.关键词It is the text you want to count the occurrences of.

Count keywords in a string

When we want to count the number of times a keyword appears in a text segment, we can directly pass the text variable tocountNote that,countThe filter performs matching on strings usingexact word matchinglogic. This means it looks for substrings that are exactly the same as the keywords you provide.

For example, if you have a text 'Welcome to AnQiCMS (AnQiCMS), AnQiCMS is a good helper for your content management.', and you want to count the number of occurrences of the keyword 'CMS':

{% set myString = "欢迎使用安企CMS(AnQiCMS),AnQiCMS是您内容管理的好帮手。" %}
{{ myString|count:"CMS" }}

This code will return2because it found two completely matching "CMS" places.

Counting keywords in the array

countThe filter also applies to array type data. In an array, it will iterate over each element and check if the element is exactly equal to the keyword you providecompletely equal.

Assume we have an array composed of string elements, such assplitorfieldsa list of words split from a piece of text by a

{% set sentence = "AnQiCMS provides efficient and customizable content management solutions." %}
{% set wordsArray = sentence|fields %} {# fields 过滤器将字符串按空格拆分成数组 #}
{{ wordsArray|count:"content" }}

Here,wordsArrayfilterfieldsprocessed by the filter["AnQiCMS", "provides", "efficient", "and", "customizable", "content", "management", "solutions."]. Then,{{ wordsArray|count:"content" }}it will return1because it found an array element that matches "content" completely.

It should be emphasized particularly that the array counting is still full-word matching. If there is 'AnQiCMS' in the array and you try to count 'AnQi', the result will be0Because 'AnQi' does not appear as an independent element.

Actual application scenario: Make data analysis more accurate.

UnderstoodcountAfter learning the basic usage of the filter, let's take a look at its specific application in AnQiCMS template development and content management.

  1. Keyword density analysis of article contentOn the article detail page, we often need to evaluate the keyword density of the article content, which is crucial for SEO optimization.We can obtain the main content of the article and then count the number of occurrences of a specific keyword.

    Firstly,archiveDetailLabel fetching article content:

    {% archiveDetail articleContent with name="Content" %}
    

    Then, you can use:countStatistics with the filter:

    {% set keywordToCount = "AnQiCMS" %} {# 你想统计的关键词 #}
    <p>关键词 "{{ keywordToCount }}" 在文章中出现了 {{ articleContent|count:keywordToCount }} 次。</p>
    
  2. Title and description keyword checkTo ensure that the website's title (Title) and description (Description) comply with SEO strategy, we can quickly check the presence of specific keywords within them.

    {% archiveDetail articleTitle with name="Title" %}
    {% archiveDetail articleDescription with name="Description" %}
    {% set seoKeyword = "内容管理" %} {# 假设要检查的SEO关键词 #}
    
    
    <p>标题中 "{{ seoKeyword }}" 出现次数:{{ articleTitle|count:seoKeyword }}</p>
    <p>描述中 "{{ seoKeyword }}" 出现次数:{{ articleDescription|count:seoKeyword }}</p>
    
  3. Count of specific identifiers in the list dataIn certain specific scenarios, for example, if you have an array composed of specific identifiers (such as tag ID lists, product attribute code lists), and you need to count the occurrences of a specific identifier.countThe filter can also play a role.

    For example, if you have an array namedproductTagsthat contains multiple product tag names:

    {% set productTags = ["电子产品", "智能家居", "手机", "电子产品", "配件"]|list %} {# 假设这是一个动态生成的标签数组 #}
    <p>“电子产品”标签出现的次数:{{ productTags|count:"电子产品" }}</p>
    

Further reading: Related search and processing techniques

ExceptcountFilter, AnQiCMS also provides other practical filters related to keyword search and string processing, which can be used withcountUse in conjunction, or as an alternative solution in different scenarios.

  • containFilterIf you only need to know whether a string or an array contains a specific keyword, without caring about how many times it appears, containThe filter will be more concise and efficient, it will return a boolean value (True or False).
  • indexFilterIf you want to know not only if a keyword exists but also its first occurrence position (in a string or an array index),indexthe filter will be your choice.
  • splitandfieldsFilterThese two filters can split strings by a specified delimiter (split) or by spaces (fieldsSplit into an array. In some cases where more granular statistics of word occurrence is needed (especially for non-Chinese words), you can first use them to split the text into a word array, and then combinecountFilter statistics.

Summary

AnQiCMS, with its flexible template tags and rich built-in filters, provides powerful data processing capabilities for website content operators.countThe filter acts as an efficient keyword statistics tool, helping us to deeply analyze content, optimize SEO strategies, and better understand and manage website content.Mastering these tools will make your content operation work more at your fingertips.


Common Questions (FAQ)

  1. countDoes the filter support fuzzy matching or partial matching? countThe filter uses strict counting when counting strings and arraysexact word matching(or full element match). It does not perform fuzzy matching or partial matching automatically. For example, if you count "AnQiCMS", and the text only has "AnQi",countwill return 0. If fuzzy matching is needed, it may be necessary to combine with other methods, such as first usingsplitSplit text into words, then traverse the array for regular expression matching or partial inclusion judgment, but the AnQiCMS template itself does not support direct regular expression matching.

  2. How to count the occurrences of multiple different keywords in a paragraph of text?To count the occurrences of multiple keywords, you need to use each keyword individuallycountFilter.For example, to count the occurrences of 'AnQiCMS' and 'content management' in a text, you can do this: twig {% set text = “AnQiCMS is a powerful content management system, AnQiCMS helps you manage content efficiently.”