|
"""The Argumentative Microtext Corpus for German and English Argumentation Mining.""" |
|
import glob |
|
import logging |
|
import os |
|
from os.path import abspath, isdir |
|
from pathlib import Path |
|
from xml.etree import ElementTree |
|
|
|
import datasets |
|
|
|
_CITATION = """\ |
|
@inproceedings{peldszus2015annotated, |
|
title={An annotated corpus of argumentative microtexts}, |
|
author={Peldszus, Andreas and Stede, Manfred}, |
|
booktitle={Argumentation and Reasoned Action: Proceedings of the 1st European Conference on Argumentation, Lisbon}, |
|
volume={2}, |
|
pages={801--815}, |
|
year={2015} |
|
} |
|
""" |
|
|
|
_DESCRIPTION = "The Argumentative Microtext Corpus for German and English Argumentation Mining." |
|
|
|
_HOMEPAGE = "http://angcl.ling.uni-potsdam.de/resources/argmicro.html" |
|
|
|
_LICENSE = "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License, see https://creativecommons.org/licenses/by-nc-sa/4.0/" |
|
|
|
|
|
|
|
|
|
_URL = "https://github.com/peldszus/arg-microtexts/archive/refs/heads/master.zip" |
|
|
|
_VERSION = datasets.Version("1.0.0") |
|
|
|
_STANCE_CLASS_LABELS = ["con", "pro", "unclear", "UNDEFINED"] |
|
_ADU_CLASS_LABELS = ["opp", "pro"] |
|
_EDGE_CLASS_LABELS = ["seg", "sup", "exa", "add", "reb", "und"] |
|
|
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
class ArgMicro(datasets.GeneratorBasedBuilder): |
|
"""ArgMicro is a argumentation mining dataset.""" |
|
|
|
BUILDER_CONFIGS = [ |
|
datasets.BuilderConfig(name="en"), |
|
datasets.BuilderConfig(name="de"), |
|
] |
|
|
|
def _info(self): |
|
features = datasets.Features( |
|
{ |
|
"id": datasets.Value("string"), |
|
"topic_id": datasets.Value("string"), |
|
"stance": datasets.ClassLabel(names=_STANCE_CLASS_LABELS), |
|
"text": datasets.Value("string"), |
|
"edus": datasets.Sequence( |
|
{ |
|
"id": datasets.Value("string"), |
|
"start": datasets.Value("int32"), |
|
"end": datasets.Value("int32"), |
|
} |
|
), |
|
"adus": datasets.Sequence( |
|
{ |
|
"id": datasets.Value("string"), |
|
"type": datasets.ClassLabel(names=_ADU_CLASS_LABELS), |
|
} |
|
), |
|
"edges": datasets.Sequence( |
|
{ |
|
"id": datasets.Value("string"), |
|
"src": datasets.Value("string"), |
|
"trg": datasets.Value("string"), |
|
"type": datasets.ClassLabel(names=_EDGE_CLASS_LABELS), |
|
} |
|
), |
|
} |
|
) |
|
|
|
return datasets.DatasetInfo( |
|
|
|
description=_DESCRIPTION, |
|
|
|
features=features, |
|
|
|
|
|
|
|
supervised_keys=None, |
|
|
|
homepage=_HOMEPAGE, |
|
|
|
license=_LICENSE, |
|
|
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
"""Returns SplitGenerators.""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
if dl_manager.manual_dir is not None: |
|
base_path = abspath(dl_manager.manual_dir) |
|
if not isdir(base_path): |
|
base_path = os.path.join(dl_manager.extract(base_path), "arg-microtexts-master") |
|
else: |
|
base_path = os.path.join( |
|
dl_manager.download_and_extract(_URL), "arg-microtexts-master" |
|
) |
|
base_path = Path(base_path) / "corpus" |
|
|
|
dtd = None |
|
etree = None |
|
try: |
|
from lxml import etree |
|
|
|
|
|
except ModuleNotFoundError: |
|
logger.warning("lxml not installed. Skipping DTD validation.") |
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={"path": base_path / self.config.name, "dtd": dtd, "etree": etree}, |
|
), |
|
] |
|
|
|
def _generate_examples(self, path, dtd=None, etree=None): |
|
"""Yields examples.""" |
|
|
|
|
|
|
|
|
|
_id = 0 |
|
text_file_names = sorted(glob.glob(f"{path}/*.txt")) |
|
if len(text_file_names) == 0: |
|
raise Exception(f"No text files found in {path}. Did you set the correct data_dir?") |
|
invalid_files = [] |
|
for text_file_name in text_file_names: |
|
txt_fn = Path(text_file_name) |
|
ann_fn = txt_fn.with_suffix(".xml") |
|
with open(txt_fn, encoding="utf-8") as f: |
|
text = f.read() |
|
|
|
|
|
if dtd is not None and etree is not None: |
|
e = etree.parse(ann_fn) |
|
v = dtd.validate(e) |
|
if not v: |
|
logger.error(f"{ann_fn} is INVALID:") |
|
logger.error(dtd.error_log.filter_from_errors()[0]) |
|
invalid_files.append(ann_fn) |
|
continue |
|
|
|
annotations = ElementTree.parse(ann_fn).getroot() |
|
edus = [] |
|
start_pos = 0 |
|
for edu in annotations.findall("edu"): |
|
start = text.find(edu.text, start_pos) |
|
if start == -1: |
|
raise Exception(f"Cannot find {edu.text} in {text}") |
|
end = start + len(edu.text) |
|
edus.append({"id": edu.attrib["id"], "start": start, "end": end}) |
|
start_pos = end |
|
adus = [ |
|
{"id": adu.attrib["id"], "type": adu.attrib["type"]} |
|
for adu in annotations.findall("adu") |
|
] |
|
edges = [ |
|
{ |
|
"id": edge.attrib["id"], |
|
"src": edge.attrib["src"], |
|
"trg": edge.attrib["trg"], |
|
"type": edge.attrib["type"], |
|
} |
|
for edge in annotations.findall("edge") |
|
] |
|
yield _id, { |
|
"id": annotations.attrib["id"], |
|
"topic_id": annotations.attrib.get("topic_id", "UNDEFINED"), |
|
"stance": annotations.attrib.get("stance", "UNDEFINED"), |
|
"text": text, |
|
"edus": sorted(edus, key=lambda x: x["id"]), |
|
"adus": sorted(adus, key=lambda x: x["id"]), |
|
"edges": sorted(edges, key=lambda x: x["id"]), |
|
} |
|
_id += 1 |
|
|
|
if len(invalid_files) > 0: |
|
raise Exception(f"Found {len(invalid_files)} invalid files: {invalid_files}") |
|
|