This repository has been archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #422 from urvashik/master
Completing the CNN/Dailymail summarization pipeline
- Loading branch information
Showing
4 changed files
with
131 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/bin/bash | ||
|
||
# Path to moses dir | ||
mosesdecoder=$1 | ||
|
||
# Path to file containing gold summaries, one per line | ||
targets_file=$2 | ||
# Path to file containing model generated summaries, one per line | ||
decodes_file=$3 | ||
|
||
# Tokenize. | ||
perl $mosesdecoder/scripts/tokenizer/tokenizer.perl -l en < $targets_file > $targets_file.tok | ||
perl $mosesdecoder/scripts/tokenizer/tokenizer.perl -l en < $decodes_file > $decodes_file.tok | ||
|
||
# Get rouge scores | ||
python get_rouge.py --decodes_filename $decodes_file.tok --targets_filename $targets_file.tok |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# coding=utf-8 | ||
# Copyright 2017 The Tensor2Tensor 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. | ||
|
||
"""Computing rouge scores using pyrouge.""" | ||
|
||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
import os | ||
import logging | ||
import shutil | ||
from tempfile import mkdtemp | ||
from pprint import pprint | ||
|
||
# Dependency imports | ||
from pyrouge import Rouge155 | ||
|
||
import numpy as np | ||
import tensorflow as tf | ||
|
||
FLAGS = tf.flags.FLAGS | ||
|
||
tf.flags.DEFINE_string("decodes_filename", None, "File containing model generated summaries tokenized") | ||
tf.flags.DEFINE_string("targets_filename", None, "File containing model target summaries tokenized") | ||
|
||
def write_to_file(filename, data): | ||
data = ".\n".join(data.split(". ")) | ||
with open(filename, "w") as fp: | ||
fp.write(data) | ||
|
||
def prep_data(decode_dir, target_dir): | ||
with open(FLAGS.decodes_filename, "rb") as fdecodes, open(FLAGS.targets_filename, "rb") as ftargets: | ||
for i, (d, t) in enumerate(zip(fdecodes, ftargets)): | ||
write_to_file(os.path.join(decode_dir, "rouge.%06d.txt" % (i+1)), d) | ||
write_to_file(os.path.join(target_dir, "rouge.A.%06d.txt" % (i+1)), t) | ||
|
||
if (i+1 % 1000) == 0: | ||
tf.logging.into("Written %d examples to file" % i) | ||
|
||
def main(_): | ||
rouge = Rouge155() | ||
rouge.log.setLevel(logging.ERROR) | ||
rouge.system_filename_pattern = "rouge.(\d+).txt" | ||
rouge.model_filename_pattern = "rouge.[A-Z].#ID#.txt" | ||
|
||
tf.logging.set_verbosity(tf.logging.INFO) | ||
|
||
tmpdir = mkdtemp() | ||
tf.logging.info("tmpdir: %s" % tmpdir) | ||
# system = decodes/predictions | ||
system_dir = os.path.join(tmpdir, 'system') | ||
# model = targets/gold | ||
model_dir = os.path.join(tmpdir, 'model') | ||
os.mkdir(system_dir) | ||
os.mkdir(model_dir) | ||
|
||
rouge.system_dir = system_dir | ||
rouge.model_dir = model_dir | ||
|
||
prep_data(rouge.system_dir, rouge.model_dir) | ||
|
||
rouge_scores = rouge.convert_and_evaluate() | ||
rouge_scores = rouge.output_to_dict(rouge_scores) | ||
for prefix in ["rouge_1", "rouge_2", "rouge_l"]: | ||
for suffix in ["f_score", "precision", "recall"]: | ||
key = "_".join([prefix, suffix]) | ||
tf.logging.info("%s: %.4f" % (key, rouge_scores[key])) | ||
|
||
# clean up after pyrouge | ||
shutil.rmtree(tmpdir) | ||
shutil.rmtree(rouge._config_dir) | ||
shutil.rmtree(os.path.split(rouge._system_dir)[0]) | ||
|
||
if __name__=='__main__': | ||
tf.app.run() |