-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatasets.py
More file actions
81 lines (73 loc) · 2.64 KB
/
Copy pathdatasets.py
File metadata and controls
81 lines (73 loc) · 2.64 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from __future__ import annotations
import os
import pathlib
import requests
from typing import Iterable, List, Tuple
SUPPORTED = {
"FB15k-237": {
"bases": [
"https://raw.githubusercontent.com/ibalazevic/Tucker/master/data/FB15k-237",
"https://raw.githubusercontent.com/TimDettmers/ConvE/master/FB15k-237",
],
"files": ["train.txt", "valid.txt", "test.txt"],
},
"WN18RR": {
"bases": [
"https://raw.githubusercontent.com/ibalazevic/Tucker/master/data/WN18RR",
"https://raw.githubusercontent.com/TimDettmers/ConvE/master/WN18RR",
],
"files": ["train.txt", "valid.txt", "test.txt"],
},
}
def download_dataset(name: str, out_dir: str = "data") -> str:
if name not in SUPPORTED:
raise ValueError(f"Unsupported dataset: {name}. Supported: {list(SUPPORTED)}")
bases = SUPPORTED[name]["bases"]
files = SUPPORTED[name]["files"]
ds_dir = os.path.join(out_dir, name)
os.makedirs(ds_dir, exist_ok=True)
for fn in files:
dest = os.path.join(ds_dir, fn)
if os.path.exists(dest) and os.path.getsize(dest) > 0:
continue
last_err: Exception | None = None
for base in bases:
url = f"{base}/{fn}"
try:
r = requests.get(url, timeout=60)
r.raise_for_status()
with open(dest, "wb") as f:
f.write(r.content)
last_err = None
break
except Exception as e:
last_err = e
if last_err is not None:
raise last_err
return ds_dir
def load_triples_from_dir(ds_dir: str, split: str = "train") -> List[Tuple[str, str, str]]:
path = os.path.join(ds_dir, f"{split}.txt")
if not os.path.exists(path):
raise FileNotFoundError(path)
triples: List[Tuple[str, str, str]] = []
with open(path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
parts = line.split()
if len(parts) != 3:
# Some datasets are tab-separated; try that
parts = line.split("\t")
if len(parts) != 3:
continue
h, r, t = parts
triples.append((h, r, t))
return triples
def all_triples(ds_dir: str) -> List[Tuple[str, str, str]]:
triples: List[Tuple[str, str, str]] = []
for split in ("train", "valid", "test"):
p = os.path.join(ds_dir, f"{split}.txt")
if os.path.exists(p):
triples.extend(load_triples_from_dir(ds_dir, split))
return triples