Open_Medieval_French / open_medieval_french.py
davanstrien's picture
davanstrien HF staff
Upload open_medieval_french.py
56a8d42
# 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)