class_docstr: This ``SpanConstituencyParser`` simply encodes a sequence of text
  with a stacked ``Seq2SeqEncoder``, extracts span representations using a
  ``SpanExtractor``, and then predicts a label for each span in the sequence.
  These labels are non-terminal nodes in a constituency parse tree, which we then
  greedily reconstruct.
  
  Parameters
  ----------
  vocab : ``Vocabulary``, required
      A Vocabulary, required in order to compute sizes for input/output projections.
  text_field_embedder : ``TextFieldEmbedder``, required
      Used to embed the ``tokens`` ``TextField`` we get as input to the model.
  span_extractor : ``SpanExtractor``, required.
      The method used to extract the spans from the encoded sequence.
  encoder : ``Seq2SeqEncoder``, required.
      The encoder that we will use in between embedding tokens and
      generating span representations.
  feedforward : ``FeedForward``, required.
      The FeedForward layer that we will use in between the encoder and the linear
      projection to a distribution over span labels.
  pos_tag_embedding : ``Embedding``, optional.
      Used to embed the ``pos_tags`` ``SequenceLabelField`` we get as input to the model.
  initializer : ``InitializerApplicator``, optional (default=``InitializerApplicator()``)
      Used to initialize the model parameters.
  regularizer : ``RegularizerApplicator``, optional (default=``None``)
      If provided, will be used to calculate the regularization penalty during training.
  evalb_directory_path : ``str``, optional (default=``DEFAULT_EVALB_DIR``)
      The path to the directory containing the EVALB executable used to score
      bracketed parses. By default, will use the EVALB included with allennlp,
      which is located at allennlp/tools/EVALB . If ``None``, EVALB scoring
      is not used.
class_docstr_tok: ["This","``","SpanConstituencyParser","``","simply","encodes","a","sequence","of","text","with","a","stacked","``","Seq2SeqEncoder","``",",","extracts","span","representations","using","a","``","SpanExtractor","``",",","and","then","predicts","a","label","for","each","span","in","the","sequence",".","These","labels","are","non-terminal","nodes","in","a","constituency","parse","tree",",","which","we","then","greedily","reconstruct",".","Parameters","--","--","--","--","--","vocab",":","``","Vocabulary","``",",","required","A","Vocabulary",",","required","in","order","to","compute","sizes","for","input\/output","projections",".","text_field_embedder",":","``","TextFieldEmbedder","``",",","required","Used","to","embed","the","``","tokens","``","``","TextField","``","we","get","as","input","to","the","model",".","span_extractor",":","``","SpanExtractor","``",",","required",".","The","method","used","to","extract","the","spans","from","the","encoded","sequence",".","encoder",":","``","Seq2SeqEncoder","``",",","required",".","The","encoder","that","we","will","use","in","between","embedding","tokens","and","generating","span","representations",".","feedforward",":","``","FeedForward","``",",","required",".","The","FeedForward","layer","that","we","will","use","in","between","the","encoder","and","the","linear","projection","to","a","distribution","over","span","labels",".","pos_tag_embedding",":","``","Embedding","``",",","optional",".","Used","to","embed","the","``","pos_tags","``","``","SequenceLabelField","``","we","get","as","input","to","the","model",".","initializer",":","``","InitializerApplicator","``",",","optional","(","default=","``","InitializerApplicator","(",")","``",")","Used","to","initialize","the","model","parameters",".","regularizer",":","``","RegularizerApplicator","``",",","optional","(","default=","``","None","``",")","If","provided",",","will","be","used","to","calculate","the","regularization","penalty","during","training",".","evalb_directory_path",":","``","str","``",",","optional","(","default=","``","DEFAULT_EVALB_DIR","``",")","The","path","to","the","directory","containing","the","EVALB","executable","used","to","score","bracketed","parses",".","By","default",",","will","use","the","EVALB","included","with","allennlp",",","which","is","located","at","allennlp\/tools\/EVALB",".","If","``","None","``",",","EVALB","scoring","is","not","used","."]
docstr: Construct ``nltk.Tree``'s for each batch element by greedily nesting spans.
  The trees use exclusive end indices, which contrasts with how spans are
  represented in the rest of the model.
  
  Parameters
  ----------
  predictions : ``torch.FloatTensor``, required.
      A tensor of shape ``(batch_size, num_spans, span_label_vocab_size)``
      representing a distribution over the label classes per span.
  all_spans : ``torch.LongTensor``, required.
      A tensor of shape (batch_size, num_spans, 2), representing the span
      indices we scored.
  num_spans : ``torch.LongTensor``, required.
      A tensor of shape (batch_size), representing the lengths of non-padded spans
      in ``enumerated_spans``.
  sentences : ``List[List[str]]``, required.
      A list of tokens in the sentence for each element in the batch.
  pos_tags : ``List[List[str]]``, optional (default = None).
      A list of POS tags for each word in the sentence for each element
      in the batch.
  
  Returns
  -------
  A ``List[Tree]`` containing the decoded trees for each element in the batch.
docstr_tok: ["Construct","``","nltk.Tree","``","'s","for","each","batch","element","by","greedily","nesting","spans",".","The","trees","use","exclusive","end","indices",",","which","contrasts","with","how","spans","are","represented","in","the","rest","of","the","model",".","Parameters","--","--","--","--","--","predictions",":","``","torch.FloatTensor","``",",","required",".","A","tensor","of","shape","``","(","batch_size",",","num_spans",",","span_label_vocab_size",")","``","representing","a","distribution","over","the","label","classes","per","span",".","all_spans",":","``","torch.LongTensor","``",",","required",".","A","tensor","of","shape","(","batch_size",",","num_spans",",","2",")",",","representing","the","span","indices","we","scored",".","num_spans",":","``","torch.LongTensor","``",",","required",".","A","tensor","of","shape","(","batch_size",")",",","representing","the","lengths","of","non-padded","spans","in","``","enumerated_spans","``",".","sentences",":","``","List","[","List","[","str","]","]","``",",","required",".","A","list","of","tokens","in","the","sentence","for","each","element","in","the","batch",".","pos_tags",":","``","List","[","List","[","str","]","]","``",",","optional","(","default","=","None",")",".","A","list","of","POS","tags","for","each","word","in","the","sentence","for","each","element","in","the","batch",".","Returns","--","--","--","-","A","``","List","[","Tree","]","``","containing","the","decoded","trees","for","each","element","in","the","batch","."]
code: def construct_trees(
      self, predictions, all_spans, num_spans, sentences, pos_tags=None
  ):
      """
      Construct ``nltk.Tree``'s for each batch element by greedily nesting spans.
      The trees use exclusive end indices, which contrasts with how spans are
      represented in the rest of the model.
  
      Parameters
      ----------
      predictions : ``torch.FloatTensor``, required.
          A tensor of shape ``(batch_size, num_spans, span_label_vocab_size)``
          representing a distribution over the label classes per span.
      all_spans : ``torch.LongTensor``, required.
          A tensor of shape (batch_size, num_spans, 2), representing the span
          indices we scored.
      num_spans : ``torch.LongTensor``, required.
          A tensor of shape (batch_size), representing the lengths of non-padded spans
          in ``enumerated_spans``.
      sentences : ``List[List[str]]``, required.
          A list of tokens in the sentence for each element in the batch.
      pos_tags : ``List[List[str]]``, optional (default = None).
          A list of POS tags for each word in the sentence for each element
          in the batch.
  
      Returns
      -------
      A ``List[Tree]`` containing the decoded trees for each element in the batch.
      """
      # Switch to using exclusive end spans.
      exclusive_end_spans = all_spans.clone()
      exclusive_end_spans[:, :, -1] += 1
      no_label_id = self.vocab.get_token_index("NO-LABEL", "labels")
  
      trees = []
      for batch_index, (scored_spans, spans, sentence) in enumerate(
          izip(predictions, exclusive_end_spans, sentences)
      ):
          selected_spans = []
          for prediction, span in izip(
              scored_spans[: num_spans[batch_index]],
              spans[: num_spans[batch_index]],
          ):
              start, end = span
              no_label_prob = prediction[no_label_id]
              label_prob, label_index = torch.max(prediction, -1)
  
              # Does the span have a label != NO-LABEL or is it the root node?
              # If so, include it in the spans that we consider.
              if int(label_index) != no_label_id or (
                  start == 0 and end == len(sentence)
              ):
                  # TODO(Mark): Remove this once pylint sorts out named tuples.
                  # https://github.com/PyCQA/pylint/issues/1418
                  selected_spans.append(
                      SpanInformation(
                          start=int(
                              start
                          ),  # pylint: disable=no-value-for-parameter
                          end=int(end),
                          label_prob=float(label_prob),
                          no_label_prob=float(no_label_prob),
                          label_index=int(label_index),
                      )
                  )
  
          # The spans we've selected might overlap, which causes problems when we try
          # to construct the tree as they won't nest properly.
          consistent_spans = self.resolve_overlap_conflicts_greedily(
              selected_spans
          )
  
          spans_to_labels = dict(
              (
                  (span.start, span.end),
                  self.vocab.get_token_from_index(
                      span.label_index, "labels"
                  ),
              )
              for span in consistent_spans
          )
          sentence_pos = (
              pos_tags[batch_index] if pos_tags is not None else None
          )
          trees.append(
              self.construct_tree_from_spans(
                  spans_to_labels, sentence, sentence_pos
              )
          )
  
      return trees
  
imports: from __future__ import absolute_import
  import torch
  from torch.nn.modules.linear import Linear
  from nltk import Tree
  from allennlp.common.checks import check_dimensions_match
  from allennlp.data import Vocabulary
  from allennlp.modules import Seq2SeqEncoder, TimeDistributed, TextFieldEmbedder, FeedForward
  from allennlp.modules.token_embedders import Embedding
  from allennlp.modules.span_extractors.span_extractor import SpanExtractor
  from allennlp.models.model import Model
  from allennlp.nn import InitializerApplicator, RegularizerApplicator
  from allennlp.nn.util import get_text_field_mask, sequence_cross_entropy_with_logits
  from allennlp.nn.util import last_dim_softmax, get_lengths_from_binary_sequence_mask
  from allennlp.training.metrics import CategoricalAccuracy
  from allennlp.training.metrics import EvalbBracketingScorer, DEFAULT_EVALB_DIR
  from allennlp.common.checks import ConfigurationError
label_desc: Use image node_id 4 for calling the SpanConstituencyParser obj's underlying member method code with example usage: obj.construct_trees(predictions, all_spans, num_spans, sentences, pos_tags) and returns: trees