File size: 4,139 Bytes
02e8cc7
 
df0f3a2
 
caed78f
 
bb7b387
df0f3a2
bb7b387
 
02e8cc7
77d852e
 
02e8cc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7d4f95
02e8cc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import datasets
import json
import os
import sys

dl = datasets.DownloadManager()
configs_file = dl.download('https://huggingface.co/datasets/RealTimeData/bbc_alltime/raw/main/configs.txt')

with open(configs_file, encoding="utf-8") as f:
    _TIMES = f.read().splitlines()

_TIMES += ['all']

_CITATION = """\
@misc{li2023estimating,
      title={Estimating Contamination via Perplexity: Quantifying Memorisation in Language Model Evaluation}, 
      author={Yucheng Li},
      year={2023},
      eprint={2309.10677},
      archivePrefix={arXiv},
      primaryClass={cs.CL}
}
"""

_DESCRIPTION = """\
This dataset contains BBC News articles from 2017 to 2022. The articles are arraged by month. Access the specific month by using the format "YYYY-MM" as config. Such as load_dataset("RealTimeData/bbc_alltime", "2021-1").
"""

_HOMEPAGE = "https://github.com/liyucheng09/Contamination_Detector"

class Bbc_alltimes(datasets.GeneratorBasedBuilder):

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name=time, version=datasets.Version("1.0.0"), description=f"BBC News articles published in the priod of {time}"
        )
        for time in _TIMES
    ]

    def _info(self):
        features = datasets.Features(
            {
                "title": datasets.Value("string"),
                "published_date": datasets.Value("string"),
                "authors": datasets.Value("string"),
                "description": datasets.Value("string"),
                "section": datasets.Value("string"),
                "content": datasets.Value("string"),
                "link": datasets.Value("string"),
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        if self.config.name == "all":
            times = _TIMES[:-1]
            files = dl_manager.download([f"articles/{time}.json" for time in _TIMES ])
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    gen_kwargs={"files": files},
                )
            ]
        else:
            time = self.config.name
            _URL = f"articles/{time}.json"
            file = dl_manager.download(_URL)
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    gen_kwargs={"files": file},
                )
            ]

    def _generate_examples(self, files):
        """Yields examples."""
        if self.config.name == "all":
            assert isinstance(files, list)
            for file in files:
                time = file.strip('.json')
                with open(file, encoding="utf-8") as f:
                    data = json.load(f)
                length = len(data['title'])
                for i in range(length):
                    yield f'{time}-{i}', {
                        "title": data['title'][i],
                        "published_date": data['published_date'][i],
                        "authors": data['authors'][i],
                        "description": data['description'][i],
                        "section": data['section'][i],
                        "content": data['content'][i],
                        "link": data['link'][i],
                    }
        else:
            assert isinstance(files, str)
            time = self.config.name
            with open(files, encoding="utf-8") as f:
                data = json.load(f)
            length = len(data['title'])
            for i in range(length):
                yield f'{time}-{i}', {
                    "title": data['title'][i],
                    "published_date": data['published_date'][i],
                    "authors": data['authors'][i],
                    "description": data['description'][i],
                    "section": data['section'][i],
                    "content": data['content'][i],
                    "link": data['link'][i],
                }