o
    5ti                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddlZddl	m
Z
 ddl	mZ 		dd
dZdd ZdddZdd Zdd Zdd Zdd ZdS )z2Library for reading/writing input and score files.    )absolute_import)division)print_functionN)logging)zip)zip_longest
c           
      C   s   t | }t |}t|dk st|t|kr$tdt|| t||f t||||}|r@|D ]}	||	 q/t||  dS t|| dS )a  Runs aggregate score calculations and outputs results to a CSV file.

  Args:
    target_filepattern: Pattern for files containing target text.
    prediction_filepattern: Pattern for files containing prediction text.
    output_filename: Name of file to write results to.
    scorer: A BaseScorer object to compute scores.
    aggregator: An aggregator to aggregate scores. If None, outputs are
      per-example scores.
    delimiter: Record delimiter.
     zzMust have equal and positive number of target and prediction files. Found: %d target files (%s), %d prediction files (%s).N)_globlen
ValueError_compute_scores
add_scores_write_aggregates_to_csv	aggregate_write_scores_to_csv)
target_filepatternprediction_filepatternoutput_filenamescorer
aggregator	delimitertarget_filenamesprediction_filenamesscoresscore r   B/home/ubuntu/.local/lib/python3.10/site-packages/rouge_score/io.pycompute_scores_and_write_to_csv   s"   r   c                 C   s
   t  | S N)glob)filepatternr   r   r   r
   D      
r
   rc                 C   s
   t | |S r   )open)r!   moder   r   r   _openH   r"   r&   c                 c   sr    t | }| t|}W d   n1 sw   Y  |d r)td n|dd }|D ]}|V  q1dS )z5Opens file and yields records separated by delimiter.Nz!Expected delimiter at end of file)r&   readsplitsix
ensure_strr   warn)filenamer   frecordsrecordr   r   r   _record_genL   s   
r1   c                 C   s   g }t t| t|D ]=\}}td| td| t||}t||}t||D ]\}	}
|	du s6|
du r>td||f |||	|
 q*q|S )a  Computes aggregates scores across the given target and prediction files.

  Args:
    target_filenames: List of filenames from which to read target lines.
    prediction_filenames: List of filenames from which to read prediction lines.
    scorer: A BaseScorer object to compute scores.
    delimiter: string delimiter between each record in input files

  Returns:
    A list of dicts mapping score_type to Score objects.
  Raises:
    ValueError: If invalid targets or predictions are provided.
  zReading targets from %s.zReading predictions from %s.NzcMust have equal number of lines across target and prediction files. Mismatch between files: %s, %s.)	r   sortedr   infor1   r   r   appendr   )r   r   r   r   r   target_filenameprediction_filenametargetspreds
target_recprediction_recr   r   r   r   Z   s    

r   c              	   C   s   t d|  t| dK}|d t| D ]7\}}|d||jj|jj|j	jf  |d||jj
|jj
|j	j
f  |d||jj|jj|j	jf  qW d   n1 sYw   Y  t d dS )	au  Writes aggregate scores to an output CSV file.

  Output file is a comma separated where each line has the format:
    score_type-(P|R|F),low_ci,mean,high_ci

  P/R/F indicates whether the score is a precision, recall or f-measure.

  Args:
    output_filename: Name of file to write results to.
    aggregates: A dict mapping each score_type to a AggregateScore object.
  Writing results to %s.wzscore_type,low,mid,high
z%s-R,%f,%f,%f
z%s-P,%f,%f,%f
z%s-F,%f,%f,%f
NFinished writing results.)r   r3   r&   writer2   itemslowrecallmidhigh	precisionfmeasure)r   
aggregatesoutput_file
score_typer   r   r   r   r   z   s,   
r   c              
   C   s   t |dk rtd dS t|d  }td|  t| dM}|d |D ]}|dj|d	 q(|d
 t	|D ](\}}|d|  |D ]}|d|| j
|| j|| jf  qJ|d
 q=W d   n1 spw   Y  td dS )ai  Writes scores for each individual example to an output CSV file.

  Output file is a comma separated where each line has the format:
    id,score1,score2,score3,...

  The header row indicates the type of each score column.

  Args:
    output_filename: Name of file to write results to.
    scores: A list of dicts mapping each score_type to a Score object.
  r	   zNo scores to writeNr   r;   r<   idz,{t}-P,{t}-R,{t}-F)tr   z%dz	,%f,%f,%fr=   )r   r   r,   r2   keysr3   r&   r>   format	enumeraterD   rA   rE   )r   r   rouge_typesout_file
rouge_typeiresultr   r   r   r      s,   


r   )r   )r#   )__doc__
__future__r   r   r   r    abslr   r*   	six.movesr   r   r   r
   r&   r1   r   r   r   r   r   r   r   <module>   s"   	
&
 