2020 the huggingface team 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 orglicenseslicense2 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 kwargs will be passed to calculaterouge predlns x strip for x in openpredpath readlines tgtlns x strip for x in opentgtpath readlines lenpredlns metrics calculaterougepredlns tgtlns kwargs if savepath is not none savejsonmetrics savepath indentnone return metrics these print nicely if name main fire firecalculaterougepath 2020 the huggingface team 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 kwargs will be passed to calculate_rouge these print nicely
import fire

from utils import calculate_rouge, save_json


def calculate_rouge_path(pred_path, tgt_path, save_path=None, **kwargs):
    
    pred_lns = [x.strip() for x in open(pred_path).readlines()]
    tgt_lns = [x.strip() for x in open(tgt_path).readlines()][: len(pred_lns)]
    metrics = calculate_rouge(pred_lns, tgt_lns, **kwargs)
    if save_path is not None:
        save_json(metrics, save_path, indent=None)
    return metrics  


if __name__ == "__main__":
    fire.Fire(calculate_rouge_path)