from typing import Counter


def states_and_counts_from_annots(annots):
    """get ``states`` and ``counts``
    from a list of ``crowsetta.Annotation``s,
    to build a transition matrix
    showing the probability of going from
    one state to another.

    Parameters
    ----------
    annots : list
        of ``crowsetta.Annotation``s
        whose ``Sequence.label`` attributes
        will be used to count transitions
        from one label to another
        in the sequences of syllables

    Returns
    -------
    states : set
        of characters, set of
        unique labels
        that occur in the list of
        ``Annotation``s.
    counts : collections.Counter
        where keys are transitions
        of the form "from label,
        to label", e.g. "ab",
        and values are number of
        occurrences of that transition.
    """
    states = sorted(
        set([lbl
             for annot in annots
             for lbl in annot.seq.labels.tolist()])
    )

    counts = Counter()
    for annot in annots:
        labels = annot.seq.labels.tolist()
        trans = zip(labels[:-1], labels[1:])
        for a_trans in trans:
            counts[a_trans] += 1
    return states, counts