def token2idx(word, vocab):
    """
    词换取index

    Example:
        for token in tokenizer('Nice to meet you!'):
            print(token, token2idx(token, enc_vocab))

    :param word: 要查询的index
    :param vocab: 词典
    :return: index , 如果word不在词典中, 则返回<unk>对应的index
    """
    try:
        return vocab[word]
    except KeyError:
        # 如果是因为出现次数太少而不在词典中的词, 则返回<unk>
        return vocab['<unk>']