File size: 3,413 Bytes
7b9e520
a493627
 
7b9e520
 
 
 
 
 
4a5609e
7b9e520
 
 
 
 
 
 
 
 
 
 
4d962fc
a493627
 
768f7eb
b82e694
373bd8e
 
f479237
 
4d962fc
7fe48b5
7b9e520
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373bd8e
7fe48b5
 
 
 
 
 
7b9e520
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a5609e
 
 
7b9e520
 
 
 
 
 
 
 
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
import json
from itertools import product

import datasets


logger = datasets.logging.get_logger(__name__)
_DESCRIPTION = """T-Rex dataset."""
_NAME = "t_rex"
_VERSION = "0.1.0"
_CITATION = """
@inproceedings{elsahar2018t,
  title={T-rex: A large scale alignment of natural language with knowledge base triples},
  author={Elsahar, Hady and Vougiouklis, Pavlos and Remaci, Arslen and Gravier, Christophe and Hare, Jonathon and Laforest, Frederique and Simperl, Elena},
  booktitle={Proceedings of the Eleventh International Conference on Language Resources and Evaluation (LREC 2018)},
  year={2018}
} 
"""

_HOME_PAGE = "https://github.com/asahi417/relbert"
_URL = f'https://huggingface.co/datasets/relbert/{_NAME}/resolve/main/data'
MIN_ENTITY_FREQ = [1, 2, 3, 4]
MAX_PREDICATE_FREQ = [100, 50, 25, 10]

_TYPES = [f"filter_unified.min_entity_{a}_max_predicate_{b}" for a, b in product(MIN_ENTITY_FREQ, MAX_PREDICATE_FREQ)]
# _TYPES += ["raw", "filter", "filter_unified"]
_NON_SPLITS = ["raw", "filter", "filter_unified"]
_URLS = {i: {str(datasets.Split.TRAIN): [f'{_URL}/t_rex.{i}.jsonl']} if i in _NON_SPLITS else {
    str(datasets.Split.TRAIN): [f'{_URL}/t_rex.{i}.train.jsonl'],
    str(datasets.Split.VALIDATION): [f'{_URL}/t_rex.{i}.validation.jsonl'],
    str(datasets.Split.TEST): [f'{_URL}/t_rex.filter_unified.test.jsonl']}
         for i in _TYPES}


class TREXConfig(datasets.BuilderConfig):
    """BuilderConfig"""

    def __init__(self, **kwargs):
        """BuilderConfig.
        Args:
          **kwargs: keyword arguments forwarded to super.
        """
        super(TREXConfig, self).__init__(**kwargs)


class TREX(datasets.GeneratorBasedBuilder):
    """Dataset."""

    BUILDER_CONFIGS = [
        TREXConfig(name=i, version=datasets.Version(_VERSION), description=_DESCRIPTION)
        for i in sorted(_TYPES)
    ]

    def _split_generators(self, dl_manager):
        downloaded_file = dl_manager.download_and_extract(_URLS[self.config.name])
        if self.config.name in _NON_SPLITS:
            return [datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"filepaths": downloaded_file[str(datasets.Split.TRAIN)]})]
        else:
            return [datasets.SplitGenerator(name=i, gen_kwargs={"filepaths": downloaded_file[str(i)]})
                    for i in [datasets.Split.TRAIN, datasets.Split.VALIDATION, datasets.Split.TEST]]

    def _generate_examples(self, filepaths):
        _key = 0
        for filepath in filepaths:
            logger.info(f"generating examples from = {filepath}")
            with open(filepath, encoding="utf-8") as f:
                _list = [i for i in f.read().split('\n') if len(i) > 0]
                for i in _list:
                    data = json.loads(i)
                    yield _key, data
                    _key += 1

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "relation": datasets.Value("string"),
                    "head": datasets.Value("string"),
                    "tail": datasets.Value("string"),
                    "title": datasets.Value("string"),
                    "text": datasets.Value("string"),
                }
            ),
            supervised_keys=None,
            homepage=_HOME_PAGE,
            citation=_CITATION,
        )