In the content management process of AnQiCMS, we often need to count the number of words in articles in order to better plan content, estimate reading time, or optimize SEO.wordcountThe filter has become a very practical tool. However, many users may be curious about when the text contains some consecutive non-space character sequences, such as a complete URL link, an email address, or a hyphenated term.wordcountHow will the filter identify and count them? Will it treat them as a separate 'word' like it does with ordinary words?
After processing AnQiCMSwordcountThe in-depth understanding of the filter, we can clearly say, its working principle is mainly through identifying the text inspacesTo distinguish and count the "word".This means that any continuous sequence of non-space characters, regardless of whether it contains letters, numbers, or special symbols (such as slashes or dots in URLs, or the @ symbol in email addresses), will be counted as a single 'word'.
For example,https://en.anqicms.comSuch a complete URL, although it contains multiple letters, slashes, and dots, but because there are no spaces between them,wordcountthe filter will treat it asAna word. Similarly,[email protected]such an email address, as well asGo-Langwords connected with hyphens, inwordcountthe eyes of a filter, are alsoAnwords.
This counting method brings many conveniences in practical applications.Firstly, it makes the estimation of content length more intuitive and in line with expectations.For example, when writing articles that contain a large number of external references or technical terms, we do not have to worry that URLs or complex names are incorrectly split into multiple words, which could lead to distorted statistical results.wordcountThe filter can provide a relatively unified and reliable measurement standard.It avoids complex semantic analysis of words and focuses on their textual form, ensuring the conciseness and efficiency of the statistical results.
Used in the templatewordcountThe filter is also very simple, and it has two common usages:
Used as an inline filter:When you need to quickly count the number of words in a variable, you can simply add a pipe character after the variable
|andwordcount.{{ my_content | wordcount }}Used as a block-level filter:When you need to count the number of text words in a template code block, you can use
filterLabel.{% filter wordcount %} 这里是需要统计单词数量的一段文本,其中包含一个URL:https://anqicms.com/docs 和一个邮箱地址:[email protected]。 {% endfilter %}
Let's demonstrate this counting mechanism with several specific examples:
We have the following text segments:
"安企CMS 是一个强大的内容管理系统。""这是一个包含URL的句子:https://en.anqicms.com 和一个电子邮件地址:[email protected]。""AnQiCMS 是一个基于 Go-Lang 开发的系统。"""(Empty string)
UsewordcountThe filter statistics will be:
- For text 1:
{{ "安企CMS 是一个强大的内容管理系统。" | wordcount }}, the result is:7(English CMS, is, a, powerful, content, management, system, .). - For text2:
{{ "这是一个包含URL的句子:https://en.anqicms.com 和一个电子邮件地址:[email protected]。" | wordcount }}, the result is:10(This, is, a, sentence, containing, a, URL:,,)EnglishAnd, a, email address: [email protected]. ) - For text 3:
{{ "AnQiCMS 是一个基于 Go-Lang 开发的系统。" | wordcount }}, the result is:8(AnQiCMS, is, a, based, on, Go-Lang, developed, system, .). - For text 4:
{{ "" | wordcount }}, the result is:0.
From these examples, it can be seen that,wordcountThe filter provides a concise logic, offering AnQiCMS users an efficient and easy-to-understand word counting method, especially suitable for processing modern web content containing URLs, email addresses, and other continuous characters.
Common Questions (FAQ)
Q1: If my text contains words with many hyphens (such as 'all-in-one'), are they considered as a single word?A1: Yes, if there are no spaces between the characters on either side of the hyphen,wordcountthe filter will consider it as a continuous character sequence and count it asAnWords. For example, 'all-in-one' is counted as a single word.
Q2:wordcountDoes the filter distinguish between uppercase and lowercase?A2:wordcountThe filter does not distinguish between uppercase and lowercase when counting word occurrences. It only cares about whether the character sequence is separated by spaces, without analyzing the actual content or case of the words.
Q3:wordcountFilter can count Chinese characters?A3: Yes,wordcountThe filter can correctly count Chinese characters.Each Chinese word group or single Chinese character separated by space (if not separated by space, then a continuous sequence of Chinese characters) is considered a separate 'word' for counting.For example, 'Hello World' would be counted as two words.