Son aktivite 1 month ago

add_spaces.py Ham
1import os
2import re
3import sys
4
5# Add spaces between Chinese and English
6def add_spaces_between_chinese_and_english(directory):
7 md_files = [f for f in os.listdir(directory) if f.endswith((".md", ".mdx"))]
8 pattern = re.compile(r'([\u4e00-\u9fff])([a-zA-Z0-9])|([a-zA-Z0-9])([\u4e00-\u9fff])')
9
10 for md_file in md_files:
11 md_path = os.path.join(directory, md_file)
12 with open(md_path, "r", encoding="utf-8") as file:
13 content = file.read()
14
15 new_content = pattern.sub(lambda m: f'{m.group(1) or m.group(3)} {m.group(2) or m.group(4)}', content)
16
17 with open(md_path, "w", encoding="utf-8") as file:
18 file.write(new_content)
19
20 print(f"Processed: {md_file}")
21
22if __name__ == "__main__":
23 if len(sys.argv) != 2:
24 print("Usage: python script.py <directory>")
25 sys.exit(1)
26
27 directory = sys.argv[1]
28 if not os.path.isdir(directory):
29 print("Error: Directory does not exist.")
30 sys.exit(1)
31
32 add_spaces_between_chinese_and_english(directory)