From 76c9518d2f66aa101cc880204d27cfc749d23480 Mon Sep 17 00:00:00 2001 From: zhiyu1998 <542716863@qq.com> Date: Thu, 30 May 2024 16:35:30 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix:=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E5=AD=97=E6=95=B0=E4=B8=BA=E7=BB=9F=E8=AE=A1?= =?UTF-8?q?=E4=B8=AD=E6=96=87=E5=AD=97=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/common.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/utils/common.js b/utils/common.js index e37008f..dde259f 100644 --- a/utils/common.js +++ b/utils/common.js @@ -380,13 +380,15 @@ export async function retryAxiosReq(requestFunction, retries = 3, delay = 1000) } /** - * 统计给定文本中的字数 + * 统计给定文本中的中文字数 * * @param {string} text - The text to count words in * @return {number} The number of words in the text */ -export function countWords(text) { - return text.split(/\s+/).filter(word => word.length > 0).length; +export function countChineseCharacters(text) { + const chineseCharacterRegex = /[\u4e00-\u9fa5]/g; + const matches = text.match(chineseCharacterRegex); + return matches ? matches.length : 0; } /** @@ -397,7 +399,7 @@ export function countWords(text) { * @return {Object} An object containing the estimated reading time in minutes and the word count. */ export function estimateReadingTime(text, wpm = 200) { - const wordCount = countWords(text); + const wordCount = countChineseCharacters(text); const readingTimeMinutes = wordCount / wpm; return { minutes: Math.ceil(readingTimeMinutes),