chinese-markdown / build.py
罗杰斯
Upload build.py
32825f2 verified
raw
history blame
1.35 kB
import json
from pydantic import BaseModel
class Sample(BaseModel):
code: str
size: int
license: str
def is_chinese_char(char):
"""Check if the char is a Chinese character."""
return char >= "\u4e00" and char <= "\u9fff"
def is_chinese(text: str):
"""Check if the text is Chinese. We consider a text as Chinese if >20% of
the text is Chinese characters."""
cn_count = 0
for char in text:
if is_chinese_char(char):
cn_count += 1
return cn_count * 5 > len(text)
with open("train.jsonl", "wb") as cn:
with open("/Users/diegorojas/Desktop/markdown.jsonl", "r") as samples:
for sample in samples:
try:
data = json.loads(sample)
except json.JSONDecodeError as e:
print(f"Error: {e}")
continue
code, size, license = (
data["code"],
data["size"],
data["license"],
)
if license not in ["", "MIT", "Apache-2.0"]:
continue
encoded = Sample(
code=code,
size=size,
license=license,
).model_dump_json()
language = is_chinese(code)
if language == "cn":
cn.write(encoded.encode("utf-8") + b"\n")