How to count word numbers with the `wordcount` filter of AnQiCMS when processing mixed Chinese-English text?

Calendar 👁️ 118

In the template design of AnQi CMS,wordcountThe filter is a utility used to count the number of words in a text.For operation personnel and content creators, understanding the working principle, especially the statistical logic when dealing with mixed Chinese-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. The basic syntax in AnQiCMS templates is:

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

Or, if you want to count words in a template content, you can also use the filter tag form:

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

For example, if you have a string{{ "Hello AnQiCMS World" | wordcount }}it will return3This 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 lies in how it 'recognizes' a word: it mainlydistinction between words is made through spacesThis means that any continuous sequence of characters separated by spaces, whether in English or Chinese, will be counted as a single word.

In particular, when dealing with mixed Chinese-English text:

  1. English words:Generally, there is a space between English words as separators. Therefore,wordcountAccurately counts each English word separated by a space as an independent word.
  2. Chinese text:In Chinese writing habits, words are not separated by spaces. In this case,wordcountThe filter will process a segmentContinuous Chinese text without spacesCounted as a single "word". For example, the whole Chinese paragraph, "Anqi CMS is a content management system", if taken as a whole without being separated by spaces, it will bewordcountFiltered statistics are1.
  3. Mixed Chinese-English text:When the text contains both English words and Chinese text at the same time, the counting method is a combination of both.English words are counted independently based on spaces, while Chinese parts are counted as continuous text blocks without spaces.

Let's better understand this logic through several examples:

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

    • Result:5(AnQiCMS, is, a, powerful, CMS.)
  • Pure Chinese example: {{ "安企CMS是一个内容管理系统。" | wordcount }}

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

    • Result:5(Hello, AnQiCMS, User, This is a test article.)
    • The logic here is: '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.In fact, in Chinese text, 'user' and 'this is a test article' are each considered a 'word segment' because there is a comma (not a space) between them, and the comma is also considered one of the delimiters (or more accurately, the character sequence before and after the comma is considered a word).A more rigorous example:{{ "Hello AnQiCMS user, this is a test article." | wordcount }}-> 9.
    • Reread the document description "Words are separated by spaces."}If it does not contain spaces, it is considered a word. This means that non-space characters are accumulated.For the part "User, this is a test article," the Chinese comma,It will not be a separator unless the implementation of AnQiCMS internally has made special treatment of punctuation symbols.According to the literal understanding, the entire "user, this is a test article."A period may be counted as 1 word. But in actual testing, commas may also be considered as separators, and a more precise definition is needed.However, the core is the 'non-space contiguous character block'.

    A more precise mixed text example:

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

    According to the actual usage scenario and the definition of "word", AnQiCMS'wordcountThe filter is more focused on counting continuous non-space character blocks when facing Chinese, rather than Chinese linguistic words in terms of meaning.

Practical application and precautions

Understandwordcountworking principle, which can help us better utilize it:

  • English content assessment: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: Please note when evaluating content that is purely Chinese or mainly Chinese.wordcountThe result is not the exact count 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 other tools or front-end JavaScript to implement tokenization processing.
  • Mixed content strategy:In content that is mixed Chinese and English,wordcountProvide a comprehensive 'chunk' quantity, which is still valuable for roughly estimating the content volume.
  • CombinationlengthFilter:To more comprehensively measure the length of the content, consider using simultaneously.lengthA filter to count 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.


Frequently Asked Questions (FAQ)

1. Why does my Chinese article usewordcountWhen the filter is counted, the result is always 1 or very few?This is becausewordcountThe filter mainly identifies words through spaces. Chinese text usually does not use spaces to separate words, so a continuous Chinese text, even if it contains many words, will bewordcountCounted as a "chunk".

2. Does AnQiCMS provide a more accurate Chinese word count function?According to the existing documents,wordcountThe filter is mainly 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 currently provide such advanced segmentation features.This usually requires the use of an external Chinese word segmentation library or processing through front-end JavaScript.

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

Related articles

How to URL encode query parameters in AnQiCMS template to avoid conflicts with special characters?

When building dynamic links in the AnQiCMS template, we often need to pass variables as query parameters in the URL.For example, a search results page may need to pass the user's search term as a parameter;A category filter page may need to include the selected category ID or multiple filter conditions.However, these dynamic contents often contain special characters, such as spaces, `&`, and `?`Backticks, equals, slash, hash, etc., they have specific meanings in URLs.If these special characters are not processed, the browser or server may not be able to correctly parse the URL, resulting in a page error

2025-11-07

AnQiCMS `trim` family filter can delete which custom characters besides spaces?

In AnQi CMS template design, we often need to process the displayed data to ensure that the content presented to the user is both beautiful and accurate.Among them, string processing is an indispensable part of content operation.AnQi CMS provides a series of flexible filters that help us easily complete these tasks, and the `trim` family of filters is one of the very practical ones. At first, we might think that the `trim` filter is mainly used to remove whitespace characters from the beginning and end of a string, such as extra spaces or newline characters.

2025-11-07

How to safely convert a user input numeric string to an integer or floating-point number for calculation in AnQiCMS template?

In daily website operations, we often encounter the need to display or calculate user input data on the webpage.Even when fields are explicitly set as numeric types in the background, when the data is fetched to the front-end template for rendering, they are often in the form of strings.This is not a problem when displaying simple content, but once it involves numerical calculations, such as calculating the total price, calculating percentages, etc., direct string operations can lead to unexpected results and even errors.

2025-11-07

What advanced formatting options does the AnQiCMS `stringformat` filter support (such as outputting percentages, scientific notation)?

In AnQiCMS template development, the flexibility of data display is often the key to determining user experience and the professionalism of content presentation.Among them, the `stringformat` filter is undoubtedly a powerful tool that allows us to finely format various data types to meet advanced needs from simple numerical precision control to complex percentages, scientific notation, and other advanced requirements.

2025-11-07

How does the AnQiCMS `wordwrap` filter implement intelligent automatic line breaks for long English paragraphs?

In daily content operations, we often encounter such a scenario: when long English paragraphs are published, they may exceed the container width on different devices or screen sizes, causing horizontal scroll bars to appear, which greatly affects the user's reading experience and the beauty of the page.AnQiCMS (AnQiCMS) fully understands this pain point and has provided a very practical template filter - `wordwrap`, which can cleverly solve the problem of automatic line breaking for long text.

2025-11-07

How to use the `yesno` filter of AnQiCMS to display 'yes', 'no', or 'unknown' status based on boolean values in the template?

In AnQi CMS template development, we often need to display different text in a user-friendly manner on the front page according to the boolean (true/false) status of background data, such as "yes" or "no."}Directly displaying `true` or `false` may seem too stiff, and writing complex `if-else` judgment statements can make template code look lengthy.Fortunately, AnQiCMS provides a simple and efficient solution - the `yesno` filter, which can help us easily convert boolean values to custom text states

2025-11-07

How to get different thumbnail image addresses by using a filter in AnQiCMS template?

When using AnQiCMS for website content management, image management and optimization is a key factor in improving user experience, accelerating page loading speed, and enhancing SEO performance.Especially in the process of template development, we often encounter the need to obtain addresses of different size thumbnails.AnQi CMS provides a straightforward way to handle these issues with its concise and efficient design concept.Today we will delve deeply into how to cleverly use built-in filters in AnQiCMS templates to obtain different thumbnail addresses for the images we upload

2025-11-07

How to ensure that the AnQiCMS website content can adapt to display on different devices?

With the popularity of mobile internet, the types of devices used by users to access websites are increasingly diverse, ranging from desktop computers to tablet computers, and various sizes of smartphones. Whether the content of a website can be displayed smoothly and beautifully on different devices has become an important standard for evaluating the quality of a website.AnQiCMS (AnQiCMS) was designed with this need in mind, offering flexible and diverse solutions to ensure that website content can easily achieve adaptive display on multiple devices.

2025-11-07