from typing import Callable


def position_features(sentence: str, tokenizer: Callable, vocab: set) -> list:
    """Encodes the relative position of the tokens in :param sentence: as follows:

    - 0 for tokens at the beginning of the sentence; 
    - 1 for tokens in the middle of the sentence;
    - 2 for tokens at the end of the sentence.

    Tokens are extracted with the :param tokenizer: callable and filtered based on
    whether they appear in :param vocab: or not.
    """
    out = []
    tokens = tokenizer(sentence)
    for i, token in enumerate(tokens):
        if token not in vocab:
            continue
        if i == 0:
            out.append(0)
        elif i == len(tokens):
            out.append(2)
        else:
            out.append(1)
    return out