import re


def clause_count(doc, infinitive_map):
    """Return clause count (heuristic).

    This function is decorated by the
    :func:`TRUNAJOD:surface_proxies.fix_parse_tree` function, in order to
    heuristically count clauses.

    :param doc: Text to be processed.
    :type doc: Spacy Doc
    :param infinitve_map: Lexicon containing maps from conjugate to infinitive.
    :type infinitive_map: dict
    :return: Clause count
    :rtype: int
    """
    n_clauses = 0
    regexp = re.compile("VerbForm=Fin")
    regexp_perif = re.compile("Perif")
    for token in doc:
        verb_or_aux = token.pos_ in {"VERB", "AUX"}
        if verb_or_aux and not regexp_perif.search(token.tag_):
            if regexp.search(token.tag_):
                n_clauses += 1
    return n_clauses