code: def score_target_hypo(
      args,
      a,
      b,
      c,
      lenpen,
      target_outfile,
      hypo_outfile,
      write_hypos,
      normalize,
  ):
      print("lenpen", lenpen, "weight1", a, "weight2", b, "weight3", c)
      (
          gen_output_lst,
          bitext1_lst,
          bitext2_lst,
          lm_res_lst,
      ) = load_score_files(args)
      dict = dictionary.Dictionary()
      scorer = scorer = bleu.Scorer(
          bleu.BleuConfig(
              pad=dict.pad(),
              eos=dict.eos(),
              unk=dict.unk(),
          )
      )
  
      ordered_hypos = {}
      ordered_targets = {}
  
      for shard_id in range(len(bitext1_lst)):
          bitext1 = bitext1_lst[shard_id]
          bitext2 = bitext2_lst[shard_id]
          gen_output = gen_output_lst[shard_id]
          lm_res = lm_res_lst[shard_id]
  
          total = len(bitext1.rescore_source.keys())
          source_lst = []
          hypo_lst = []
          score_lst = []
          reference_lst = []
          j = 1
          best_score = -math.inf
  
          for i in range(total):
              # length is measured in terms of words, not bpe tokens, since models may not share the same bpe
              target_len = len(bitext1.rescore_hypo[i].split())
  
              if lm_res is not None:
                  lm_score = lm_res.score[i]
              else:
                  lm_score = 0
  
              if bitext2 is not None:
                  bitext2_score = bitext2.rescore_score[i]
                  bitext2_backwards = bitext2.backwards
              else:
                  bitext2_score = None
                  bitext2_backwards = None
  
              score = rerank_utils.get_score(
                  a,
                  b,
                  c,
                  target_len,
                  bitext1.rescore_score[i],
                  bitext2_score,
                  lm_score=lm_score,
                  lenpen=lenpen,
                  src_len=bitext1.source_lengths[i],
                  tgt_len=bitext1.target_lengths[i],
                  bitext1_backwards=bitext1.backwards,
                  bitext2_backwards=bitext2_backwards,
                  normalize=normalize,
              )
  
              if score > best_score:
                  best_score = score
                  best_hypo = bitext1.rescore_hypo[i]
  
              if j == gen_output.num_hypos[i] or j == args.num_rescore:
                  j = 1
                  hypo_lst.append(best_hypo)
                  score_lst.append(best_score)
                  source_lst.append(bitext1.rescore_source[i])
                  reference_lst.append(bitext1.rescore_target[i])
  
                  best_score = -math.inf
                  best_hypo = ""
              else:
                  j += 1
  
          gen_keys = list(sorted(gen_output.no_bpe_target.keys()))
  
          for key in range(len(gen_keys)):
              if args.prefix_len is None:
                  assert (
                      hypo_lst[key]
                      in gen_output.no_bpe_hypo[gen_keys[key]]
                  ), (
                      "pred and rescore hypo mismatch: i: "
                      + str(key)
                      + ", "
                      + str(hypo_lst[key])
                      + str(gen_keys[key])
                      + str(gen_output.no_bpe_hypo[key])
                  )
                  sys_tok = dict.encode_line(hypo_lst[key])
                  ref_tok = dict.encode_line(
                      gen_output.no_bpe_target[gen_keys[key]]
                  )
                  scorer.add(ref_tok, sys_tok)
  
              else:
                  full_hypo = rerank_utils.get_full_from_prefix(
                      hypo_lst[key],
                      gen_output.no_bpe_hypo[gen_keys[key]],
                  )
                  sys_tok = dict.encode_line(full_hypo)
                  ref_tok = dict.encode_line(
                      gen_output.no_bpe_target[gen_keys[key]]
                  )
                  scorer.add(ref_tok, sys_tok)
  
          # if only one set of hyper parameters is provided, write the predictions to a file
          if write_hypos:
              # recover the orinal ids from n best list generation
              for key in range(len(gen_output.no_bpe_target)):
                  if args.prefix_len is None:
                      assert (
                          hypo_lst[key]
                          in gen_output.no_bpe_hypo[gen_keys[key]]
                      ), (
                          "pred and rescore hypo mismatch:"
                          + "i:"
                          + str(key)
                          + str(hypo_lst[key])
                          + str(gen_output.no_bpe_hypo[key])
                      )
                      ordered_hypos[gen_keys[key]] = hypo_lst[key]
                      ordered_targets[
                          gen_keys[key]
                      ] = gen_output.no_bpe_target[gen_keys[key]]
  
                  else:
                      full_hypo = rerank_utils.get_full_from_prefix(
                          hypo_lst[key],
                          gen_output.no_bpe_hypo[gen_keys[key]],
                      )
                      ordered_hypos[gen_keys[key]] = full_hypo
                      ordered_targets[
                          gen_keys[key]
                      ] = gen_output.no_bpe_target[gen_keys[key]]
  
      # write the hypos in the original order from nbest list generation
      if args.num_shards == (len(bitext1_lst)):
          with open(target_outfile, "w") as t:
              with open(hypo_outfile, "w") as h:
                  for key in range(len(ordered_hypos)):
                      t.write(ordered_targets[key])
                      h.write(ordered_hypos[key])
  
      res = scorer.result_string(4)
      if write_hypos:
          print(res)
      score = rerank_utils.parse_bleu_scoring(res)
      return score
  
file_path: fairseq/examples/noisychannel/rerank.py
imports: import math
  from multiprocessing import Pool
  import numpy
  from fairseq import options
  from fairseq.data import dictionary
  from fairseq.scoring import bleu
  from examples.noisychannel import rerank_generate, rerank_options, rerank_score_bw, rerank_score_lm, rerank_utils
label_desc: Use image node_id 1 for calling a global function with example usage: score_target_hypo(args, a, b, c, lenpen, target_outfile, hypo_outfile, write_hypos, normalize) and returns: score