-
Notifications
You must be signed in to change notification settings - Fork 34
/
data.py
55 lines (46 loc) · 1.92 KB
/
data.py
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
from collections import Counter
from torch.utils import data
from typing import Dict, Tuple
Mapping = Dict[str, int]
def create_mappings(dataset_path: str) -> Tuple[Mapping, Mapping]:
"""Creates separate mappings to indices for entities and relations."""
# counters to have entities/relations sorted from most frequent
entity_counter = Counter()
relation_counter = Counter()
with open(dataset_path, "r") as f:
for line in f:
# -1 to remove newline sign
head, relation, tail = line[:-1].split("\t")
entity_counter.update([head, tail])
relation_counter.update([relation])
entity2id = {}
relation2id = {}
for idx, (mid, _) in enumerate(entity_counter.most_common()):
entity2id[mid] = idx
for idx, (relation, _) in enumerate(relation_counter.most_common()):
relation2id[relation] = idx
return entity2id, relation2id
class FB15KDataset(data.Dataset):
"""Dataset implementation for handling FB15K and FB15K-237."""
def __init__(self, data_path: str, entity2id: Mapping, relation2id: Mapping):
self.entity2id = entity2id
self.relation2id = relation2id
with open(data_path, "r") as f:
# data in tuples (head, relation, tail)
self.data = [line[:-1].split("\t") for line in f]
def __len__(self):
"""Denotes the total number of samples."""
return len(self.data)
def __getitem__(self, index):
"""Returns (head id, relation id, tail id)."""
head, relation, tail = self.data[index]
head_id = self._to_idx(head, self.entity2id)
relation_id = self._to_idx(relation, self.relation2id)
tail_id = self._to_idx(tail, self.entity2id)
return head_id, relation_id, tail_id
@staticmethod
def _to_idx(key: str, mapping: Mapping) -> int:
try:
return mapping[key]
except KeyError:
return len(mapping)