File size: 3,323 Bytes
a40f768 |
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
import json
import datasets
from pathlib import Path
import pandas as pd
BASE_DATA_PATH = Path("./data")
class CSCOMMConfig(datasets.BuilderConfig):
"""BuilderConfig for CSCOMM."""
def __init__(self, key, pretraining=False, data_path="./data", **kwargs):
"""BuilderConfig for CSCOMM.
Args:
key: `string`
**kwargs: keyword arguments forwarded to super.
"""
# Version history:
# 0.0.1: Initial version.
super(CSCOMMConfig, self).__init__(version=datasets.Version("0.0.1"), **kwargs)
self.key = key
self.pretraining = pretraining
class CSCOMM(datasets.GeneratorBasedBuilder):
BUILDER_CONFIGS = [
CSCOMMConfig(
name="AP",
key="ap"
),
CSCOMMConfig(
name="AP+P",
key="ap_p"
),
CSCOMMConfig(
name="AP+J",
key="ap_j"
),
CSCOMMConfig(
name="AP+PJ",
key="ap_pj"
),
CSCOMMConfig(
name="BA",
key="ba"
),
CSCOMMConfig(
name="BA+P",
key="ba_p"
),
CSCOMMConfig(
name="BA+J",
key="ba_j"
),
CSCOMMConfig(
name="BA+PJ",
key="ba_pj"
),
CSCOMMConfig(
name="pretrain-unlabeled",
key="pt_un",
pretraining=True
),
CSCOMMConfig(
name="pretrain-labeled",
key="pt_la",
pretraining=True
),
CSCOMMConfig(
name="pretrain-both",
key="pt_unla",
pretraining=True
),
]
def _info(self):
features = {
"round_id": datasets.Value("string"),
"source": datasets.Value("string")
}
if not self.config.pretraining:
features["commentary"] = datasets.Value("string")
return datasets.DatasetInfo(
features=datasets.Features(features),
)
def _split_generators(self, dl_manager):
data_path = BASE_DATA_PATH / self.config.key
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"data_file": data_path / "train.csv",
"split": datasets.Split.TRAIN,
},
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"data_file": data_path / "valid.csv",
"split": datasets.Split.VALIDATION,
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"data_file": data_path / "test.csv",
"split": datasets.Split.TEST,
},
),
]
def _generate_examples(self, data_file, split):
df = pd.read_csv(data_file)
for i, row in enumerate(df.itertuples()):
example = {"idx": i, "round_id": row.round_id, "source": row.source}
if not self.config.pretraining:
example["commentary"] = row.commentary
yield example["idx"], example
|