|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""TODO: Add a description here.""" |
|
|
|
|
|
import csv |
|
import json |
|
import os |
|
import re |
|
import tempfile |
|
import urllib |
|
import requests |
|
from pathlib import Path |
|
from zipfile import ZipFile |
|
|
|
import datasets |
|
|
|
|
|
|
|
_CITATION = """\ |
|
@InProceedings{huggingface:dataset, |
|
title = {A great new dataset}, |
|
author={huggingface, Inc. |
|
}, |
|
year={2020} |
|
} |
|
""" |
|
|
|
|
|
|
|
_DESCRIPTION = """\ |
|
This dataset contains 402 argumentative essays from non-native |
|
""" |
|
|
|
|
|
_HOMEPAGE = "" |
|
|
|
|
|
_LICENSE = "" |
|
|
|
|
|
|
|
|
|
_URLS = { |
|
"tu_darmstadt": "https://tudatalib.ulb.tu-darmstadt.de/bitstream/handle/tudatalib/2422/ArgumentAnnotatedEssays-2.0.zip?sequence=1&isAllowed=y", |
|
} |
|
|
|
|
|
|
|
class NewDataset(datasets.GeneratorBasedBuilder): |
|
"""TODO: Short description of my dataset.""" |
|
|
|
VERSION = datasets.Version("1.1.0") |
|
|
|
temp_dir = tempfile.TemporaryDirectory() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BUILDER_CONFIGS = [ |
|
datasets.BuilderConfig( |
|
name="full_labels", |
|
version=VERSION, |
|
description="get all the data conveyed by the labels, O, B-Claim, I-Claim, etc.", |
|
), |
|
datasets.BuilderConfig( |
|
name="spans", |
|
version=VERSION, |
|
description="get the spans, O, B-Span, I-Span.", |
|
), |
|
datasets.BuilderConfig( |
|
name="simple", |
|
version=VERSION, |
|
description="get the labels without B/I, O, MajorClaim, Claim, Premise", |
|
), |
|
datasets.BuilderConfig( |
|
name="sep_tok", |
|
version=VERSION, |
|
description="get the labels without B/I, meaning O, Claim, Premise" |
|
+ ", etc.\n insert seperator tokens <s> ... </s>", |
|
), |
|
datasets.BuilderConfig( |
|
name="sep_tok_full_labels", |
|
version=VERSION, |
|
description="get the labels with B/I, meaning O, I-Claim, I-Premise" |
|
+ ", etc.\n insert seperator tokens <s> ... </s>", |
|
), |
|
] |
|
|
|
DEFAULT_CONFIG_NAME = "full_labels" |
|
|
|
def _info(self): |
|
|
|
if ( |
|
self.config.name == "full_labels" |
|
): |
|
features = datasets.Features( |
|
{ |
|
"id": datasets.Value("int16"), |
|
"tokens": datasets.Sequence(datasets.Value("string")), |
|
"ner_tags": datasets.Sequence( |
|
datasets.ClassLabel( |
|
names=[ |
|
"O", |
|
"B-MajorClaim", |
|
"I-MajorClaim", |
|
"B-Claim", |
|
"I-Claim", |
|
"B-Premise", |
|
"I-Premise", |
|
] |
|
) |
|
), |
|
"text": datasets.Value("string"), |
|
"span_begins": datasets.Sequence(datasets.Value("int16")), |
|
"span_ends": datasets.Sequence(datasets.Value("int16")), |
|
} |
|
) |
|
elif ( |
|
self.config.name == "spans" |
|
): |
|
features = datasets.Features( |
|
{ |
|
"id": datasets.Value("int16"), |
|
"tokens": datasets.Sequence(datasets.Value("string")), |
|
"ner_tags": datasets.Sequence( |
|
datasets.ClassLabel( |
|
names=[ |
|
"O", |
|
"B", |
|
"I", |
|
] |
|
) |
|
), |
|
"text": datasets.Value("string"), |
|
"span_begins": datasets.Sequence(datasets.Value("int16")), |
|
"span_ends": datasets.Sequence(datasets.Value("int16")), |
|
} |
|
) |
|
elif ( |
|
self.config.name == "simple" |
|
): |
|
features = datasets.Features( |
|
{ |
|
"id": datasets.Value("int16"), |
|
"tokens": datasets.Sequence(datasets.Value("string")), |
|
"ner_tags": datasets.Sequence( |
|
datasets.ClassLabel( |
|
names=[ |
|
"O", |
|
"X_placeholder_X", |
|
"MajorClaim", |
|
"Claim", |
|
"Premise", |
|
] |
|
) |
|
), |
|
"text": datasets.Value("string"), |
|
"span_begins": datasets.Sequence(datasets.Value("int16")), |
|
"span_ends": datasets.Sequence(datasets.Value("int16")), |
|
} |
|
) |
|
elif self.config.name == "sep_tok": |
|
features = datasets.Features( |
|
{ |
|
"id": datasets.Value("int16"), |
|
"tokens": datasets.Sequence(datasets.Value("string")), |
|
"ner_tags": datasets.Sequence( |
|
datasets.ClassLabel( |
|
names=[ |
|
"O", |
|
"X_placeholder_X", |
|
"MajorClaim", |
|
"Claim", |
|
"Premise", |
|
] |
|
) |
|
), |
|
"text": datasets.Value("string"), |
|
"span_begins": datasets.Sequence(datasets.Value("int16")), |
|
"span_ends": datasets.Sequence(datasets.Value("int16")), |
|
} |
|
) |
|
elif self.config.name == "sep_tok_full_labels": |
|
features = datasets.Features( |
|
{ |
|
"id": datasets.Value("int16"), |
|
"tokens": datasets.Sequence(datasets.Value("string")), |
|
"ner_tags": datasets.Sequence( |
|
datasets.ClassLabel( |
|
names=[ |
|
"O", |
|
"B-MajorClaim", |
|
"I-MajorClaim", |
|
"B-Claim", |
|
"I-Claim", |
|
"B-Premise", |
|
"I-Premise", |
|
] |
|
) |
|
), |
|
"text": datasets.Value("string"), |
|
"span_begins": datasets.Sequence(datasets.Value("int16")), |
|
"span_ends": datasets.Sequence(datasets.Value("int16")), |
|
} |
|
) |
|
|
|
return datasets.DatasetInfo( |
|
|
|
description=_DESCRIPTION, |
|
|
|
features=features, |
|
|
|
|
|
|
|
|
|
homepage=_HOMEPAGE, |
|
|
|
license=_LICENSE, |
|
|
|
citation=_CITATION, |
|
) |
|
|
|
def __load_data(self): |
|
|
|
save_dir = Path(self.temp_dir.name) |
|
save_file = Path("essays.zip") |
|
|
|
|
|
url = _URLS["tu_darmstadt"] |
|
|
|
r = requests.get(url, stream=True) |
|
|
|
with open(save_dir / save_file, 'wb') as fd: |
|
for chunk in r.iter_content(chunk_size=128): |
|
fd.write(chunk) |
|
|
|
for glob_path in save_dir.rglob("*.zip"): |
|
with ZipFile(glob_path, 'r') as zip_ref: |
|
zip_ref.extractall(glob_path.parent) |
|
return save_dir |
|
|
|
def __range_generator(self, train=0.8, test=0.2): |
|
""" |
|
returns three range objects to access the list of essays |
|
these are the train, test, and validate range, where the size of the |
|
validation range is dictated by the other two ranges |
|
""" |
|
|
|
return ( |
|
range(1, int(403 * train)), |
|
range(int(403 * train), int(403 * (train + test))), |
|
range(int(403 * (train + test)), 403), |
|
) |
|
|
|
def _split_generators(self, _): |
|
data_dir = self.__load_data() |
|
|
|
|
|
|
|
train, test, validate = self.__range_generator(1, 0) |
|
|
|
|
|
if len(validate) > 0 and len(test) > 0: |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
|
|
gen_kwargs={ |
|
"data_dir": data_dir, |
|
"id_range": train, |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.VALIDATION, |
|
|
|
gen_kwargs={ |
|
"data_dir": data_dir, |
|
"id_range": validate, |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
|
|
gen_kwargs={ |
|
"data_dir": data_dir, |
|
"id_range": test, |
|
}, |
|
), |
|
] |
|
elif len(test) > 0: |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
|
|
gen_kwargs={ |
|
"data_dir": data_dir, |
|
"id_range": train, |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
|
|
gen_kwargs={ |
|
"data_dir": data_dir, |
|
"id_range": test, |
|
}, |
|
), |
|
] |
|
elif len(validate) > 0: |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
|
|
gen_kwargs={ |
|
"data_dir": data_dir, |
|
"id_range": train, |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.VALIDATION, |
|
|
|
gen_kwargs={ |
|
"data_dir": data_dir, |
|
"id_range": validate, |
|
}, |
|
), |
|
] |
|
else: |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
|
|
gen_kwargs={ |
|
"data_dir": data_dir, |
|
"id_range": train, |
|
}, |
|
), |
|
] |
|
|
|
def _get_essay(self, id: int, data_dir: Path): |
|
return data_dir.joinpath(f"essay{str(id).rjust(3, '0')}.txt").read_text(), data_dir.joinpath(f"essay{str(id).rjust(3, '0')}.ann").read_text() |
|
|
|
def _parse_raw_ann(self, raw_ann: str): |
|
raw_anns = raw_ann.split("\n") |
|
clean_anns = [] |
|
for cur_raw_ann in raw_anns: |
|
matches = re.match(r".+\t(.+) (.+) (.+)\t(.+)", cur_raw_ann) |
|
if matches is not None: |
|
clean_anns.append( |
|
(matches.group(1), int(matches.group(2)), int(matches.group(3)), matches.group(4)) |
|
) |
|
|
|
return sorted(clean_anns, key=lambda x: x[1]) |
|
|
|
def _tokenise(self, text, clean_anns): |
|
|
|
previous_end = 0 |
|
spans = [] |
|
|
|
for clean_ann in clean_anns: |
|
spans.append(("O", text[previous_end:clean_ann[1]])) |
|
spans.append((clean_ann[0], text[clean_ann[1]:clean_ann[2]])) |
|
previous_end = clean_ann[2] |
|
|
|
spans.append(("O", text[previous_end:])) |
|
|
|
tokens = [] |
|
labels = [] |
|
|
|
|
|
|
|
|
|
for span in spans: |
|
span_tokens = span[1].split() |
|
label = span[0] |
|
first_label = span[0] |
|
if self.config.name == "simple": |
|
|
|
pass |
|
|
|
elif self.config.name == "sep_tok": |
|
|
|
span_tokens.insert(0, "<s>") |
|
span_tokens.append("</s>") |
|
|
|
elif self.config.name == "spans": |
|
if label != "O": |
|
first_label = "B" |
|
label = "I" |
|
|
|
elif self.config.name == "full_labels": |
|
if label != "O": |
|
first_label = "B-" + label |
|
label = "I-" + label |
|
elif self.config.name == "sep_tok_full_labels": |
|
|
|
if label != "O": |
|
first_label = "B-" + label |
|
label = "I-" + label |
|
|
|
span_tokens.insert(0, "<s>") |
|
span_tokens.append("</s>") |
|
|
|
labels.append(first_label) |
|
labels.extend([label] * (len(span_tokens) - 1)) |
|
tokens.extend(span_tokens) |
|
|
|
return tokens, labels |
|
|
|
def _process_essay(self, id, data_dir: Path): |
|
|
|
text, raw_ann = self._get_essay(id, data_dir) |
|
clean_anns = self._parse_raw_ann(raw_ann) |
|
tokens, labels = self._tokenise(text, clean_anns) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { |
|
"id": id, |
|
"tokens": tokens, |
|
"ner_tags": labels, |
|
"text": text, |
|
"span_begins": [ann[1] for ann in clean_anns], |
|
"span_ends": [ann[2] for ann in clean_anns], |
|
} |
|
|
|
|
|
def _generate_examples(self, data_dir: Path, id_range: list): |
|
|
|
|
|
|
|
data_dir = data_dir.joinpath("ArgumentAnnotatedEssays-2.0", "brat-project-final") |
|
|
|
for id in id_range: |
|
|
|
yield id, self._process_essay(id, data_dir) |
|
|