from datasets import load_metric from ast import literal_eval def compute_seqeval(references_dataset, predictions_dataset, ref_col='ner_tags', pred_col='pred_ner_tags'): # computes the seqeval scores # sort by id references_dataset = references_dataset.sort('unique_id') predictions_dataset = predictions_dataset.sort('unique_id') # load the huggingface metric function seqeval = load_metric('seqeval') # check that tokens match assert(references_dataset['tokens']==predictions_dataset['tokens']) # ensure IOB2? # compute scores seqeval_results = seqeval.compute(predictions = predictions_dataset[pred_col], references = references_dataset[ref_col], scheme = 'IOB2', suffix = False, ) # change all values to regular (not numpy) floats (otherwise cannot be serialized to json) seqeval_results = literal_eval(str(seqeval_results)) return(seqeval_results)