Skip to content




Dataset reader for SuperGLUE's Reading Comprehension with Commonsense Reasoning task (Zhang Et al. 2018).

Reader Implemented by Gabriel Orlanski


class RecordTaskReader(DatasetReader):
 | def __init__(
 |     self,
 |     transformer_model_name: str = "bert-base-cased",
 |     length_limit: int = 384,
 |     question_length_limit: int = 64,
 |     stride: int = 128,
 |     raise_errors: bool = False,
 |     tokenizer_kwargs: Dict[str, Any] = None,
 |     one_instance_per_query: bool = False,
 |     max_instances: int = None,
 |     **kwargs
 | ) -> None

Reader for Reading Comprehension with Commonsense Reasoning(ReCoRD) task from SuperGLUE. The task is detailed in the paper ReCoRD: Bridging the Gap between Human and Machine Commonsense Reading Comprehension ( by Zhang et al. Leaderboards and the official evaluation script for the ReCoRD task can be found

The reader reads a JSON file in the format from


  • tokenizer : Tokenizer, optional
    The tokenizer class to use. Defaults to SpacyTokenizer

  • token_indexers : Dict[str, TokenIndexer], optional
    We similarly use this for both the question and the passage. See TokenIndexer. Default is {"tokens": SingleIdTokenIndexer()}.

  • passage_length_limit : int, optional (default = None)
    If specified, we will cut the passage if the length of passage exceeds this limit.

  • question_length_limit : int, optional (default = None)
    If specified, we will cut the question if the length of question exceeds this limit.

  • raise_errors : bool, optional (default = False)
    If the reader should raise errors or just continue.

  • kwargs : Dict
    Keyword arguments to be passed to the DatasetReader parent class constructor.


class RecordTaskReader(DatasetReader):
 | ...
 | def get_instances_from_example(
 |     self,
 |     example: Dict,
 |     always_add_answer_span: bool = False
 | ) -> Iterable[Instance]

Helper function to get instances from an example.

Much of this comes from transformer_squad.make_instances


  • example : Dict[str,Any]
    The example dict.


Iterable[Instance] The instances for each example


class RecordTaskReader(DatasetReader):
 | ...
 | def tokenize_slice(
 |     self,
 |     text: str,
 |     start: int = None,
 |     end: int = None
 | ) -> Iterable[Token]

Get + tokenize a span from a source text.

Originally from the


  • text : str
    The text to draw from.
  • start : int
    The start index for the span.
  • end : int
    The end index for the span. Assumed that this is inclusive.


  • Iterable[Token] List of tokens for the retrieved span.


class RecordTaskReader(DatasetReader):
 | ...
 | def tokenize_str(self, text: str) -> List[Token]

Helper method to tokenize a string.

Adapted from the transformer_squad.make_instances


text: `str`
    The string to tokenize.


  • Iterable[Tokens] The resulting tokens.


class RecordTaskReader(DatasetReader):
 | ...
 | @staticmethod
 | def get_spans_from_text(
 |     text: str,
 |     spans: List[Tuple[int, int]]
 | ) -> List[str]

Helper function to get a span from a string


text: str The source string spans: List[Tuple[int,int]] List of start and end indices for spans.

Assumes that the end index is inclusive. Therefore, for start
index `i` and end index `j`, retrieves the span at `text[i:j+1]`.


  • List[str] The extracted string from text.


class RecordTaskReader(DatasetReader):
 | ...
 | def text_to_instance(
 |     self,
 |     query: str,
 |     tokenized_query: List[Token],
 |     passage: str,
 |     tokenized_passage: List[Token],
 |     answers: List[str],
 |     token_answer_span: Optional[Tuple[int, int]] = None,
 |     additional_metadata: Optional[Dict[str, Any]] = None,
 |     always_add_answer_span: Optional[bool] = False
 | ) -> Instance

A lot of this comes directly from the transformer_squad.text_to_instance