code: def _iter_translations(
      args,
      task,
      dataset,
      translations,
      align_dict,
      rescorer,
      modify_target_dict,
  ):
      """Iterate over translations.
  
      This is a generator function which wraps the beam-search sequence generator,
      performing such work on the output as converting token indices to
      strings, printing output where applicable (not args.quiet), collecting
      oracle translations where applicable, and removing language-ID tokens
      for multilingual translation.
  
      Args:
          args: Command-line arguments.
          task: FairseqTask object.
          dataset: Dataset set object for a specific split.
          translations: Batched translation iterator, as returned by
              SequenceGenerator.generate_batched_itr().
          align_dict: Dictionary for UNK replacement.
  
      Yields:
          For each sentence in `translations`, yields a TranslationInfo.
      """
      is_multilingual = (
          pytorch_translate_data.is_multilingual_many_to_one(args)
      )
  
      for sample_id, src_tokens, target_tokens, hypos in translations:
          # Process input and ground truth
          target_tokens = target_tokens.int().cpu()
  
          if is_multilingual:
              src_lang_id = (
                  src_tokens[-1]
                  - pytorch_translate_data.MULTILING_DIALECT_ID_OFFSET
              )
              target_lang_id = (
                  target_tokens[0]
                  - pytorch_translate_data.MULTILING_DIALECT_ID_OFFSET
              )
  
              # remove language ID tokens
              src_tokens = src_tokens[:-1]
              target_tokens = target_tokens[1:]
  
              # Select dictionaries
              src_dict = task.source_dictionaries[
                  task.get_encoder_lang_code(src_lang_id)
              ]
              target_dict = task.target_dictionaries[
                  task.get_decoder_lang_code(target_lang_id)
              ]
          else:
              src_dict = task.source_dictionary
              target_dict = task.target_dictionary
  
          # Either retrieve the original sentences or regenerate them from tokens.
          if align_dict is not None:
              src_str = dataset.src.get_original_text(sample_id)
              target_str = dataset.tgt.get_original_text(sample_id)
          else:
              src_str = src_dict.string(src_tokens, args.post_process)
              target_str = target_dict.string(
                  target_tokens, args.post_process, escape_unk=True
              )
  
          if not args.quiet:
              print(f"S-{sample_id}\t{src_str}")
              print(f"T-{sample_id}\t{target_str}")
  
          # used for oracle evaluation (args.report_oracle_bleu)
          best_hypo_tokens = None
          best_hypo_score = 0
          collect_oracle_hypos = args.report_oracle_bleu or (
              args.output_hypos_binary_path and args.nbest > 0
          )
  
          # Process top predictions
          for i, hypo in enumerate(
              hypos[: min(len(hypos), args.nbest)]
          ):
              (
                  hypo_tokens,
                  hypo_str,
                  alignment,
              ) = utils.post_process_prediction(
                  hypo_tokens=hypo["tokens"].int().cpu(),
                  src_str=src_str,
                  alignment=hypo["alignment"].int().cpu()
                  if align_dict is not None
                  else None,
                  align_dict=align_dict,
                  tgt_dict=task.target_dictionary,
                  remove_bpe=args.post_process,
              )
  
              if not args.quiet:
                  print(f"H-{sample_id}\t{hypo['score']}\t{hypo_str}")
                  if alignment is not None:
                      print(
                          "A-{}\t{}".format(
                              sample_id,
                              " ".join(
                                  map(
                                      lambda x: str(utils.item(x)),
                                      alignment,
                                  )
                              ),
                          )
                      )
  
              if collect_oracle_hypos:
                  score = smoothed_sentence_bleu(
                      task, target_tokens, hypo_tokens
                  )
                  if score > best_hypo_score:
                      best_hypo_tokens = hypo_tokens
                      best_hypo_score = score
  
              if i == 0:
                  if (
                      align_dict is not None
                      or args.post_process is not None
                  ):
                      # Convert back to tokens for evaluation with unk replacement
                      # and/or without BPE
                      target_tokens = (
                          task.target_dictionary.encode_line(
                              target_str,
                              add_if_not_exist=modify_target_dict,
                          )
                      )
                  # The probs score for the hypo_str; whether it's normalized by
                  # sequence length or not depends on normalize_scores, which is
                  # set by arg.nonormalize.
                  # However, as I tried, whether normalize_scores is set or not,
                  # the returned scores are the same (to be investigated).
                  # Here, the probs are normalized by hypo length so the value
                  # is big enough to be used as weights for backtranslations in
                  # dual learning.
                  hypo_score = (
                      hypo["score"] / len(hypo_tokens)
                      if len(hypo_tokens) > 0
                      else 0.0
                  )
                  top_hypo_tokens = hypo_tokens
                  top_hypo_str = hypo_str
  
          if not collect_oracle_hypos:
              best_hypo_tokens = top_hypo_tokens
  
          yield TranslationInfo(
              sample_id=sample_id,
              src_tokens=src_tokens,
              target_tokens=target_tokens,
              hypo_tokens=top_hypo_tokens,
              src_str=src_str,
              target_str=target_str,
              hypo_str=top_hypo_str,
              hypo_score=hypo_score,
              best_hypo_tokens=best_hypo_tokens,
              hypos=hypos,
          )
  
imports: import argparse
  import collections
  import os
  import pickle
  from typing import List, NamedTuple, Optional
  import numpy
  import torch
  from fairseq import data, iterative_refinement_generator, options, progress_bar, tasks, utils
  from fairseq.meters import StopwatchMeter, TimeMeter
  from fairseq.models import FairseqEncoderDecoderModel, FairseqMultiModel
  from fairseq.scoring import bleu
  from pytorch_translate import beam_decode, char_aware_hybrid, char_source_hybrid, char_source_model, char_source_transformer_model, hybrid_transformer_rnn, options, rnn, transformer, utils
  from pytorch_translate.constants import CHECKPOINT_PATHS_DELIMITER
  from pytorch_translate.data import data
  from pytorch_translate.dual_learning.dual_learning_models import DualLearningModel
  from pytorch_translate.research.beam_search import competing_completed
  from pytorch_translate.research.multisource import multisource_data, multisource_decode
  from pytorch_translate.tasks.pytorch_translate_multi_task import PyTorchTranslateMultiTask
  from pytorch_translate.tasks.pytorch_translate_task import PytorchTranslateTask
label_desc: Use image node_id 5 for calling a global function with example usage: _iter_translations(args, task, dataset, translations, align_dict, rescorer, modify_target_dict) without return types