code: def preprocess_examples(
      args, tasks, splits, field, logger=None, train=True
  ):
      min_length = 1
      max_context_length = (
          args.max_train_context_length
          if train
          else args.max_val_context_length
      )
      is_too_long = lambda ex: (
          len(ex.answer) > args.max_answer_length
          or len(ex.context) > max_context_length
      )
      is_too_short = lambda ex: (
          len(ex.answer) < min_length or len(ex.context) < min_length
      )
  
      for task, s in zip(tasks, splits):
          if logger is not None:
              logger.info(f"{task} has {len(s.examples)} examples")
          if "cnn" in task or "dailymail" in task or "imdb" in task:
              for x in s.examples:
                  x.context = x.context[:max_context_length]
  
          if train:
              l = len(s.examples)
              s.examples = [
                  ex for ex in s.examples if not is_too_long(ex)
              ]
              if len(s.examples) < l:
                  if logger is not None:
                      logger.info(
                          f"Filtering out long {task} examples: {l} -> {len(s.examples)}"
                      )
  
              l = len(s.examples)
              s.examples = [
                  ex for ex in s.examples if not is_too_short(ex)
              ]
              if len(s.examples) < l:
                  if logger is not None:
                      logger.info(
                          f"Filtering out short {task} examples: {l} -> {len(s.examples)}"
                      )
  
              l = len(s.examples)
              s.examples = [
                  ex
                  for ex in s.examples
                  if "This page includes the show" not in ex.answer
              ]
              if len(s.examples) < l:
                  if logger is not None:
                      logger.info(
                          f"Filtering {task} examples with a dummy summary: {l} -> {len(s.examples)} "
                      )
  
          if logger is not None:
              context_lengths = [len(ex.context) for ex in s.examples]
              question_lengths = [len(ex.question) for ex in s.examples]
              answer_lengths = [len(ex.answer) for ex in s.examples]
  
              logger.info(
                  f"{task} context lengths (min, mean, max): {np.min(context_lengths)}, {int(np.mean(context_lengths))}, {np.max(context_lengths)}"
              )
              logger.info(
                  f"{task} question lengths (min, mean, max): {np.min(question_lengths)}, {int(np.mean(question_lengths))}, {np.max(question_lengths)}"
              )
              logger.info(
                  f"{task} answer lengths (min, mean, max): {np.min(answer_lengths)}, {int(np.mean(answer_lengths))}, {np.max(answer_lengths)}"
              )
  
          for x in s.examples:
              x.context_question = get_context_question(
                  x, x.context, x.question, field
              )
  
          if logger is not None:
              logger.info("Tokenized examples:")
              for ex in s.examples[:10]:
                  logger.info("Context: " + " ".join(ex.context))
                  logger.info("Question: " + " ".join(ex.question))
                  logger.info(" ".join(ex.context_question))
                  logger.info("Answer: " + " ".join(ex.answer))
  
file_path: decanlp/util.py
imports: from text import torchtext
  import time
  import os
  import sys
  import torch
  import random
  import numpy
label_desc: Use image node_id 2 for calling a global function with example usage: preprocess_examples(args, tasks, splits, field, logger, train) without return types