| |
| """Fetch real XNLI validation text per language -> tests/data/xnli/<lang>.txt (premise+hypothesis). |
| Uses the HF datasets-server rows API (no auth needed for public datasets).""" |
| import urllib.request, urllib.parse, json, os |
|
|
| LANGS = ["ar","bg","de","el","en","es","fr","hi","ru","sw","th","tr","ur","vi","zh"] |
| ROWS = 400 |
| here = os.path.dirname(os.path.abspath(__file__)) |
| out = os.path.join(here, "xnli") |
| os.makedirs(out, exist_ok=True) |
|
|
| def fetch(lang): |
| q = urllib.parse.urlencode({"dataset":"facebook/xnli","config":lang, |
| "split":"validation","offset":0,"length":ROWS}) |
| url = "https://datasets-server.huggingface.co/rows?" + q |
| req = urllib.request.Request(url, headers={"User-Agent":"fast_split-parity/0.1"}) |
| data = json.load(urllib.request.urlopen(req, timeout=60)) |
| lines = [] |
| for r in data["rows"]: |
| row = r["row"] |
| for k in ("premise","hypothesis"): |
| v = row.get(k) |
| if isinstance(v, str) and v: |
| lines.append(v) |
| return "\n".join(lines) |
|
|
| for lang in LANGS: |
| try: |
| t = fetch(lang) |
| open(os.path.join(out, f"{lang}.txt"), "w", encoding="utf-8").write(t) |
| print(f"{lang}: {len(t.encode())} bytes, {t.count(chr(10))+1} segments") |
| except Exception as e: |
| print(f"{lang}: FAILED {e}") |
|
|