murakami / murakami.py
vladsaveliev
Remove gdown dependency and add requirements.txt
749f8c8
raw
history blame
7.33 kB
"""
Parse all paragraphs from all *.fb2 files in the input directory, create a Huggingface Dataset and push it to the Hub as `vldsavelyev/murakami`.
"""
import os
from pathlib import Path
from lxml import etree
import datasets
from datasets import Dataset
from huggingface_hub import create_repo
datasets.logging.set_verbosity_info()
_DESCRIPTION = """\
Russian translations of Murakami novels, to fine-tune a generative language model. Source is FB2 files from http://flibusta.is/a/8570.
"""
class Builder(datasets.GeneratorBasedBuilder):
"""Murakami novels, translated to Russian."""
VERSION = datasets.Version("1.1.0")
# This is an example of a dataset with multiple configurations.
# If you don't want/need to define several sub-sets in your dataset,
# just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
# If you need to make complex sub-parts in the datasets with configurable options
# You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
# BUILDER_CONFIG_CLASS = MyBuilderConfig
# You will be able to load one or the other configurations in the following list with
# data = datasets.load_dataset('my_dataset', 'first_domain')
# data = datasets.load_dataset('my_dataset', 'second_domain')
def _info(self):
# This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
return datasets.DatasetInfo(
# This is the description that will appear on the datasets page.
description=_DESCRIPTION,
# This defines the different columns of the dataset and their types
features=datasets.Features({"text": datasets.Value("string")}),
)
# Number of initial <p> element to take from each fb2, by number. This allows to skip
# intros and other junk in the beginning of an fb2. This is built semi-manually using
# the `helper_to_find_first_paragraphs` func.
START_PARAGRAPHS = {
3: 5,
6: 27,
7: 3,
9: 4,
10: 3,
12: 11,
18: 5,
20: 3,
21: 5,
}
@staticmethod
def helper_to_find_first_paragraphs(paragraphs, title, book_number, n=30):
"""
Helps to eyeball first few paragraphs of a book to skip junk paragraphs
in the beginning and manually construct the `tart_paragraphs` dict.
"""
found_paragraphs = []
skipping = True
for i, p in enumerate(list(paragraphs)[:n]):
if p.text is None:
continue
if (
book_number in Builder.START_PARAGRAPHS
and i >= Builder.START_PARAGRAPHS[book_number]
):
skipping = False
if skipping and p.text.lower() == title.lower():
skipping = False
if not skipping:
found_paragraphs.append(f" {i} {p.text}")
if found_paragraphs:
print("✅")
print("\n".join(found_paragraphs))
else:
print("❌")
for i, p in enumerate(list(paragraphs)[:30]):
print(f" {i} {p.text}")
def _split_generators(self, dl_manager):
# This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
data_dir = "data"
text_by_name = {}
fb2s = list(Path(data_dir).glob("*.fb2"))
if len(fb2s) > 0:
print(f"Found {len(fb2s)} fb2 files in {data_dir}")
else:
raise ValueError(f"No fb2 files found in {data_dir}")
for bi, path in enumerate(fb2s):
print(bi, path)
# Load the FB2 format file
with path.open("rb") as file:
fb2_data = file.read()
# Print structure of the FB2 format file
# print(etree.tostring(etree.fromstring(fb2_data), pretty_print=True))
# Parse the FB2 format file using lxml
root = etree.fromstring(fb2_data)
# Get the title of the book
title = root.xpath(
"//fb:title-info/fb:book-title",
namespaces={"fb": "http://www.gribuser.ru/xml/fictionbook/2.0"},
)[0].text
print(title)
# Get all book paragraphs
paragraphs = root.xpath(
"//fb:p",
namespaces={"fb": "http://www.gribuser.ru/xml/fictionbook/2.0"},
)
# UNCOMMENT THE LINE BELOW TO BUILD `START_PARAGRAPHS`:
# self.helper_to_find_first_paragraphs(paragraphs, title, bi)
found_paragraphs = []
skipping = True
for pi, p in enumerate(paragraphs):
if p.text is None:
continue
if (
bi in Builder.START_PARAGRAPHS
and pi >= Builder.START_PARAGRAPHS[bi]
):
skipping = False
if skipping and p.text.lower() == title.lower():
skipping = False
if not skipping:
found_paragraphs.append(p)
print(f"Found {len(found_paragraphs)} paragraphs")
text_by_name[title] = ""
for p in found_paragraphs:
text_by_name[title] += p.text.replace(" ", " ") + "\n"
text_by_name[title] += "\n"
print("Novel by size:")
for title, text in text_by_name.items():
print(f" {title}: {len(text):,} characters")
smallest_title = min(text_by_name, key=lambda k: len(text_by_name[k]))
print(
f"Using smallest novel {smallest_title} "
f"({len(text_by_name[smallest_title]):,} characters) as a test set"
)
test_titles = [smallest_title]
train_titles = [t for t in text_by_name if t not in test_titles]
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"titles": train_titles,
"texts": [text_by_name[t] for t in train_titles],
"split": "train",
},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"titles": test_titles,
"texts": [text_by_name[t] for t in test_titles],
"split": "test",
},
),
]
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
def _generate_examples(self, titles, texts, split):
# This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
# The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
for title, text in zip(titles, texts):
yield title, {"text": text}