Datasets:
Commit
•
56a8d42
1
Parent(s):
14fdbc7
Upload open_medieval_french.py
Browse files- open_medieval_french.py +137 -0
open_medieval_french.py
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2022 HuggingFace Datasets Authors.
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
|
16 |
+
# Lint as: python3
|
17 |
+
|
18 |
+
import requests
|
19 |
+
import json
|
20 |
+
import re
|
21 |
+
|
22 |
+
|
23 |
+
import datasets
|
24 |
+
|
25 |
+
|
26 |
+
_CITATION = """\
|
27 |
+
TODO
|
28 |
+
"""
|
29 |
+
|
30 |
+
_DESCRIPTION = """\
|
31 |
+
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.
|
32 |
+
|
33 |
+
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.
|
34 |
+
|
35 |
+
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".
|
36 |
+
"""
|
37 |
+
|
38 |
+
_URL = "https://api.github.com/repos/OpenMedFr/texts/contents/."
|
39 |
+
|
40 |
+
|
41 |
+
class OpenMedievalFrench(datasets.BuilderConfig):
|
42 |
+
"""BuilderConfig for Open Medieval French"""
|
43 |
+
|
44 |
+
def __init__(self, data_urls, **kwargs):
|
45 |
+
"""BuilderConfig for Open Medieval French.
|
46 |
+
|
47 |
+
Args:
|
48 |
+
**kwargs: keyword arguments forwarded to super.
|
49 |
+
"""
|
50 |
+
super(OpenMedievalFrench, self).__init__(**kwargs)
|
51 |
+
self.url = "TODO"
|
52 |
+
self.data_urls = data_urls
|
53 |
+
|
54 |
+
|
55 |
+
class OpenMedievalFrench(datasets.GeneratorBasedBuilder):
|
56 |
+
"""HIPE2020 dataset."""
|
57 |
+
|
58 |
+
BUILDER_CONFIGS = [
|
59 |
+
OpenMedievalFrench(
|
60 |
+
name="default",
|
61 |
+
data_urls=_URL,
|
62 |
+
version=datasets.Version("1.0.0"),
|
63 |
+
description="Open Medieval French",
|
64 |
+
)
|
65 |
+
]
|
66 |
+
|
67 |
+
def _info(self):
|
68 |
+
return datasets.DatasetInfo(
|
69 |
+
description=_DESCRIPTION,
|
70 |
+
features=datasets.Features(
|
71 |
+
{
|
72 |
+
"text_uri": datasets.Value("string"),
|
73 |
+
"text": datasets.Value("string"),
|
74 |
+
"text_deaf": datasets.Value("string"),
|
75 |
+
"text_title": datasets.Value("string"),
|
76 |
+
"text_dat_arl": datasets.Value("string"),
|
77 |
+
"text_data_adj": datasets.Value("string"),
|
78 |
+
"text_data_min": datasets.Value("string"),
|
79 |
+
"text_data_max": datasets.Value("string"),
|
80 |
+
"text_licence": datasets.Value("string"),
|
81 |
+
}
|
82 |
+
),
|
83 |
+
supervised_keys=None,
|
84 |
+
homepage="TODO",
|
85 |
+
citation=_CITATION,
|
86 |
+
)
|
87 |
+
|
88 |
+
def _split_generators(self, dl_manager):
|
89 |
+
"""Returns SplitGenerators."""
|
90 |
+
url = _URL
|
91 |
+
r = requests.get(url)
|
92 |
+
data = r.json()
|
93 |
+
data_urls = [x["download_url"] for x in data]
|
94 |
+
downloaded_files = dl_manager.download_and_extract(data_urls)
|
95 |
+
return [
|
96 |
+
datasets.SplitGenerator(
|
97 |
+
name=datasets.Split.TRAIN,
|
98 |
+
gen_kwargs={"downloaded_files": downloaded_files},
|
99 |
+
),
|
100 |
+
]
|
101 |
+
|
102 |
+
def _generate_examples(self, downloaded_files):
|
103 |
+
regex_dict = {
|
104 |
+
"text_uri": re.compile(r"#META#.*TextURI.*\:\:\s(.*)$"),
|
105 |
+
"text_deaf": re.compile(r"#META#.*DEAF.*\:\:\s(.*)$"),
|
106 |
+
"text_title": re.compile(r"#META#.*TextTitle.*\:\:\s(.*)$"),
|
107 |
+
"text_dat_arl": re.compile(r"#META#.*TextDateARL.*\:\:\s(.*)$"),
|
108 |
+
"text_data_adj": re.compile(r"#META#.*TextDateAdj.*\:\:\s(.*)$"),
|
109 |
+
"text_data_min": re.compile(r"#META#.*TextDateMin.*\:\:.*(\d{4})"),
|
110 |
+
"text_data_max": re.compile(r"#META#.*TextDateMax.*\:\:\s(.*)$"),
|
111 |
+
"text_licence": re.compile(r"#META#.*CCLicense.*\:\:\s(.*)$"),
|
112 |
+
}
|
113 |
+
id_ =0
|
114 |
+
for file in downloaded_files:
|
115 |
+
with open(file, encoding="utf-8") as f:
|
116 |
+
paragraph = {k: None for k in regex_dict} # default none values for metadata
|
117 |
+
para_text = []
|
118 |
+
first = True
|
119 |
+
for line in f.readlines():
|
120 |
+
if line.startswith("#META#"):
|
121 |
+
for k, v in regex_dict.items():
|
122 |
+
m = v.search(line)
|
123 |
+
if m:
|
124 |
+
paragraph[k] = m.group(1)
|
125 |
+
if line.startswith("*** START"):
|
126 |
+
continue
|
127 |
+
if line.startswith("# |"):
|
128 |
+
if not first:
|
129 |
+
paragraph["text"] = " ".join(para_text)
|
130 |
+
id_ +=1
|
131 |
+
para_text = []
|
132 |
+
yield id_, paragraph
|
133 |
+
else:
|
134 |
+
first = False
|
135 |
+
continue
|
136 |
+
else:
|
137 |
+
para_text.append(line)
|