def match_bert_span_to_text(pred,
			                bertid_2_goldid,
			                question_len,
			                context_tokens
                            ):
	"""
		Match the predicted bert span to the text in gold context.
		Args:
			pred (:obj:`dict`):
				The prediction dictionary.
			bertid_2_goldid (:obj:`list`):
				The list mapping bert token ids to gold token ids.
			question_len (:obj:`int`):
				The length of the question in bert tokens.
			context_tokens (:obj:`list`):
				The list of gold context tokens.
		"""

	answer_start, answer_end = pred["span"]

	# null prediction
	if (answer_start, answer_end) == (0, 0):
		return {'span': None,
		        'answer': None,
		        'answer_tokens': None,
		        'confidence': pred["confidence"],
		        "start_logit": pred["start_logit"],
		        "end_logit": pred["end_logit"],
		        }

	# prediction is not in context
	if (answer_start < question_len or answer_end < question_len):
		return None

	bert_span = (answer_start - question_len, answer_end - question_len)  # span in bert tokens

	gold_span = (bertid_2_goldid[bert_span[0]], bertid_2_goldid[bert_span[1]] + 1)  # span in gold tokens

	# span contains invalid tokens
	if (gold_span[0] < 0 or gold_span[1] < 0):
		return None

	answer_tokens = context_tokens[gold_span[0]:gold_span[1]]

	answer = ' '.join(answer_tokens)

	return {'span': gold_span,
	        'answer': answer,
	        'answer_tokens': answer_tokens,
	        'confidence': pred["confidence"],
	        "start_logit": pred["start_logit"],
	        "end_logit": pred["end_logit"],
	        }