def top_wer_utts(details_by_utterance, top_k=20):
    """
    Finds the k utterances with highest word error rates.

    Useful for diagnostic purposes, to see where the system
    is making the most mistakes.
    Returns results utterances which were not empty
    i.e. had to have been present in the hypotheses, with output produced

    Arguments
    ---------
    details_by_utterance : list
        See output of wer_details_by_utterance.
    top_k : int
        Number of utterances to return.

    Returns
    -------
    list
        List of at most K utterances,
        with the highest word error rates, which were not empty.
        The utterance dict has the same keys as
        details_by_utterance.
    """
    scored_utterances = [
        dets for dets in details_by_utterance if dets["scored"]
    ]
    utts_by_wer = sorted(
        scored_utterances, key=lambda d: d["WER"], reverse=True
    )
    top_non_empty = []
    top_empty = []
    while utts_by_wer and (
        len(top_non_empty) < top_k or len(top_empty) < top_k
    ):
        utt = utts_by_wer.pop(0)
        if utt["hyp_empty"] and len(top_empty) < top_k:
            top_empty.append(utt)
        elif not utt["hyp_empty"] and len(top_non_empty) < top_k:
            top_non_empty.append(utt)
    return top_non_empty, top_empty