porsimplessent / porsimplessent.py
ruanchaves's picture
feat: write reader
04ab224
"""PorSimplesSent dataset"""
import datasets
import pandas as pd
_CITATION = """
"""
_DESCRIPTION = """
"""
_URLS = {
"nat_str": "https://raw.githubusercontent.com/sidleal/porsimplessent/c0b7bb6ccda6b40ebd7f5524b08a5699b2266ffe/pss/pss2_align_length_nat_str.tsv",
"ori_nat": "https://raw.githubusercontent.com/sidleal/porsimplessent/c0b7bb6ccda6b40ebd7f5524b08a5699b2266ffe/pss/pss2_align_length_ori_nat.tsv",
"ori_str": "https://raw.githubusercontent.com/sidleal/porsimplessent/c0b7bb6ccda6b40ebd7f5524b08a5699b2266ffe/pss/pss2_align_length_ori_str.tsv"
}
class PorSimplesSent(datasets.GeneratorBasedBuilder):
VERSION = datasets.Version("1.0.0")
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"sentence1": datasets.Value("string"),
"sentence2": datasets.Value("string"),
"label": datasets.Value("int32"),
"production_id": datasets.Value("int32"),
"level": datasets.Value("string"),
"changed": datasets.Value("string"),
"split": datasets.Value("string"),
"sentence_text_from": datasets.Value("string"),
"sentence_text_to": datasets.Value("string"),
}),
supervised_keys=None,
homepage="",
citation=_CITATION,
)
def _split_generators(self, dl_manager):
downloaded_files = dl_manager.download(_URLS)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"nat_str": downloaded_files["nat_str"],
"ori_nat": downloaded_files["ori_nat"],
"ori_str": downloaded_files["ori_str"],
"split": "train"
}
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION,
gen_kwargs={
"nat_str": downloaded_files["nat_str"],
"ori_nat": downloaded_files["ori_nat"],
"ori_str": downloaded_files["ori_str"],
"split": "validation"
}
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={
"nat_str": downloaded_files["nat_str"],
"ori_nat": downloaded_files["ori_nat"],
"ori_str": downloaded_files["ori_str"],
"split": "test"
}
)
]
def _generate_examples(self, nat_str, ori_nat, ori_str, split):
df1 = pd.read_csv(nat_str, sep="\t", on_bad_lines="skip")
df2 = pd.read_csv(ori_nat, sep="\t", on_bad_lines="skip")
df3 = pd.read_csv(ori_str, sep="\t", on_bad_lines="skip")
df = pd.concat([df1, df2, df3], axis=0)
df["mod"] = df["production_id"].apply(lambda x: x % 5)
if split == "validation":
df = df[df["mod"] == 1]
elif split == "test":
df = df[df["mod"] == 2]
elif split == "train":
df = df[df["mod"] != 1]
df = df[df["mod"] != 2]
df = df.sort_values(by=["production_id", "sentence_text_from", "sentence_text_to"])
prod_id_set = sorted(list(set(df["production_id"].values.tolist())))
records = df.to_dict("records")
for idx, item in enumerate(records):
row = {}
for key in ["production_id", "level", "changed", "split", "sentence_text_from", "sentence_text_to"]:
row[key] = item[key]
if item["changed"] == "S":
if prod_id_set.index(item["production_id"]) % 2 == 0:
row["sentence1"] = item["sentence_text_from"]
row["sentence2"] = item["sentence_text_to"]
row["label"] = 2
else:
row["sentence1"] = item["sentence_text_to"]
row["sentence2"] = item["sentence_text_from"]
row["label"] = 0
else:
row["sentence1"] = item["sentence_text_from"]
row["sentence2"] = item["sentence_text_to"]
row["label"] = 1
yield idx, row