Datasets:

License:
File size: 3,704 Bytes
8ab590c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import datasets
from datasets.tasks import LanguageModeling

# TODO 
# - shows how to include metadata from a separate file: https://huggingface.co/datasets/SIA86/WaterFlowCountersRecognition/blob/e659c03dfc5e50dd08648b92d66b2f3f3ef560a4/WaterFlowCountersRecognition.py 
# - shows how to add and use custom kwargs that we could use for globbing filenames: https://discuss.huggingface.co/t/using-config-kwargs-within-the-load-dataset/32112/3

_DATA_URL = "https://huggingface.co/datasets/anhaltai/fincorpus-de-10k/resolve/main/data/corpus_safe_txt_only.zip"

ALL_COLLECTIONS_NAME = "all"

# Top-level directories' names
CONFIG_NAMES = {
    "Annual_reports",
    "BBK_monthly",
    "Base_prospectuses",
    "Final_terms",
    #  "IFRS",
    #  "Informational_materials",
    "Law",
    ALL_COLLECTIONS_NAME
}


# TODO
_DESCRIPTION = """\
We introduce a predominantly German corpus comprising 12.5k PDF documents (and 10.5k extracted txt files) sourced from the financial domain. The corresponding extracted textual data encompasses more than 165 million tokens derived predominantly from German, and to a lesser extent, bilingual documents. 
We provide detailed information about the document types included in the corpus, such as final terms, base prospectuses, annual reports, information materials, law documents, international financial reporting standards, and monthly reports from the Bundesbank, accompanied by comprehensive statistical analysis.
This version of the dataset excludes two collections, IFRS and Informational_materials, leaving only datasets definitely releasable with an open license.
"""

# TODO bibtex citation here
_CITATION = """  """


class FincorpusConfig(datasets.BuilderConfig):
    def __init__(self, generate_sentences=False, **kwargs):
        super(FincorpusConfig, self).__init__(
            version=datasets.Version("1.0.0"), **kwargs
        )


class Fincorpus(datasets.GeneratorBasedBuilder):
    # VERSION = datasets.Version('1.0.0')

    BUILDER_CONFIGS = [
        FincorpusConfig(name=config_name) for config_name in CONFIG_NAMES
    ]
    DEFAULT_CONFIG_NAME = ALL_COLLECTIONS_NAME

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "filename": datasets.Value("string"),
                    "text": datasets.Value("string"),
                }
            ),
            supervised_keys=None,
            #  citation=_CITATION,
            task_templates=[LanguageModeling(text_column="text")],
        )

    def _split_generators(self, dl_manager):
        #  config_urls = _DATA_URL[self.config.name]
        config_url = _DATA_URL
        arch_path = dl_manager.download(config_url)
        #  files_paths = dl_manager.download_and_extract(config_url)
        #  subdir = self.config.name
        #  clean_paths = [x for x in files_paths if x.startswith(subdir)]
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"files": dl_manager.iter_archive(arch_path)},
            ),
        ]

    def _path_belongs_to_collection(self, path: str):
        subfolder_name = self.config.name
        if subfolder_name == ALL_COLLECTIONS_NAME:
            return True

        if path.startswith("txt/" + subfolder_name):
            return True
        return False

    def _generate_examples(self, files):
        _id = 0
        for path, f in files:
            if not self._path_belongs_to_collection(path):
                continue
            text = f.read().decode("utf-8").strip()
            yield _id, {"text": text, "filename": path}
            _id += 1