Exemplo n.º 1
0
def cseg_similarity_matrix_classes(card, prime_algorithm="prime_form_sampaio"):
    """Returns a matrix with CSIM between multiple csegs.

    >>> cseg_similarity_matrix_classes(3)
    [[< 0 1 2 >, < 0 2 1 >],
    [1.0, 0.66666666666666663],
    [0.66666666666666663, 1.0]]
    """

    classes_lst = contour.build_classes_card(card, prime_algorithm)
    classes = [contour.Contour(list(cseg)) for (a, b, cseg, c) in classes_lst]

    return cseg_similarity_matrix(classes)
Exemplo n.º 2
0
def cseg_from_class_number(card, class_number, prime_algorithm="prime_form_sampaio"):
    """Returns a cseg from a given cardinality and class
    number. Sampaio Prime algorithm is default.

    >>> cseg_from_class_number(4, 7)
    < 1 0 3 2 >
    """

    card_classes = contour.build_classes_card(card, prime_algorithm)
    for classes in card_classes:
        cc, cn, cs, ri = classes
        if card == cc and class_number == cn:
            return contour.Contour(cs)
Exemplo n.º 3
0
def cseg_similarity_continuum(cseg, prime_algorithm="prime_form_marvin_laprade"):
    """Returns all csegs with the same cardinality of the given one
    sorted by cseg similarity.

    >>> cseg_similarity_continuum(Contour([1, 0, 3, 2]))
    [[0.5, [< 0 2 1 3 >, < 0 3 2 1 >]],
    [0.66666666666666663, [< 0 1 2 3 >, < 0 2 3 1 >, < 0 3 1 2 >]],
    [0.83333333333333337, [< 0 1 3 2 >, < 1 3 0 2 >]],
    [1.0, [< 1 0 3 2 >]]]
    """

    size = len(cseg)
    built_classes = contour.build_classes_card(size, prime_algorithm)

    def cseg_similarity_lists(built_classes):
        """Returns a tuple with two lists:
        1. cseg similarity (CSIM) and csegclass, and
        2. all cseg similarity in previous list.

        Accepts built classes with one cardinality
        """

        csegclasses = []
        similarity = []

        for (a, b, csegclass, d) in built_classes:
            csegclass = contour.Contour(csegclass)
            csim = cseg_similarity(cseg, csegclass)
            csegclasses.append([csim, csegclass])
            similarity.append(csim)

        return csegclasses, sorted(list(set(similarity)))

    def grouped_cseg_similarity_lists((csegclasses, similarity)):
        """Returns csegclasses grouped by cseg similarity. Accepts
        lists provided by cseg_similarity_lists function.
        """

        result = []

        for similarity_index in similarity:
            simil_index = []
            for csegclass in sorted(csegclasses):
                if csegclass[0] == similarity_index:
                    simil_index.append(csegclass[1])
            result.append([similarity_index, simil_index])

        return result

    return grouped_cseg_similarity_lists(cseg_similarity_lists(built_classes))