INNER CODE UNIT · Python
count_chinese_words
voocel/ainovel-cli · scripts/check_chapter_wordcount.py:19
def count_chinese_words(text: str) -> int:
"""统计中文字数(排除标点符号和 Markdown 标记)"""
text = re.sub(r'#{1,6}\s*', '', text)
text = re.sub(r'\*\*(.*?)\*\*', r'\1', text)
text = re.sub(r'\*(.*?)\*', r'\1', text)
text = re.sub(r'~~(.*?)~~', r'\1', text)
text = re.sub(r'`(.*?)`', r'\1', text)
text = re.sub(r'\[(.*?)\]\(.*?\)', r'\1', text)
chinese_chars = re.findall(r'[\u4e00-\u9fff]', text)
return len(chinese_chars)
def extract_content_from_chapter(file_path: Path) -> str:
"""从章节文件中提取正文内容(排除标题等元数据)"""
content = file_path.read_text(encoding='utf-8')
lines = content.split('\n')