38 lines
941 B
Python
38 lines
941 B
Python
# -*- coding: utf-8 -*-
|
|
# Final pass - fix hz function and expand all
|
|
import os, re
|
|
|
|
d = "/app/working/workspaces/default/novels/round3/01-越躺平越强"
|
|
|
|
# CORRECT regex - use raw string
|
|
r = re.compile(r'[\u4e00-\u9fff]')
|
|
|
|
def hz(s):
|
|
return len(r.findall(s))
|
|
|
|
def read(fname):
|
|
path = os.path.join(d, fname)
|
|
with open(path, 'r', encoding='utf-8') as f:
|
|
return f.read()
|
|
|
|
def write(fname, content):
|
|
path = os.path.join(d, fname)
|
|
with open(path, 'w', encoding='utf-8') as f:
|
|
f.write(content)
|
|
|
|
def expand(fname, text):
|
|
c = read(fname)
|
|
cn = hz(c)
|
|
write(fname, c + text)
|
|
cn_new = hz(read(fname))
|
|
print(f" {fname}: {cn} -> {cn_new}汉字")
|
|
return cn_new
|
|
|
|
# First, check all chapters
|
|
print("当前状态:")
|
|
for f in sorted(os.listdir(d)):
|
|
if f.endswith('.md') and f.startswith('第'):
|
|
cn = hz(read(f))
|
|
print(f" {f}: {cn}汉字")
|
|
|
|
print("\n开始修复和扩写...") |