code: def apply_templates(toks, templates):
      
      for template in templates:
          name = '|'.join(['%s[%d]' % (f, o) for f, o in template])
          for t in range(len(toks)):
              values_list = []
              for field, offset in template:
                  p = t + offset
                  if p < 0 or p >= len(toks):
                      values_list = []
                      break
                  if field in toks[p]:
                      value = toks[p][field]
                      values_list.append(value if isinstance(value, (set, list)) else [value])
              if len(template) == len(values_list):
                  for values in product(*values_list):
                      toks[t]['F'].append('%s=%s' % (name, '|'.join(values)))
docs: Generate features for an item sequence by applying feature templates.
      A feature template consists of a tuple of (name, offset) pairs,
      where name and offset specify a field name and offset from which
      the template extracts a feature value. Generated features are stored
      in the 'F' field of each item in the sequence.
  
      Parameters
      ----------
      toks: list of tokens
          A list of processed toknes.
  
      templates: list of template tuples (str, int)
          A feature template consists of a tuple of (name, offset) pairs,
          where name and offset specify a field name and offset from which
          the template extracts a feature value.