code: def build_ngrams(sentences, process_func, id_func, ngram_len=4):
      """
      Turn the list of processed trees into a bunch of ngrams
  
      The returned map is from tuple to set of ids
  
      The idea being that this map can be used to search for trees to
      match datasets
      """
      ngram_map = defaultdict(set)
      for sentence in tqdm(sentences, postfix="Extracting ngrams"):
          sentence_id = id_func(sentence)
          ngrams = extract_ngrams(sentence, process_func, ngram_len)
          for ngram in ngrams:
              ngram_map[ngram].add(sentence_id)
      return ngram_map
  
imports: from collections import defaultdict, deque, namedtuple
  import itertools
  import os
  import re
  import sys
  from tqdm import tqdm
  from stanza.models.constituency.tree_reader import read_trees, UnclosedTreeError, ExtraCloseTreeError
  from stanza.server import tsurgeon
  from stanza.utils.conll import CoNLL
  from stanza.utils.datasets.constituency.utils import SHARDS, write_dataset
  import stanza.utils.default_paths
label_desc: Use image node_id 5 for calling a global function with example usage: build_ngrams(sentences, process_func, id_func, ngram_len) and returns: ngram_map