Download scripts/locate_folded.py from bravesigma/quran-md-data: direct link, hf CLI and curl.
- Browser
- Download file 2.71 kB
-
https://huggingface.co/datasets/bravesigma/quran-md-data/resolve/main/scripts/locate_folded.py
- Command line
-
hf download hf://datasets/bravesigma/quran-md-data/scripts/locate_folded.py
-
curl -L -o locate_folded.py https://huggingface.co/datasets/bravesigma/quran-md-data/resolve/main/scripts/locate_folded.py
2.71 kB
| #!/usr/bin/env python3 | |
| """Locate a folded needle in a substrate file and print the exact raw span. | |
| Char-level fold mapping: for every folded character, remember the raw index | |
| it came from; then map the needle's folded span back to raw coordinates and | |
| print the verbatim raw substring (repr) for use as a citation quote_ar. | |
| """ | |
| import sys | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parent)) | |
| from fetch_majma_eshia import TASHKEEL, QMARKS, CTRL, BRACKETS # noqa: E402 | |
| def fold_with_map(s: str): | |
| """Fold s and return (folded, folded_index -> raw_index map).""" | |
| out = [] | |
| imap = [] # folded idx -> raw idx | |
| i = 0 | |
| n = len(s) | |
| last_was_space = True # leading strip | |
| while i < n: | |
| ch = s[i] | |
| if TASHKEEL.match(ch) or QMARKS.match(ch) or CTRL.match(ch): | |
| i += 1 | |
| continue | |
| if ch in BRACKETS: | |
| if not last_was_space: | |
| out.append(" ") | |
| imap.append(i) | |
| last_was_space = True | |
| i += 1 | |
| continue | |
| if ch.isspace(): | |
| if not last_was_space: | |
| out.append(" ") | |
| imap.append(i) | |
| last_was_space = True | |
| i += 1 | |
| continue | |
| c = ch | |
| if c in ("أ", "إ", "آ", "ٱ", "ا"): | |
| c = "ا" | |
| elif c == "ى": | |
| c = "ي" | |
| elif c == "ة": | |
| c = "ه" | |
| out.append(c) | |
| imap.append(i) | |
| last_was_space = False | |
| i += 1 | |
| while out and out[-1] == " ": | |
| out.pop() | |
| imap.pop() | |
| return "".join(out), imap | |
| def main(): | |
| path = Path(sys.argv[1]) | |
| needle = sys.argv[2] | |
| pad_before = int(sys.argv[3]) if len(sys.argv) > 3 else 0 | |
| pad_after = int(sys.argv[4]) if len(sys.argv) > 4 else 0 | |
| raw = path.read_text(encoding="utf-8") | |
| folded, imap = fold_with_map(raw) | |
| nf = fold_with_map(needle)[0] | |
| j = folded.find(nf) | |
| if j < 0: | |
| print("NOT FOUND in folded text") | |
| sys.exit(1) | |
| lo = imap[j] | |
| hi = imap[j + len(nf) - 1] + 1 | |
| # extend to word boundaries | |
| while lo > 0 and not raw[lo - 1].isspace(): | |
| lo -= 1 | |
| while hi < len(raw) and not raw[hi].isspace(): | |
| hi += 1 | |
| if pad_before: | |
| k = 0 | |
| while lo > 0 and k < pad_before: | |
| lo -= 1 | |
| k += 1 | |
| if pad_after: | |
| k = 0 | |
| while hi < len(raw) and k < pad_after: | |
| hi += 1 | |
| k += 1 | |
| span = raw[lo:hi] | |
| print("folded pos:", j, "raw span:", lo, hi) | |
| print("RAW SPAN repr:") | |
| print(repr(span)) | |
| print() | |
| print("verbatim substring check:", span in raw) | |
| if __name__ == "__main__": | |
| main() | |