from typing import OrderedDict


def get_word2pos_map(text, pos_list, pattern, stopwords, min_length=4):
    """Get a dictionary with positional values from spacy doc object.

    Select only the word with target POS and not found in the pattern

    :param text:spacy doc object.
    :param pos_list:a list of target POS.
    :param pattern: regular expression or None.
    :param stopwords: a list of stopwords to filter.
    :return:A dictionary, where key is the lemma and the values are the positions in the text
     for example: {"vervoer": [4,6,19]}.
    """

    map = OrderedDict()
    for i, word in enumerate(text):
        if word.pos_ in pos_list:  # only select word from the target POS.
            # select only word that is neither in the stop word list or found in the pattern list
            if word.lemma_ not in stopwords and len(word.lemma_) >= min_length and (not pattern or not pattern.search(word.text)):
                if word.lemma_ not in map:
                    map[word.lemma_] = []
                map[word.lemma_].append(i)
    return map