|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2017 The Tensor2Tensor 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 | +"""Data generators for the CNN and Daily Mail datasets.""" |
| 17 | + |
| 18 | +from __future__ import absolute_import |
| 19 | +from __future__ import division |
| 20 | +from __future__ import print_function |
| 21 | + |
| 22 | +import os |
| 23 | +import tarfile |
| 24 | + |
| 25 | +# Dependency imports |
| 26 | + |
| 27 | +import six |
| 28 | +from tensor2tensor.data_generators import generator_utils |
| 29 | +from tensor2tensor.data_generators import problem |
| 30 | +from tensor2tensor.data_generators import text_encoder |
| 31 | +from tensor2tensor.utils import registry |
| 32 | + |
| 33 | +import tensorflow as tf |
| 34 | + |
| 35 | + |
| 36 | +# Links to data from http://cs.nyu.edu/~kcho/DMQA/ |
| 37 | +_CNN_STORIES_DRIVE_URL = "https://drive.google.com/uc?export=download&id=0BwmD_VLjROrfTHk4NFg2SndKcjQ" |
| 38 | + |
| 39 | +_DAILYMAIL_STORIES_DRIVE_URL = "https://drive.google.com/uc?export=download&id=0BwmD_VLjROrfM1BxdkxVaTY2bWs" |
| 40 | + |
| 41 | + |
| 42 | +# End-of-sentence marker. |
| 43 | +EOS = text_encoder.EOS_ID |
| 44 | + |
| 45 | + |
| 46 | +def _maybe_download_corpora(tmp_dir): |
| 47 | + """Download corpora if necessary and unzip them. |
| 48 | +
|
| 49 | + Args: |
| 50 | + tmp_dir: directory containing dataset. |
| 51 | +
|
| 52 | + Returns: |
| 53 | + filepath of the downloaded corpus file. |
| 54 | + """ |
| 55 | + cnn_filename = "cnn_stories.tgz" |
| 56 | + dailymail_filename = "dailymail_stories.tgz" |
| 57 | + cnn_finalpath = os.path.join(tmp_dir, "cnn/stories/") |
| 58 | + dailymail_finalpath = os.path.join(tmp_dir, "dailymail/stories/") |
| 59 | + if not tf.gfile.Exists(cnn_finalpath): |
| 60 | + cnn_file = generator_utils.maybe_download_from_drive( |
| 61 | + tmp_dir, cnn_filename, _CNN_STORIES_DRIVE_URL) |
| 62 | + with tarfile.open(cnn_file, "r:gz") as cnn_tar: |
| 63 | + cnn_tar.extractall(tmp_dir) |
| 64 | + if not tf.gfile.Exists(dailymail_finalpath): |
| 65 | + dailymail_file = generator_utils.maybe_download_from_drive( |
| 66 | + tmp_dir, dailymail_filename, _CNN_STORIES_DRIVE_URL) |
| 67 | + with tarfile.open(dailymail_file, "r:gz") as dailymail_tar: |
| 68 | + dailymail_tar.extractall(tmp_dir) |
| 69 | + return [cnn_finalpath, dailymail_finalpath] |
| 70 | + |
| 71 | + |
| 72 | +def story_generator(tmp_dir): |
| 73 | + paths = _maybe_download_corpora(tmp_dir) |
| 74 | + for path in paths: |
| 75 | + for story_file in tf.gfile.Glob(path + "*"): |
| 76 | + story = u"" |
| 77 | + for line in tf.gfile.Open(story_file): |
| 78 | + line = unicode(line, "utf-8") if six.PY2 else line.decode("utf-8") |
| 79 | + story += line |
| 80 | + yield story |
| 81 | + |
| 82 | + |
| 83 | +def _story_summary_split(story): |
| 84 | + end_pos = story.find("\n\n") # Upto first empty line. |
| 85 | + assert end_pos != -1 |
| 86 | + return story[:end_pos], story[end_pos:].strip() |
| 87 | + |
| 88 | + |
| 89 | +@registry.register_problem |
| 90 | +class SummarizeCnnDailymail32k(problem.Text2TextProblem): |
| 91 | + """Summarize CNN and Daily Mail articles to their first paragraph.""" |
| 92 | + |
| 93 | + @property |
| 94 | + def is_character_level(self): |
| 95 | + return False |
| 96 | + |
| 97 | + @property |
| 98 | + def has_inputs(self): |
| 99 | + return True |
| 100 | + |
| 101 | + @property |
| 102 | + def input_space_id(self): |
| 103 | + return problem.SpaceID.EN_TOK |
| 104 | + |
| 105 | + @property |
| 106 | + def target_space_id(self): |
| 107 | + return problem.SpaceID.EN_TOK |
| 108 | + |
| 109 | + @property |
| 110 | + def num_shards(self): |
| 111 | + return 100 |
| 112 | + |
| 113 | + @property |
| 114 | + def vocab_name(self): |
| 115 | + return "vocab.cnndailymail" |
| 116 | + |
| 117 | + @property |
| 118 | + def use_subword_tokenizer(self): |
| 119 | + return True |
| 120 | + |
| 121 | + @property |
| 122 | + def targeted_vocab_size(self): |
| 123 | + return 2**15 # 32768 |
| 124 | + |
| 125 | + @property |
| 126 | + def use_train_shards_for_dev(self): |
| 127 | + return True |
| 128 | + |
| 129 | + def generator(self, data_dir, tmp_dir, _): |
| 130 | + encoder = generator_utils.get_or_generate_vocab_inner( |
| 131 | + data_dir, self.vocab_file, self.targeted_vocab_size, |
| 132 | + lambda: story_generator(tmp_dir)) |
| 133 | + for story in story_generator(tmp_dir): |
| 134 | + summary, rest = _story_summary_split(story) |
| 135 | + encoded_summary = encoder.encode(summary) + [EOS] |
| 136 | + encoded_story = encoder.encode(rest) + [EOS] |
| 137 | + yield {"inputs": encoded_story, "targets": encoded_summary} |
0 commit comments