Spaces:
Running
Running
Download src/chunker.py from ngocdang83/HachimiMT-demo: direct link, hf CLI and curl.
- Browser
- Download file 777 Bytes
-
https://huggingface.co/spaces/ngocdang83/HachimiMT-demo/resolve/a9e1fba3326dfe8e57aaf9f9a8d2ef4df697cc06/src/chunker.py
- Command line
-
hf download hf://spaces/ngocdang83/HachimiMT-demo@a9e1fba3326dfe8e57aaf9f9a8d2ef4df697cc06/src/chunker.py
-
curl -L -o chunker.py https://huggingface.co/spaces/ngocdang83/HachimiMT-demo/resolve/a9e1fba3326dfe8e57aaf9f9a8d2ef4df697cc06/src/chunker.py
777 Bytes
| """Split Chinese source text into translation chunks.""" | |
| from __future__ import annotations | |
| import re | |
| SENTENCE_END = re.compile(r"(?<=[。!?!?;;…])") | |
| PARAGRAPH_BREAK = re.compile(r"\n\s*\n+") | |
| def split_chunks(text: str, mode: str = "sentence") -> list[str]: | |
| """Split *text* into non-empty chunks for independent translation.""" | |
| text = text.strip() | |
| if not text: | |
| return [] | |
| if mode == "paragraph": | |
| parts = PARAGRAPH_BREAK.split(text) | |
| else: | |
| parts: list[str] = [] | |
| for paragraph in text.splitlines(): | |
| paragraph = paragraph.strip() | |
| if not paragraph: | |
| continue | |
| parts.extend(SENTENCE_END.split(paragraph)) | |
| return [chunk.strip() for chunk in parts if chunk.strip()] |