In the template design of AnQi CMS,wordcountFilter is a utility for counting the number of words in text.For operators and content creators, understanding the working principle, especially the statistical logic when dealing with mixed Chinese and English text, can help us more accurately assess content length, optimize article structure, and better meet the needs of search engine optimization (SEO) and user reading experience.

wordcountBasic usage of the filter

wordcountThe filter is very straightforward to use. It applies to a string variable and returns the number of 'words' in the string. In the AnQiCMS template, its basic syntax is:

{{ 你的字符串变量 | wordcount }}

or, if you want to perform word count on a template content, you can also use the filter tag form:

{% filter wordcount %}
    这里是需要统计单词的文本内容。
{% endfilter %}

for example, if there is a string{{ "Hello AnQiCMS World" | wordcount }},它将返回3This is consistent with the word count we usually understand when processing pure English text.

Word count logic for mixed Chinese-English text

wordcountThe core statistical logic of the filter is how it “identifies” a word: it mainlydistinguishes words by spacesThis means that any continuous sequence of characters separated by spaces, regardless of whether they are English or Chinese, will be counted as a single 'word'.

Specifically, when dealing with mixed Chinese-English text:

  1. English words:English words are usually separated by spaces.wordcountAccurately counts each English word separated by spaces as an independent word.
  2. Chinese text:In Chinese writing habits, words are not separated by spaces. In this case,wordcountThe filter will take a segmentof continuous Chinese text without spacescounted as a single "word". For example, the whole paragraph "安企CMS is a content management system" in Chinese, if it is not separated by spaces as a whole, it will bewordcountFiltered statistics as1.
  3. Mixed Chinese and English text:When both English words and Chinese text appear in the text, the counting method is a combination of both.English word counting is based on spaces, while Chinese characters are counted as continuous, spaceless text blocks.

Let's understand this logic better through several examples:

  • Pure English example: {{ "AnQiCMS is a powerful CMS." | wordcount }}

    • Result:5(AnQiCMS, is, a, powerful, CMS.)
  • 纯中文示例: {{ "安企CMS是一个内容管理系统。" | wordcount }}

    • Result:1(Because the entire string does not contain spaces, it is regarded as a continuous "word block")
  • Mixed Chinese-English example: {{ "Hello AnQiCMS 用户,这是一个测试文章。" | wordcount }}

    • Result:5(Hello, AnQiCMS, User, This is a test article.)
    • Here is the logic: "Hello" (1 word) + "AnQiCMS" (1 word) + "user" (1 word) + "This is a test article" (1 word) = 4. If 'user' and 'this is a test article' are not separated by spaces, they will be treated as a whole.Actually, in Chinese text, 'user' and 'this is a test article' are each considered as a 'chunk' because there is a comma (non-space) between them, and the comma is also considered as one of the separators (or more accurately, the character sequence before and after the comma is considered as a chunk).{{ "Hello AnQiCMS user, this is a test article." | wordcount }}-> 9.
    • Rethink the document description “Words will be separated by spaces.”}]If it does not contain spaces, it is considered a word.This means that non-space characters will accumulate.For the user, this is a test article.,autoAccording to the literal meaning, the whole 'user, this is a test article.'” May be counted as 1 word.But in actual testing, commas may also be used as separators, and here a more precise definition is needed.However, the core is 'non-space continuous character block'.

    A more precise example of mixed text:

    • {{ "AnQiCMS 提供了丰富的功能。" | wordcount }}
      • “AnQiCMS” (1) + “Provided rich features.” (1) =2
    • {{ "GoLang 开发的 AnQiCMS,部署简单。" | wordcount }}
      • “GoLang” (1) + “development” (1) + “AnQiCMS” (1) + “deployment is simple.” (1) =4(Note that the Chinese comma is used herealso acts as a separator)

    According to the actual usage scenario and the definition of 'word', AnQiCMS'swordcountThe filter is more focused on statistical continuous non-blank character blocks when dealing with Chinese, rather than words in the sense of Chinese linguistics.

Application and Precautions in Practice

Understandwordcountworking principle, which can help us make better use of it:

  • English content evaluation:For pages mainly containing English (such as English websites, English versions of multilingual websites),wordcountThe filter can provide relatively accurate word statistics, which is helpful for content length planning and SEO keyword density control.
  • Chinese content evaluation:When evaluating content that is purely Chinese or mainly Chinese, please notewordcountThe result is not the precise number of Chinese words.It is more like counting the number of 'text blocks'.If you need an accurate Chinese word count, you may need to combine it with other tools or front-end JavaScript to implement tokenization processing.
  • Mixed content strategy:In content that is mixed in Chinese and English,wordcountProvide a comprehensive number of 'chunks', which is still valuable for rough estimation of content volume.
  • MatchinglengthFilter:To measure the length of content more comprehensively, consider using simultaneouslylengthThe filter counts the total number of characters (including Chinese, English, punctuation, and spaces). This provides a more direct concept of 'word count'.

Understanding these details can help us avoid misunderstandings.wordcountThe statistical results are applied to the content management and operation strategy of AnQiCMS, thus producing higher quality content that meets the expected standards.


Common Questions (FAQ)

1. Why does my Chinese article usewordcountThe result of filter statistics is always 1 or very few?This is becausewordcountThe filter identifies words mainly through spaces. Chinese text usually does not use spaces to separate words, so a continuous block of Chinese text, even if it contains many words, will bewordcountCounted as a 'word block'.

2. Does AnQiCMS provide a more accurate Chinese word count feature?According to the existing document,wordcountFilter is separated by spaces and some punctuation marks.If you need to count the number of words in Chinese linguistics (for example, recognizing 'Content Management System' as four words), AnQiCMS's template filter does not provide such advanced segmentation functions directly.This usually requires the use of an external Chinese tokenizer library or processing through front-end JavaScript.

3. BesideswordcountWhat filters can be used to measure the length of content?You can uselengthFilter to count the total number of characters in a text. For example,{{ 你的字符串变量 | length }}Returns the total count of all characters (including Chinese, English, numbers, punctuation, and spaces) in a string. This is a more direct indicator for measuring the 'word count' of an article.