|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace NLP 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 | +"""The BookCorpus dataset.""" |
| 18 | + |
| 19 | +from __future__ import absolute_import, division, print_function |
| 20 | + |
| 21 | +import glob |
| 22 | +import os |
| 23 | +import re |
| 24 | + |
| 25 | +import nlp |
| 26 | + |
| 27 | +_DESCRIPTION = """\ |
| 28 | +Books are a rich source of both fine-grained information, how a character, \ |
| 29 | +an object or a scene looks like, as well as high-level semantics, what \ |
| 30 | +someone is thinking, feeling and how these states evolve through a story.\ |
| 31 | +This work aims to align books to their movie releases in order to provide\ |
| 32 | +rich descriptive explanations for visual content that go semantically far\ |
| 33 | +beyond the captions available in current datasets. \ |
| 34 | +""" |
| 35 | + |
| 36 | +_CITATION = """\ |
| 37 | +@InProceedings{Zhu_2015_ICCV, |
| 38 | + title = {Aligning Books and Movies: Towards Story-Like Visual Explanations by Watching Movies and Reading Books}, |
| 39 | + author = {Zhu, Yukun and Kiros, Ryan and Zemel, Rich and Salakhutdinov, Ruslan and Urtasun, Raquel and Torralba, Antonio and Fidler, Sanja}, |
| 40 | + booktitle = {The IEEE International Conference on Computer Vision (ICCV)}, |
| 41 | + month = {December}, |
| 42 | + year = {2015} |
| 43 | +} |
| 44 | +""" |
| 45 | + |
| 46 | +URL = "https://storage.googleapis.com/huggingface-nlp/datasets/bookcorpus/bookcorpus.tar.bz2" |
| 47 | + |
| 48 | +class BookcorpusConfig(nlp.BuilderConfig): |
| 49 | + """BuilderConfig for BookCorpus.""" |
| 50 | + |
| 51 | + def __init__(self, **kwargs): |
| 52 | + """BuilderConfig for BookCorpus. |
| 53 | + Args: |
| 54 | + **kwargs: keyword arguments forwarded to super. |
| 55 | + """ |
| 56 | + super(BookcorpusConfig, self).__init__( |
| 57 | + version=nlp.Version("1.0.0", "New split API (https://tensorflow.org/datasets/splits)"), **kwargs |
| 58 | + ) |
| 59 | + |
| 60 | +class Bookcorpus(nlp.GeneratorBasedBuilder): |
| 61 | + """BookCorpus dataset.""" |
| 62 | + |
| 63 | + BUILDER_CONFIGS = [BookcorpusConfig(name="plain_text", description="Plain text",)] |
| 64 | + |
| 65 | + def _info(self): |
| 66 | + return nlp.DatasetInfo( |
| 67 | + description=_DESCRIPTION, |
| 68 | + features=nlp.Features( |
| 69 | + {"text": nlp.Value("string"),} |
| 70 | + ), |
| 71 | + supervised_keys=None, |
| 72 | + homepage="https://yknzhu.wixsite.com/mbweb", |
| 73 | + citation=_CITATION, |
| 74 | + ) |
| 75 | + |
| 76 | + def _vocab_text_gen(self, archive): |
| 77 | + for _, ex in self._generate_examples(archive): |
| 78 | + yield ex["text"] |
| 79 | + |
| 80 | + def _split_generators(self, dl_manager): |
| 81 | + arch_path = dl_manager.download_and_extract(URL) |
| 82 | + |
| 83 | + return [ |
| 84 | + nlp.SplitGenerator(name=nlp.Split.TRAIN, gen_kwargs={"directory": arch_path}), |
| 85 | + ] |
| 86 | + |
| 87 | + def _generate_examples(self, directory): |
| 88 | + files = [os.path.join(directory, 'books_large_p1.txt'), |
| 89 | + os.path.join(directory, 'books_large_p2.txt'),] |
| 90 | + _id = 0 |
| 91 | + for txt_file in files: |
| 92 | + with open(txt_file, mode="r") as f: |
| 93 | + for line in f: |
| 94 | + yield _id, {'text': line.strip()} |
| 95 | + _id += 1 |
0 commit comments