code: def find_ngrams(token_dict, text, n):
      """
      Break text into ngrams that appear in ``token_dict``.
  
      :param token_dict:
          ``dict`` to check for ngrams
      :param text:
          ``str`` to look for ngrams in
      :param n:
          ``int`` max size of ngrams
      """
      # base case
      if n <= 1:
          return text
      # tokens committed to output
      saved_tokens = []
      # tokens remaining to be searched in sentence
      search_tokens = text[:]
      # tokens stored until next ngram found
      next_search = []
      while len(search_tokens) >= n:
          ngram = " ".join(search_tokens[:n])
          if ngram in token_dict:
              # first, search previous unmatched words for smaller ngrams
              sub_n = min(len(next_search), n - 1)
              saved_tokens.extend(
                  find_ngrams(token_dict, next_search, sub_n)
              )
              next_search.clear()
              # then add this ngram
              saved_tokens.append(ngram)
              # then pop this ngram from the remaining words to search
              search_tokens = search_tokens[n:]
          else:
              next_search.append(search_tokens.pop(0))
      remainder = next_search + search_tokens
      sub_n = min(len(remainder), n - 1)
      saved_tokens.extend(find_ngrams(token_dict, remainder, sub_n))
      return saved_tokens
  
imports: from typing import Optional
  from parlai.core.params import ParlaiParser
  from parlai.core.opt import Opt
  from parlai.core.build_data import modelzoo_path
  from parlai.utils.bpe import bpe_factory, BPEHelper
  from .agents import Agent
  from .build_data import make_dir
  from collections import defaultdict
  import copy
  import numpy
  import os
  import json
  import re
  import parlai.utils.logging
  from parlai.utils.io import PathManager
  from typing import List
  import enum
label_desc: Use image node_id 3 for calling a global function with example usage: find_ngrams(token_dict, text, n) and returns: saved_tokens, text