code: def PruneToIntermediateTarget(self, num_extra_ngrams):
      protected_ngrams = self.GetProtectedNgrams()
      initial_num_extra_ngrams = self.GetNumExtraNgrams()
      num_ngrams_to_prune = initial_num_extra_ngrams - num_extra_ngrams
      assert num_ngrams_to_prune > 0
  
      num_candidates_per_order = [0] * args.ngram_order
      num_pruned_per_order = [0] * args.ngram_order
  
      # like_change_and_ngrams this will be a list of tuples consisting
      # of the likelihood change as a float and then the words of the n-gram
      # that we're considering pruning,
      # e.g. (-0.164, 7, 8, 9)
      # meaning that pruning the n-gram (7, 8) -> 9 leads to
      # a likelihood change of -0.164.  We'll later sort this list
      # so we can prune the n-grams that made the least-negative
      # likelihood change.
      like_change_and_ngrams = []
      for n in range(args.no_backoff_ngram_order, args.ngram_order):
          for hist, counts_for_hist in self.counts[n].items():
              for word, count in counts_for_hist.word_to_count.items():
                  if word != self.backoff_symbol:
                      if not hist + (word,) in protected_ngrams:
                          like_change = (
                              self.GetLikeChangeFromPruningNgram(
                                  hist, word
                              )
                          )
                          like_change_and_ngrams.append(
                              (like_change,) + hist + (word,)
                          )
                          num_candidates_per_order[len(hist)] += 1
  
      like_change_and_ngrams.sort(reverse=True)
  
      if num_ngrams_to_prune > len(like_change_and_ngrams):
          print(
              "make_phone_lm.py: aimed to prune {0} n-grams but could only "
              "prune {1}".format(
                  num_ngrams_to_prune, len(like_change_and_ngrams)
              ),
              file=sys.stderr,
          )
          num_ngrams_to_prune = len(like_change_and_ngrams)
  
      total_loglike_change = 0.0
  
      for i in range(num_ngrams_to_prune):
          total_loglike_change += like_change_and_ngrams[i][0]
          hist = like_change_and_ngrams[i][
              1:-1
          ]  # all but 1st and last elements
          word = like_change_and_ngrams[i][-1]  # last element
          num_pruned_per_order[len(hist)] += 1
          self.PruneNgram(hist, word)
  
      like_change_per_word = total_loglike_change / self.total_num_words
  
      if args.verbose >= 1:
          effective_threshold = (
              like_change_and_ngrams[num_ngrams_to_prune - 1][0]
              if num_ngrams_to_prune >= 0
              else 0.0
          )
          print(
              "Pruned from {0} ngrams to {1}, with threshold {2}.  Candidates per order were {3}, "
              "num-ngrams pruned per order were {4}.  Like-change per word was {5}".format(
                  initial_num_extra_ngrams,
                  initial_num_extra_ngrams - num_ngrams_to_prune,
                  "%.4f" % effective_threshold,
                  num_candidates_per_order,
                  num_pruned_per_order,
                  like_change_per_word,
              ),
              file=sys.stderr,
          )
  
      if args.verbose >= 3:
          print(
              "Pruning: like_change_and_ngrams is:\n"
              + "\n".join(
                  [
                      str(x)
                      for x in like_change_and_ngrams[
                          :num_ngrams_to_prune
                      ]
                  ]
              )
              + "\n-------- stop pruning here: ----------\n"
              + "\n".join(
                  [
                      str(x)
                      for x in like_change_and_ngrams[
                          num_ngrams_to_prune:
                      ]
                  ]
              ),
              file=sys.stderr,
          )
          self.Print(
              "Counts after pruning to num-extra-ngrams={0}".format(
                  initial_num_extra_ngrams - num_ngrams_to_prune
              )
          )
  
      self.PruneEmptyStates()
      if args.verbose >= 3:
          ngram_counts.Print(
              "Counts after removing empty states [inside pruning algorithm]:"
          )
      return like_change_per_word
  
imports: import argparse
  import math
  import sys
  from collections import defaultdict
label_desc: Use image node_id 16 for calling the NgramCounts obj's underlying member method code with example usage: obj.PruneToIntermediateTarget(num_extra_ngrams) and returns: like_change_per_word