import collections
import re


def _build_question_dictionary(vqa, min_thre=0):
    """
    :param vqa: VQA instance
    :param min_thre: only words that occur more than this number of times will be put in vocab
    :return: word-index dictionary
    """
    counter = collections.defaultdict(int)
    for i, q in vqa.qqa.items():
        words = re.findall(r"[\w']+", q['question'])
        for word in words:
            counter[word] += 1
    question_dict = {}
    indx = 0
    for word, num in counter.items():
        if num > min_thre:
            question_dict[word] = indx
            indx += 1
    return question_dict