File size: 1,368 Bytes
ad01ed3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python3
"""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  # per language
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}")