code: def run_translate_restrict(
      data: Dict[str, Any], translate_params: str
  ) -> Dict[str, Any]:
      """
      Runs sockeye.translate with vocabulary selection and checks if number of outputs are the same as without
      vocabulary selection. Adds restricted outputs and scores to the data dictionary.
      """
      translate_mod = sockeye.translate
      out_path = os.path.join(data["work_dir"], "out-restrict.txt")
      out_with_target_prefix_path = os.path.join(
          data["work_dir"], "out-with-target-prefix-restrict.txt"
      )
      # Translate corpus with restrict-lexicon
  
      # First set of params (with target prefix in JSON format)
      params = "{} {} {} {}".format(
          translate_mod.__file__,
          TRANSLATE_PARAMS_COMMON.format(
              model=data["model"],
              input=data["test_source_with_target_prefix"],
              output=out_with_target_prefix_path,
          ),
          translate_params,
          TRANSLATE_PARAMS_RESTRICT.format(
              lexicon=data["lexicon"], topk=1
          ),
      )
      params += TRANSLATE_WITH_JSON_FORMAT
      with patch.object(sys, "argv", params.split()):
          translate_mod.main()
  
      # Collect test translate outputs and scores
      data[
          "test_with_target_prefix_outputs_restricted"
      ] = collect_translate_output_and_scores(
          out_with_target_prefix_path
      )
  
      # Second set of params (without using target prefix)
      params = "{} {} {} {}".format(
          translate_mod.__file__,
          TRANSLATE_PARAMS_COMMON.format(
              model=data["model"],
              input=data["test_source"],
              output=out_path,
          ),
          translate_params,
          TRANSLATE_PARAMS_RESTRICT.format(
              lexicon=data["lexicon"], topk=1
          ),
      )
      if "test_source_factors" in data:
          params += TRANSLATE_WITH_FACTORS_COMMON.format(
              input_factors=" ".join(data["test_source_factors"])
          )
      with patch.object(sys, "argv", params.split()):
          translate_mod.main()
  
      # Collect test translate outputs and scores
      data[
          "test_outputs_restricted"
      ] = collect_translate_output_and_scores(out_path)
      assert (
          len(data["test_with_target_prefix_outputs_restricted"])
          == len(data["test_outputs_restricted"])
          == len(data["test_outputs"])
      )
      return data
  
imports: import json
  import logging
  import os
  import random
  import sys
  from contextlib import contextmanager
  from tempfile import TemporaryDirectory
  from typing import Any, Dict, List, Optional
  from unittest.mock import patch
  import sockeye.constants
  import sockeye.prepare_data
  import sockeye.train
  import sockeye.translate
  import sockeye.lexicon
  import sockeye.utils
label_desc: Use image node_id 8 for calling a global function with example usage: run_translate_restrict(data, translate_params) and returns: data