code: def rerank(args: argparse.Namespace):
      """
      Reranks a list of hypotheses according to a sentence-level metric.
      Writes all output to STDOUT.
  
      :param args: Namespace object holding CLI arguments.
      """
      reranker = Reranker(
          args.metric, args.isometric_alpha, args.return_score
      )
      output_stream = (
          sys.stdout
          if args.output is None
          else utils.smart_open(args.output, mode="w")
      )
      logger.info(
          "Hypotheses re-ranking using criterion: '%s' " % args.metric
      )
  
      with utils.smart_open(
          args.reference
      ) as reference, utils.smart_open(args.hypotheses) as hypotheses:
          for i, (reference_line, hypothesis_line) in enumerate(
              zip(reference, hypotheses), 1
          ):
              reference = reference_line.strip()
              # Expects a JSON object with keys containing at least 'translations',
              # as returned by sockeye.translate's nbest output
              hypotheses = json.loads(hypothesis_line.strip())
              utils.check_condition(
                  "translations" in hypotheses,
                  "Reranking requires nbest JSON input with 'translations' key present.",
              )
              num_hypotheses = len(hypotheses["translations"])
  
              if not num_hypotheses > 1:
                  logger.info(
                      "Line %d contains %d hypotheses. Nothing to rerank.",
                      i,
                      num_hypotheses,
                  )
                  reranked_hypotheses = hypotheses
              else:
                  reranked_hypotheses = reranker.rerank(
                      hypotheses, reference
                  )
  
              if args.output_best:
                  best_hypothesis = (
                      reranked_hypotheses["translations"][0]
                      if num_hypotheses
                      else ""
                  )
  
                  if (
                      not best_hypothesis
                      and args.output_reference_instead_of_blank
                  ):
                      logger.warning(
                          "Line %d: replacing blank hypothesis with reference.",
                          i,
                      )
                      best_hypothesis = reference
  
                  # get best non-blank hypothesis, when reference is not used
                  if (
                      not best_hypothesis
                      and args.output_best_non_blank
                      and num_hypotheses > 1
                  ):
                      for h in range(num_hypotheses):
                          best_hypothesis = reranked_hypotheses[
                              "translations"
                          ][h]
  
                          if not best_hypothesis:
                              continue
                          else:
                              logger.warning(
                                  "Line %d: blank hypothesis replaced by line [%d] non-blank "
                                  "hypothesis: %s .",
                                  h - 1,
                                  h,
                                  best_hypothesis,
                              )
                              break
  
                  print(best_hypothesis, file=output_stream)
              else:
                  print(
                      json.dumps(reranked_hypotheses, sort_keys=True),
                      file=output_stream,
                  )
  
      if output_stream is not sys.stdout:
          output_stream.close()
  
file_path: sockeye/sockeye/rerank.py
imports: import argparse
  from functools import partial
  import json
  import logging
  import sys
  from typing import Any, Dict, List
  import numpy
  import sacrebleu
  from .None import arguments
  from .None import constants
  from .None import log
  from .None import utils
label_desc: Use image node_id 1 for calling a global function with example usage: rerank(args) without return types