File size: 5,411 Bytes
56a8d42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# coding=utf-8
# Copyright 2022 HuggingFace Datasets Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Lint as: python3

import requests
import json
import re


import datasets


_CITATION = """\
TODO
"""

_DESCRIPTION = """\
This repository contains plain text versions of texts written in medieval French, deriving from critical editions believed to be out of copyright or known to be under Creative Commons licenses. They are published here with a CC BY-NC-SA 4.0 International license.

At present there is no order for the texts. File names are derivative of the DEAFél unique identifiers ('sigle') for editions. Metadata included in the file header are an expanded version of the 2006 recommendations of the Consortium pour les corpus de français médiéval CCFM.

The categories WebAuthor and WebReviewer in the metadata attempt to assign credit for the various tasks of the project. We use the CASRAI Credit Typology here. WebAuthor most closely maps to "Data Curation" and Web Reviewer to "Writing – Review & Editing".
"""

_URL = "https://api.github.com/repos/OpenMedFr/texts/contents/."


class OpenMedievalFrench(datasets.BuilderConfig):
    """BuilderConfig for Open Medieval French"""

    def __init__(self, data_urls, **kwargs):
        """BuilderConfig for Open Medieval French.

        Args:
          **kwargs: keyword arguments forwarded to super.
        """
        super(OpenMedievalFrench, self).__init__(**kwargs)
        self.url = "TODO"
        self.data_urls = data_urls


class OpenMedievalFrench(datasets.GeneratorBasedBuilder):
    """HIPE2020 dataset."""

    BUILDER_CONFIGS = [
        OpenMedievalFrench(
            name="default",
            data_urls=_URL,
            version=datasets.Version("1.0.0"),
            description="Open Medieval French",
        )
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "text_uri": datasets.Value("string"),
                    "text": datasets.Value("string"),
                    "text_deaf": datasets.Value("string"),
                    "text_title": datasets.Value("string"),
                    "text_dat_arl": datasets.Value("string"),
                    "text_data_adj": datasets.Value("string"),
                    "text_data_min": datasets.Value("string"),
                    "text_data_max": datasets.Value("string"),
                    "text_licence": datasets.Value("string"),
                }
            ),
            supervised_keys=None,
            homepage="TODO",
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        url = _URL
        r = requests.get(url)
        data = r.json()
        data_urls = [x["download_url"] for x in data]
        downloaded_files = dl_manager.download_and_extract(data_urls)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"downloaded_files": downloaded_files},
            ),
        ]

    def _generate_examples(self, downloaded_files):
        regex_dict = {
            "text_uri": re.compile(r"#META#.*TextURI.*\:\:\s(.*)$"),
            "text_deaf": re.compile(r"#META#.*DEAF.*\:\:\s(.*)$"),
            "text_title": re.compile(r"#META#.*TextTitle.*\:\:\s(.*)$"),
            "text_dat_arl": re.compile(r"#META#.*TextDateARL.*\:\:\s(.*)$"),
            "text_data_adj": re.compile(r"#META#.*TextDateAdj.*\:\:\s(.*)$"),
            "text_data_min": re.compile(r"#META#.*TextDateMin.*\:\:.*(\d{4})"),
            "text_data_max": re.compile(r"#META#.*TextDateMax.*\:\:\s(.*)$"),
            "text_licence": re.compile(r"#META#.*CCLicense.*\:\:\s(.*)$"),
        }
        id_ =0
        for file in downloaded_files:
            with open(file, encoding="utf-8") as f:
                paragraph = {k: None for k in regex_dict} # default none values for metadata
                para_text  = []
                first = True   
                for line in f.readlines():
                    if line.startswith("#META#"):
                        for k, v in regex_dict.items():
                            m = v.search(line)
                            if m:
                                paragraph[k] = m.group(1)
                    if line.startswith("*** START"):
                        continue
                    if line.startswith("# |"):
                        if not first:
                            paragraph["text"] = " ".join(para_text)
                            id_ +=1
                            para_text = []
                            yield id_, paragraph
                        else:
                            first = False
                            continue
                    else:
                        para_text.append(line)