def encode(seqTokens, tokenToIdx, allowUnk=False):
  """
  Given seqTokens (list of tokens), encode to seqIdx (list of token_ixs)
  """
  seqIdx = []
  for token in seqTokens:
    if token not in tokenToIdx:
      if allowUnk:
        token = '<UNK>'
      else:
        raise KeyError('Token "%s" not in vocab' % token)
    seqIdx.append(tokenToIdx[token])
  return seqIdx