Problems
Loading...
Step 4 of 6
Next Sentence Prediction
Hard

Construct deterministic training pairs for BERT's next-sentence-prediction objective. Each pair specification identifies sentence A and sentence B by document and sentence indices. Label a pair as one only when sentence B immediately follows sentence A in the same document. Otherwise label it as zero.

Return a Python list containing one dictionary per specification in the original order. Every dictionary must contain exactly sentence_a, sentence_b, and is_next. The two sentence values are strings and is_next is an integer.

Loading visualization...

Examples

Input: documents = [["The cat sat.","It was sunny."],["Dogs bark.","They also play."]], pair_specs = [{"doc_a":0,"sent_a":0,"doc_b":0,"sent_b":1}]

Output: [{"sentence_a":"The cat sat.","sentence_b":"It was sunny.","is_next":1}]

Explanation: Sentence B immediately follows sentence A in the same document, so is_next is one.

Input: documents = [["The cat sat.","It was sunny."],["Dogs bark.","They also play."]], pair_specs = [{"doc_a":0,"sent_a":0,"doc_b":1,"sent_b":0}]

Output: [{"sentence_a":"The cat sat.","sentence_b":"Dogs bark.","is_next":0}]

Input: documents = [["A0","A1","A2"],["B0","B1"]], pair_specs = [{"doc_a":0,"sent_a":1,"doc_b":0,"sent_b":2},{"doc_a":1,"sent_a":0,"doc_b":0,"sent_b":2},{"doc_a":0,"sent_a":0,"doc_b":0,"sent_b":2}]

Output: [{"sentence_a":"A1","sentence_b":"A2","is_next":1},{"sentence_a":"B0","sentence_b":"A2","is_next":0},{"sentence_a":"A0","sentence_b":"A2","is_next":0}]

Hint 1

Read each sentence with documents[doc_index][sentence_index].

Hint 2

Check document equality and whether sent_b equals sent_a + 1.

Requirements

  • Use ordinary Python list and dictionary operations.
  • Resolve both sentences from their supplied indices.
  • Assign one only to consecutive sentences from the same document.
  • Preserve the order of pair_specs.
  • Return the exact documented list of dictionaries.

Constraints

  • documents is a nonempty list of nonempty sentence lists.
  • Every sentence is a string.
  • pair_specs is a nonempty list of dictionaries.
  • Every supplied document and sentence index is valid.