MCPcopy Create free account
hub / github.com/tensorflow/models / _improve_answer_span

Function _improve_answer_span

official/nlp/data/squad_lib.py:477–511  ·  view source on GitHub ↗

Returns tokenized answer spans that better match the annotated answer.

(doc_tokens, input_start, input_end, tokenizer,
                         orig_answer_text)

Source from the content-addressed store, hash-verified

475
476
477def _improve_answer_span(doc_tokens, input_start, input_end, tokenizer,
478 orig_answer_text):
479 """Returns tokenized answer spans that better match the annotated answer."""
480
481 # The SQuAD annotations are character based. We first project them to
482 # whitespace-tokenized words. But then after WordPiece tokenization, we can
483 # often find a "better match". For example:
484 #
485 # Question: What year was John Smith born?
486 # Context: The leader was John Smith (1895-1943).
487 # Answer: 1895
488 #
489 # The original whitespace-tokenized answer will be "(1895-1943).". However
490 # after tokenization, our tokens will be "( 1895 - 1943 ) .". So we can match
491 # the exact answer, 1895.
492 #
493 # However, this is not always possible. Consider the following:
494 #
495 # Question: What country is the top exporter of electronics?
496 # Context: The Japanese electronics industry is the lagest in the world.
497 # Answer: Japan
498 #
499 # In this case, the annotator chose "Japan" as a character sub-span of
500 # the word "Japanese". Since our WordPiece tokenizer does not split
501 # "Japanese", we just use "Japanese" as the annotation. This is fairly rare
502 # in SQuAD, but does happen.
503 tok_answer_text = " ".join(tokenizer.tokenize(orig_answer_text))
504
505 for new_start in range(input_start, input_end + 1):
506 for new_end in range(input_end, new_start - 1, -1):
507 text_span = " ".join(doc_tokens[new_start:(new_end + 1)])
508 if text_span == tok_answer_text:
509 return (new_start, new_end)
510
511 return (input_start, input_end)
512
513
514def _check_is_max_context(doc_spans, cur_span_index, position):

Callers 1

Calls 2

joinMethod · 0.45
tokenizeMethod · 0.45

Tested by

no test coverage detected