# Buggy code
def align_entity_predictions(targets, predictions, tokens, extractors):
    """Aligns entity predictions to the message tokens.
    Determines for every token the true label based on the
    prediction targets and the label assigned by each
    single extractor.
    :param targets: list of target entities
    :param predictions: list of predicted entities
    :param tokens: original message tokens
    :param extractors: the entity extractors that should be considered
    :return: dictionary containing the true token labels and token labels
             from the extractors
    """

    true_token_labels = []
    entities_by_extractors = {extractor: [] for extractor in extractors}
    for p in predictions:
        entities_by_extractors[p["extractor"]].append(p)
    extractor_labels = defaultdict(list)
    for t in tokens:
        true_token_labels.append(determine_token_labels(t, targets, None))
        for extractor, entities in entities_by_extractors.items():
            extracted = determine_token_labels(t, entities, extractor)
            extractor_labels[extractor].append(extracted)

    return {
        "target_labels": true_token_labels,
        "extractor_labels": dict(extractor_labels),
    }


# Traceback (KeyError)
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "C:\ProgramData\Anaconda3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\ProgramData\Anaconda3\lib\site-packages\rasa_nlu\evaluate.py", line 857,
in <module>
main()
File "C:\ProgramData\Anaconda3\lib\site-packages\rasa_nlu\evaluate.py", line 834,
in main
data, int(cmdline_args.folds), nlu_config)
File "C:\ProgramData\Anaconda3\lib\site-packages\rasa_nlu\evaluate.py", line 720,
in run_cv_evaluation
interpreter, train)
File "C:\ProgramData\Anaconda3\lib\site-packages\rasa_nlu\evaluate.py", line 684,
in combine_entity_result
current_result = compute_entity_metrics(interpreter, data)
File "C:\ProgramData\Anaconda3\lib\site-packages\rasa_nlu\evaluate.py", line 777,
in compute_entity_metrics
merged_predictions = merge_labels(aligned_predictions, extractor)
File "C:\ProgramData\Anaconda3\lib\site-packages\rasa_nlu\evaluate.py", line 307,
in merge_labels
for ap in aligned_predictions]
File "C:\ProgramData\Anaconda3\lib\site-packages\rasa_nlu\evaluate.py", line 307,
in <listcomp>
for ap in aligned_predictions]
KeyError: 'ner_crf'

# Fixed code
def align_entity_predictions(targets, predictions, tokens, extractors):
    """Aligns entity predictions to the message tokens.
    Determines for every token the true label based on the
    prediction targets and the label assigned by each
    single extractor.
    :param targets: list of target entities
    :param predictions: list of predicted entities
    :param tokens: original message tokens
    :param extractors: the entity extractors that should be considered
    :return: dictionary containing the true token labels and token labels
             from the extractors
    """

    true_token_labels = []
    entities_by_extractors = {extractor: [] for extractor in extractors}
    for p in predictions:
        entities_by_extractors[p["extractor"]].append(p)
    extractor_labels = {extractor: [] for extractor in extractors}
    for t in tokens:
        true_token_labels.append(determine_token_labels(t, targets, None))
        for extractor, entities in entities_by_extractors.items():
            extracted = determine_token_labels(t, entities, extractor)
            extractor_labels[extractor].append(extracted)

    return {
        "target_labels": true_token_labels,
        "extractor_labels": dict(extractor_labels),
    }