-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroc_stories.py
58 lines (52 loc) · 1.91 KB
/
roc_stories.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
56
57
58
import csv
from dataclasses import dataclass
from pathlib import Path
from typing import Optional
from torch.utils.data import Dataset
@dataclass
class Story():
story_id: str
sentences: list[str]
candidate_endings: list[str]
label: Optional[int] # index into candidate endings
@classmethod
def from_json(cls, data):
return cls(
story_id=data["InputStoryid"],
sentences=[data[f"InputSentence{n}"] for n in range(1, 5)],
candidate_endings=[data[f"RandomFifthSentenceQuiz{n}"] for n in range(1, 3)],
label=int(data["AnswerRightEnding"] or 0) - 1
)
class ROCStoriesDataset(Dataset):
def __init__(self, data_dir: str, split: str = "dev") -> None:
self.data_dir = Path(data_dir)
self.split = split
split_file_name = {
"test": "cloze_test_test__winter2018-cloze_test_ALL_test - 1.csv",
"dev": "cloze_test_val__winter2018-cloze_test_ALL_val - 1 - 1.csv",
"train": "ROCStories_winter2017 - ROCStories_winter2017.csv",
}[split]
columns = [
"InputStoryid",
"InputSentence1",
"InputSentence2",
"InputSentence3",
"InputSentence4",
"RandomFifthSentenceQuiz1",
"RandomFifthSentenceQuiz2",
"AnswerRightEnding",
]
self.file_path = self.data_dir / split_file_name
self.reader = csv.DictReader(open(self.file_path), fieldnames=columns)
self.data = []
next(self.reader)
for line in self.reader:
self.data.append(Story.from_json(line))
super().__init__()
def __getitem__(self, index):
return self.data[index]
if __name__ == "__main__":
ds = ROCStoriesDataset("../roc_stories", "dev")
for item in ds:
anchor = " ".join(item.sentences)
choices = [anchor + " " + s for s in item.candidate_endings]