Example #1
0
def generate_src_word_accuracy_report(ref, outs, src, ref_align_file=None, out_align_files=None,
                          acc_type='fmeas', bucket_type='freq', bucket_cutoffs=None,
                          freq_count_file=None, freq_corpus_file=None,
                          label_set=None,
                          src_labels=None,
                          title=None,
                          case_insensitive=False):
  """
  Generate a report for source word analysis in both plain text and graphs.

  Args:
    ref: Tokens from the reference
    outs: Tokens from the output file(s)
    src: Tokens from the source
    ref_align_file: Alignment file for the reference
    out_align_files: Alignment file for the output file
    acc_type: The type of accuracy to show (prec/rec/fmeas). Can also have multiple separated by '+'.
    bucket_type: A string specifying the way to bucket words together to calculate F-measure (freq/tag)
    bucket_cutoffs: The boundaries between buckets, specified as a colon-separated string.
    freq_corpus_file: When using "freq" as a bucketer, which corpus to use to calculate frequency.
                      By default this uses the frequency in the reference test set, but it's often more informative
                      se the frequency in the training set, in which case you specify the path of the target side
                      he training corpus.
    freq_count_file: An alternative to freq_corpus that uses a count file in "word\tfreq" format.
    src_labels: either a filename of a file full of source labels, or a list of strings corresponding to `ref`.
    title: A string specifying the caption of the printed table
    case_insensitive: A boolean specifying whether to turn on the case insensitive option
  """
  case_insensitive = True if case_insensitive == 'True' else False

  if not src or not ref_align_file or not out_align_files:
    raise ValueError("Must specify the source and the alignment files when performing source analysis.")

  ref_align = corpus_utils.load_tokens(ref_align_file) 
  out_aligns = [corpus_utils.load_tokens(x) for x in arg_utils.parse_files(out_align_files)]

  if len(out_aligns) != len(outs):
    raise ValueError(f'The number of output files should be equal to the number of output alignment files.')

  bucketer = bucketers.create_word_bucketer_from_profile(bucket_type,
                                                         bucket_cutoffs=bucket_cutoffs,
                                                         freq_count_file=freq_count_file,
                                                         freq_corpus_file=freq_corpus_file,
                                                         freq_data=src,
                                                         label_set=label_set,
                                                         case_insensitive=case_insensitive)
  src_labels = corpus_utils.load_tokens(src_labels) if type(src_labels) == str else src_labels
  matches = [bucketer.calc_source_bucketed_matches(src, ref, out, ref_align, out_align, src_labels=src_labels) for out, out_align in zip(outs, out_aligns)]

  reporter = reporters.WordReport(bucketer=bucketer, matches=matches,
                                  acc_type=acc_type, header="Source Word Accuracy Analysis", 
                                  title=title)
  reporter.generate_report(output_fig_file=f'src-word-acc',
                           output_fig_format='pdf', 
                           output_directory='outputs')
  return reporter 
Example #2
0
def generate_word_accuracy_report(ref, outs,
                          acc_type='fmeas', bucket_type='freq', bucket_cutoffs=None,
                          freq_count_file=None, freq_corpus_file=None,
                          label_set=None,
                          ref_labels=None, out_labels=None,
                          title=None,
                          case_insensitive=False):
  """
  Generate a report comparing the word accuracy in both plain text and graphs.

  Args:
    ref: Tokens from the reference
    outs: Tokens from the output file(s)
    acc_type: The type of accuracy to show (prec/rec/fmeas). Can also have multiple separated by '+'.
    bucket_type: A string specifying the way to bucket words together to calculate F-measure (freq/tag)
    bucket_cutoffs: The boundaries between buckets, specified as a colon-separated string.
    freq_corpus_file: When using "freq" as a bucketer, which corpus to use to calculate frequency.
                      By default this uses the frequency in the reference test set, but it's often more informative
                      to use the frequency in the training set, in which case you specify the path of the
                      training corpus.
    freq_count_file: An alternative to freq_corpus that uses a count file in "word\tfreq" format.
    ref_labels: either a filename of a file full of reference labels, or a list of strings corresponding to `ref`.
    out_labels: output labels. must be specified if ref_labels is specified.
    title: A string specifying the caption of the printed table
    case_insensitive: A boolean specifying whether to turn on the case insensitive option
  """
  case_insensitive = True if case_insensitive == 'True' else False

  if out_labels is not None:
    out_labels = arg_utils.parse_files(out_labels)
    if len(out_labels) != len(outs):
      raise ValueError(f'The number of output files should be equal to the number of output labels.')

  bucketer = bucketers.create_word_bucketer_from_profile(bucket_type,
                                                         bucket_cutoffs=bucket_cutoffs,
                                                         freq_count_file=freq_count_file,
                                                         freq_corpus_file=freq_corpus_file,
                                                         freq_data=ref,
                                                         label_set=label_set,
                                                         case_insensitive=case_insensitive)
  ref_labels = corpus_utils.load_tokens(ref_labels) if type(ref_labels) == str else ref_labels
  out_labels = [corpus_utils.load_tokens(out_labels[i]) if not out_labels is None else None for i in range(len(outs))]
  matches = [bucketer.calc_bucketed_matches(ref, out, ref_labels=ref_labels, out_labels=out_label) for out, out_label in zip(outs, out_labels)]
  
  reporter = reporters.WordReport(bucketer=bucketer, matches=matches,
                                  acc_type=acc_type, header="Word Accuracy Analysis", 
                                  title=title)
  reporter.generate_report(output_fig_file=f'word-acc',
                           output_fig_format='pdf', 
                           output_directory='outputs')
  return reporter 
Example #3
0
def generate_ngram_report(ref, outs,
                       min_ngram_length=1, max_ngram_length=4,
                       report_length=50, alpha=1.0, compare_type='match',
                       ref_labels=None, out_labels=None,
                       compare_directions='0-1',
                       case_insensitive=False):
  """
  Generate a report comparing aggregate n-gram statistics in both plain text and graphs

  Args:
    ref: Tokens from the reference
    outs: Tokens from the output file(s)
    min_ngram_length: minimum n-gram length
    max_ngram_length: maximum n-gram length
    report_length: the number of n-grams to report
    alpha: when sorting n-grams for salient features, the smoothing coefficient. A higher smoothing coefficient
           will result in more frequent phenomena (sometimes this is good).
    compare_type: what type of statistic to compare
                  (match: n-grams that match the reference, over: over-produced ngrams, under: under-produced ngrams)
    ref_labels: either a filename of a file full of reference labels, or a list of strings corresponding to `ref`.
                If specified, will aggregate statistics over labels instead of n-grams.
    out_labels: output labels. must be specified if ref_labels is specified.
    compare_directions: A string specifying which systems to compare
    case_insensitive: A boolean specifying whether to turn on the case insensitive option
  """
  min_ngram_length, max_ngram_length, report_length = int(min_ngram_length), int(max_ngram_length), int(report_length)
  alpha = float(alpha)
  case_insensitive = True if case_insensitive == 'True' else False

  if out_labels is not None:
    out_labels = arg_utils.parse_files(out_labels)
    if len(out_labels) != len(outs):
      raise ValueError(f'The number of output files should be equal to the number of output labels.')

  if type(ref_labels) == str:
    label_files_str = f'    ref_labels={ref_labels},'
    for i, out_label in enumerate(out_labels):
      label_files_str += f' out{i}_labels={out_label},'
    label_files = (label_files_str)
  else:
    label_files = None

  if type(alpha) == str:
    alpha = float(alpha)

  if not type(ref_labels) == str and case_insensitive:
    ref = corpus_utils.lower(ref)
    outs = [corpus_utils.lower(out) for out in outs]

  ref_labels = corpus_utils.load_tokens(ref_labels) if type(ref_labels) == str else ref_labels
  out_labels = [corpus_utils.load_tokens(out_labels[i]) if not out_labels is None else None for i in range(len(outs))]
  totals, matches, overs, unders = zip(*[ngram_utils.compare_ngrams(ref, out, ref_labels=ref_labels, out_labels=out_label,
                                                             min_length=min_ngram_length, max_length=max_ngram_length) for out, out_label in zip(outs, out_labels)])
  direcs = arg_utils.parse_compare_directions(compare_directions)
  scores = []
  for (left, right) in direcs:
    if compare_type == 'match':
      scores.append(stat_utils.extract_salient_features(matches[left], matches[right], alpha=alpha))
    elif compare_type == 'over':
      scores.append(stat_utils.extract_salient_features(overs[left], overs[right], alpha=alpha))
    elif compare_type == 'under':
      scores.append(stat_utils.extract_salient_features(unders[left], unders[right], alpha=alpha))
    else:
      raise ValueError(f'Illegal compare_type "{compare_type}"')
  scorelist = [sorted(score.items(), key=operator.itemgetter(1), reverse=True) for score in scores]

  reporter = reporters.NgramReport(scorelist=scorelist, report_length=report_length,
                                   min_ngram_length=min_ngram_length, 
                                   max_ngram_length=max_ngram_length,
                                   matches=matches,
                                   compare_type=compare_type, alpha=alpha,
                                   compare_directions=direcs,
                                   label_files=label_files)                                   
  reporter.generate_report(output_fig_file=f'ngram-min{min_ngram_length}-max{max_ngram_length}-{compare_type}',
                           output_fig_format='pdf', 
                           output_directory='outputs')
  return reporter 
Example #4
0
def generate_sentence_bucketed_report(ref, outs,
                                   bucket_type='score', bucket_cutoffs=None,
                                   statistic_type='count',
                                   score_measure='bleu',
                                   label_set=None,
                                   ref_labels=None, out_labels=None,
                                   title=None,
                                   case_insensitive=False):
  """
  Generate a report of sentences by bucket in both plain text and graphs

  Args:
    ref: Tokens from the reference
    outs: Tokens from the output file(s)
    bucket_type: The type of bucketing method to use
    score_measure: If using 'score' as either bucket_type or statistic_type, which scorer to use
    ref_labels: either a filename of a file full of reference labels, or a list of strings corresponding to `ref`. Would overwrite out_labels if specified.
    out_labels: output labels. 
    title: A string specifying the caption of the printed table
    case_insensitive: A boolean specifying whether to turn on the case insensitive option
  """
  case_insensitive = True if case_insensitive == 'True' else False

  if ref_labels is not None:
    ref_labels = corpus_utils.load_tokens(ref_labels) if type(ref_labels) == str else ref_labels
    if len(ref_labels) != len(ref):
      raise ValueError(f'The number of labels should be equal to the number of sentences.')

  elif out_labels is not None:
    out_labels = arg_utils.parse_files(out_labels)
    if len(out_labels) != len(outs):
      raise ValueError(f'The number of output files should be equal to the number of output labels.')

    out_labels = [corpus_utils.load_tokens(out_label) if type(out_label) == str else out_label for out_label in out_labels]
    for out, out_label in zip(outs, out_labels):
      if len(out_label) != len(out):
        raise ValueError(f'The number of labels should be equal to the number of sentences.')
    

  bucketer = bucketers.create_sentence_bucketer_from_profile(bucket_type, bucket_cutoffs=bucket_cutoffs,
                                                             score_type=score_measure, label_set=label_set, case_insensitive=case_insensitive)
  bcs = [bucketer.create_bucketed_corpus(out, ref=ref, ref_labels=ref_labels if ref_labels else None, out_labels=out_labels[i] if out_labels else None) for i, out in enumerate(outs)]

  if statistic_type == 'count':
    scorer = None
    aggregator = lambda out,ref: len(out)
  elif statistic_type == 'score':
    scorer = scorers.create_scorer_from_profile(score_measure, case_insensitive=case_insensitive)
    aggregator = lambda out,ref: scorer.score_corpus(ref,out)[0]
  else:
    raise ValueError(f'Illegal statistic_type {statistic_type}')

  stats = [[aggregator(out,ref) for (out,ref) in bc] for bc in bcs]

  reporter = reporters.SentenceReport(bucketer=bucketer,
                                      sys_stats=stats,
                                      statistic_type=statistic_type, scorer=scorer, 
                                      title=title)

  reporter.generate_report(output_fig_file=f'sentence-{statistic_type}-{score_measure}',
                           output_fig_format='pdf', 
                           output_directory='outputs')
  return reporter 
Example #5
0
def generate_sentence_bucketed_report(ref,
                                      outs,
                                      src=None,
                                      bucket_type='score',
                                      bucket_cutoffs=None,
                                      statistic_type='count',
                                      score_measure='sentbleu',
                                      label_set=None,
                                      ref_labels=None,
                                      out_labels=None,
                                      title=None,
                                      case_insensitive=False,
                                      output_bucket_details=False,
                                      to_cache=False,
                                      cache_dicts=None):
    """
  Generate a report of sentences by bucket in both plain text and graphs

  Args:
    ref: Tokens from the reference
    outs: Tokens from the output file(s)
    bucket_type: The type of bucketing method to use
    score_measure: If using 'score' as either bucket_type or statistic_type, which scorer to use
    ref_labels: either a filename of a file full of reference labels, or a list of strings corresponding to `ref`. Would overwrite out_labels if specified.
    out_labels: output labels. 
    title: A string specifying the caption of the printed table
    case_insensitive: A boolean specifying whether to turn on the case insensitive option
    output_bucket_details: A boolean specifying whether to output the number of words in each bucket
    to_cache: Return a list of computed statistics if True
    cache_dicts: A list of dictionaries that store cached statistics for each output
  """
    # check and set parameters
    if type(case_insensitive) == str:
        case_insensitive = True if case_insensitive == 'True' else False
    if type(output_bucket_details) == str:
        output_bucket_details = True if output_bucket_details == 'True' else False

    if ref_labels is not None:
        ref_labels = corpus_utils.load_tokens(ref_labels) if type(
            ref_labels) == str else ref_labels
        if len(ref_labels) != len(ref):
            raise ValueError(
                f'The number of labels should be equal to the number of sentences.'
            )

    elif out_labels is not None:
        out_labels = arg_utils.parse_files(out_labels)
        if len(out_labels) != len(outs):
            raise ValueError(
                f'The number of output files should be equal to the number of output labels.'
            )

        out_labels = [
            corpus_utils.load_tokens(out_label)
            if type(out_label) == str else out_label
            for out_label in out_labels
        ]
        for out, out_label in zip(outs, out_labels):
            if len(out_label) != len(out):
                raise ValueError(
                    f'The number of labels should be equal to the number of sentences.'
                )

    # compute statistics
    bucketer = bucketers.create_sentence_bucketer_from_profile(
        bucket_type,
        bucket_cutoffs=bucket_cutoffs,
        score_type=score_measure,
        label_set=label_set,
        case_insensitive=case_insensitive)

    src = [None for _ in ref] if src is None else src

    if statistic_type == 'count':
        scorer = None
        if bucket_type != 'score' and bucket_type != 'lengthdiff':
            ref = ref_label = None
        aggregator = lambda out, refs, src: len(out)
    elif statistic_type == 'score':
        scorer = scorers.create_scorer_from_profile(
            score_measure, case_insensitive=case_insensitive)
        aggregator = lambda out, ref, src: scorer.score_corpus(ref, out, src)[0
                                                                              ]
    else:
        raise ValueError(f'Illegal statistic_type {statistic_type}')

    cache_key_list = ['stats']
    stats = cache_utils.extract_cache_dicts(cache_dicts, cache_key_list,
                                            len(outs))

    if cache_dicts is None:
        bcs = [
            bucketer.create_bucketed_corpus(
                out,
                ref=ref,
                src=src,
                ref_labels=ref_labels if ref_labels else None,
                out_labels=out_labels[i] if out_labels else None)
            for i, out in enumerate(outs)
        ]
        stats = [[aggregator(out, ref, src) for (out, ref, src) in bc]
                 for bc in bcs]

    if output_bucket_details and statistic_type == 'score':
        bucket_cnt_calculator = lambda out, ref, src: len(out)
        bucket_interval_calculator = lambda out, ref: sign_utils.eval_with_paired_bootstrap(
            ref, [out], src, scorer, None)[1][0]
        if cache_dicts is not None:  # we don't cache bcs
            bcs = [
                bucketer.create_bucketed_corpus(
                    out,
                    ref=ref,
                    src=src,
                    ref_labels=ref_labels if ref_labels else None,
                    out_labels=out_labels[i] if out_labels else None)
                for i, out in enumerate(outs)
            ]
        bucket_cnts = [
            bucket_cnt_calculator(out, ref, src) for (out, ref, src) in bcs[0]
        ]
        bucket_intervals = [[
            bucket_interval_calculator(out, ref, src) for (out, ref, src) in bc
        ] for bc in bcs]
    else:
        bucket_cnts = bucket_intervals = None

    if to_cache:
        cache_dict = cache_utils.return_cache_dict(cache_key_list, [stats])
        return cache_dict

    # generate reports
    reporter = reporters.SentenceReport(bucketer=bucketer,
                                        sys_stats=stats,
                                        statistic_type=statistic_type,
                                        scorer=scorer,
                                        bucket_cnts=bucket_cnts,
                                        bucket_intervals=bucket_intervals,
                                        title=title)

    reporter.generate_report(
        output_fig_file=f'sentence-{statistic_type}-{score_measure}',
        output_fig_format='pdf',
        output_directory='outputs')
    return reporter
Example #6
0
def generate_word_accuracy_report(ref,
                                  outs,
                                  src=None,
                                  acc_type='fmeas',
                                  bucket_type='freq',
                                  bucket_cutoffs=None,
                                  freq_count_file=None,
                                  freq_corpus_file=None,
                                  label_set=None,
                                  ref_labels=None,
                                  out_labels=None,
                                  title=None,
                                  case_insensitive=False,
                                  output_bucket_details=False,
                                  to_cache=False,
                                  cache_dicts=None):
    """
  Generate a report comparing the word accuracy in both plain text and graphs.

  Args:
    ref: Tokens from the reference
    outs: Tokens from the output file(s)
    src: Tokens from the source
    acc_type: The type of accuracy to show (prec/rec/fmeas). Can also have multiple separated by '+'.
    bucket_type: A string specifying the way to bucket words together to calculate F-measure (freq/tag)
    bucket_cutoffs: The boundaries between buckets, specified as a colon-separated string.
    freq_corpus_file: When using "freq" as a bucketer, which corpus to use to calculate frequency.
                      By default this uses the frequency in the reference test set, but it's often more informative
                      to use the frequency in the training set, in which case you specify the path of the
                      training corpus.
    freq_count_file: An alternative to freq_corpus that uses a count file in "word\tfreq" format.
    ref_labels: either a filename of a file full of reference labels, or a list of strings corresponding to `ref`.
    out_labels: output labels. must be specified if ref_labels is specified.
    title: A string specifying the caption of the printed table
    case_insensitive: A boolean specifying whether to turn on the case insensitive option
    output_bucket_details: A boolean specifying whether to output the number of words in each bucket
    to_cache: Return a list of computed statistics if True
    cache_dicts: A list of dictionaries that store cached statistics for each output
  """
    # check and set parameters
    if type(case_insensitive) == str:
        case_insensitive = True if case_insensitive == 'True' else False
    if type(output_bucket_details) == str:
        output_bucket_details = True if output_bucket_details == 'True' else False

    if type(ref_labels) == str:
        ref_labels = corpus_utils.load_tokens(ref_labels)
    if out_labels is not None:
        out_label_files = arg_utils.parse_files(out_labels)
        out_labels = [corpus_utils.load_tokens(x) for x in out_label_files]
        if len(out_labels) != len(outs):
            raise ValueError(
                f'The number of output files should be equal to the number of output labels.'
            )
        for i, (o, ol) in enumerate(zip(outs, out_labels)):
            if len(o) != len(ol):
                raise ValueError(
                    f'The labels in {out_label_files[i]} do not match the length of the output file {outs[i]}.'
                )

    # compute statistics
    bucketer = bucketers.create_word_bucketer_from_profile(
        bucket_type,
        bucket_cutoffs=bucket_cutoffs,
        freq_count_file=freq_count_file,
        freq_corpus_file=freq_corpus_file,
        freq_data=ref,
        label_set=label_set,
        case_insensitive=case_insensitive)

    cache_key_list = [
        'statistics', 'my_ref_total_list', 'my_out_totals_list',
        'my_out_matches_list'
    ]
    statistics, my_ref_total_list, my_out_totals_list, my_out_matches_list = cache_utils.extract_cache_dicts(
        cache_dicts, cache_key_list, len(outs))
    if cache_dicts is None:
        statistics, my_ref_total_list, my_out_totals_list, my_out_matches_list = bucketer.calc_statistics(
            ref, outs, ref_labels=ref_labels, out_labels=out_labels)
    else:
        my_ref_total_list = my_ref_total_list[0]
        my_out_totals_list = list(np.concatenate(my_out_totals_list, 1))
        my_out_matches_list = list(np.concatenate(my_out_matches_list, 1))
    examples = bucketer.calc_examples(len(ref), len(outs), statistics,
                                      my_ref_total_list, my_out_matches_list)

    bucket_cnts, bucket_intervals = bucketer.calc_bucket_details(
        my_ref_total_list, my_out_totals_list,
        my_out_matches_list) if output_bucket_details else (None, None)

    if to_cache:
        cache_dict = cache_utils.return_cache_dict(cache_key_list, [
            statistics, [my_ref_total_list], [my_out_totals_list],
            [my_out_matches_list]
        ])
        return cache_dict

    # generate reports
    reporter = reporters.WordReport(bucketer=bucketer,
                                    statistics=statistics,
                                    examples=examples,
                                    bucket_cnts=bucket_cnts,
                                    bucket_intervals=bucket_intervals,
                                    src_sents=src,
                                    ref_sents=ref,
                                    ref_labels=ref_labels,
                                    out_sents=outs,
                                    out_labels=out_labels,
                                    acc_type=acc_type,
                                    header="Word Accuracy Analysis",
                                    title=title)
    reporter.generate_report(output_fig_file=f'word-acc',
                             output_fig_format='pdf',
                             output_directory='outputs')
    return reporter