code: def get_translation_candidates(
      src_dict,
      dst_dict,
      lexical_dictionaries,
      num_top_words,
      max_translation_candidates_per_word,
  ):
      """
      Reads a lexical dictionary file, where each line is (source token, possible
      translation of source token, probability). The file is generally grouped
      by source tokens, but within the group, the probabilities are not
      necessarily sorted.
  
      A a 0.3
      A c 0.1
      A e 0.05
      A f 0.01
      B b 0.6
      B b 0.2
      A z 0.001
      A y 0.002
      ...
  
      Returns: translation_candidates
          Matrix of shape (src_dict, max_translation_candidates_per_word) where
          each row corresponds to a source word in the vocab and contains token
          indices of translation candidates for that source word
      """
  
      translation_candidates = np.zeros(
          [len(src_dict), max_translation_candidates_per_word],
          dtype=np.int32,
      )
  
      # running count of translation candidates per source word
      counter_per_word = np.zeros(len(src_dict), dtype=np.int32)
  
      # tracks if we've already seen some (source token, target token) pair so we
      # ignore duplicate lines
      translation_candidates_set = set()
  
      for lexical_dictionary in lexical_dictionaries:
          logger.info(
              f"Processing dictionary file {lexical_dictionary}"
          )
          translation_candidates_saved = 0
  
          with codecs.open(
              lexical_dictionary, "r", "utf-8"
          ) as lexical_dictionary_file:
              current_source_index = None
              current_target_indices = []
              for line in lexical_dictionary_file.readlines():
                  alignment_data = line.split()
                  if len(alignment_data) != 3:
                      logger.warning(
                          f"Malformed line in lexical dictionary: {line}"
                      )
                      continue
                  source_word, target_word, prob = alignment_data
                  prob = float(prob)
                  source_index = src_dict.index(source_word)
                  target_index = dst_dict.index(target_word)
                  if (
                      source_index not in src_dict.lexicon_indices
                      and target_index in dst_dict.lexicon_indices
                  ):
                      continue
  
                  if (
                      source_index is not None
                      and target_index is not None
                  ):
                      if source_index != current_source_index:
                          # We've finished processing the possible translation
                          # candidates for this source token group, so save the
                          # extracted translation candidates
                          translation_candidates_saved += (
                              select_top_candidate_per_word(
                                  current_source_index,
                                  current_target_indices,
                                  counter_per_word,
                                  max_translation_candidates_per_word,
                                  translation_candidates,
                                  translation_candidates_set,
                              )
                          )
                          current_source_index = source_index
                          current_target_indices = []
  
                      if (
                          target_index >= num_top_words
                          and (source_index, target_index)
                          not in translation_candidates_set
                      ):
                          current_target_indices.append(
                              (target_index, prob)
                          )
          # Save the extracted translation candidates for the last source token
          # group
          translation_candidates_saved += select_top_candidate_per_word(
              current_source_index,
              current_target_indices,
              counter_per_word,
              max_translation_candidates_per_word,
              translation_candidates,
              translation_candidates_set,
          )
          logger.info(
              f"Loaded {translation_candidates_saved} translation"
              f"candidates from dictionary {lexical_dictionary}"
          )
      return translation_candidates
  
file_path: translate/pytorch_translate/vocab_reduction.py
imports: import codecs
  import logging
  import numpy
  import torch
  import torch.nn
label_desc: Use image node_id 4 for calling a global function with example usage: get_translation_candidates(src_dict, dst_dict, lexical_dictionaries, num_top_words, max_translation_candidates_per_word) and returns: translation_candidates