def count_relations(w0):
    """
    0 -> no terms idd
    1 -> most term idd are shared in root morphem
    2 -> most term idd are shared in flexing morphem
    3 -> most term idd are shared root <-> flexing (crossed)
    :param w0:
    :param w1:
    :return:
    """

    root_w0_relations = set(chain.from_iterable(relations[t.index, :].indices for t in w0.root))
    flexing_w0_relations = set(chain.from_iterable(relations[t.index, :].indices for t in w0.flexing))

    def f(w1):
        root_w1 = set(t.index for t in w1.root)
        flexing_w1 = set(t.index for t in w1.flexing)

        count = [root_w0_relations.intersection(root_w1),
                 flexing_w0_relations.intersection(flexing_w1),
                 root_w0_relations.intersection(flexing_w1) | flexing_w0_relations.intersection(root_w1)]

        if any(count):
            return max((1,2,3), key=lambda i: len(count[i - 1]))
        else:
            return 0
    return f