class_docstr: Implements a KIE document builder
  
  Args:
  ----
      resolve_lines: whether words should be automatically grouped into lines
      resolve_blocks: whether lines should be automatically grouped into blocks
      paragraph_break: relative length of the minimum space separating paragraphs
      export_as_straight_boxes: if True, force straight boxes in the export (fit a rectangle
          box to all rotated boxes). Else, keep the boxes format unchanged, no matter what it is.
class_docstr_tok: ["Implements","a","KIE","document","builder","Args",":","--","--","resolve_lines",":","whether","words","should","be","automatically","grouped","into","lines","resolve_blocks",":","whether","lines","should","be","automatically","grouped","into","blocks","paragraph_break",":","relative","length","of","the","minimum","space","separating","paragraphs","export_as_straight_boxes",":","if","True",",","force","straight","boxes","in","the","export","(","fit","a","rectangle","box","to","all","rotated","boxes",")",".","Else",",","keep","the","boxes","format","unchanged",",","no","matter","what","it","is","."]
docstr: Re-arrange detected words into structured predictions
  
  Args:
  ----
      pages: list of N elements, where each element represents the page image
      boxes: list of N dictionaries, where each element represents the localization predictions for a class,
          of shape (*, 5) or (*, 6) for all predictions
      text_preds: list of N dictionaries, where each element is the list of all word prediction
      page_shapes: shape of each page, of size N
      orientations: optional, list of N elements,
          where each element is a dictionary containing the orientation (orientation + confidence)
      languages: optional, list of N elements,
          where each element is a dictionary containing the language (language + confidence)
  
  Returns:
  -------
      document object
docstr_tok: ["Re-arrange","detected","words","into","structured","predictions","Args",":","--","--","pages",":","list","of","N","elements",",","where","each","element","represents","the","page","image","boxes",":","list","of","N","dictionaries",",","where","each","element","represents","the","localization","predictions","for","a","class",",","of","shape","(","*",",","5",")","or","(","*",",","6",")","for","all","predictions","text_preds",":","list","of","N","dictionaries",",","where","each","element","is","the","list","of","all","word","prediction","page_shapes",":","shape","of","each","page",",","of","size","N","orientations",":","optional",",","list","of","N","elements",",","where","each","element","is","a","dictionary","containing","the","orientation","(","orientation","+","confidence",")","languages",":","optional",",","list","of","N","elements",",","where","each","element","is","a","dictionary","containing","the","language","(","language","+","confidence",")","Returns",":","--","--","--","-","document","object"]
code: def __call__(  # type: ignore[override]
      self,
      pages: List[np.ndarray],
      boxes: List[Dict[str, np.ndarray]],
      text_preds: List[Dict[str, List[Tuple[str, float]]]],
      page_shapes: List[Tuple[int, int]],
      orientations: Optional[List[Dict[str, Any]]] = None,
      languages: Optional[List[Dict[str, Any]]] = None,
  ) -> KIEDocument:
      """Re-arrange detected words into structured predictions
  
      Args:
      ----
          pages: list of N elements, where each element represents the page image
          boxes: list of N dictionaries, where each element represents the localization predictions for a class,
              of shape (*, 5) or (*, 6) for all predictions
          text_preds: list of N dictionaries, where each element is the list of all word prediction
          page_shapes: shape of each page, of size N
          orientations: optional, list of N elements,
              where each element is a dictionary containing the orientation (orientation + confidence)
          languages: optional, list of N elements,
              where each element is a dictionary containing the language (language + confidence)
  
      Returns:
      -------
          document object
      """
      if len(boxes) != len(text_preds) or len(boxes) != len(
          page_shapes
      ):
          raise ValueError(
              "All arguments are expected to be lists of the same size"
          )
      _orientations = (
          orientations if isinstance(orientations, list) else [None] * len(boxes)  # type: ignore[list-item]
      )
      _languages = languages if isinstance(languages, list) else [None] * len(boxes)  # type: ignore[list-item]
      if self.export_as_straight_boxes and len(boxes) > 0:
          # If boxes are already straight OK, else fit a bounding rect
          if next(iter(boxes[0].values())).ndim == 3:
              straight_boxes: List[Dict[str, np.ndarray]] = []
              # Iterate over pages
              for p_boxes in boxes:
                  # Iterate over boxes of the pages
                  straight_boxes_dict = {}
                  for k, box in p_boxes.items():
                      straight_boxes_dict[k] = np.concatenate(
                          (box.min(1), box.max(1)), 1
                      )
                  straight_boxes.append(straight_boxes_dict)
              boxes = straight_boxes
  
      _pages = [
          KIEPage(
              page,
              {
                  k: self._build_blocks(
                      page_boxes[k],
                      word_preds[k],
                  )
                  for k in page_boxes.keys()
              },
              _idx,
              shape,
              orientation,
              language,
          )
          for page, _idx, shape, page_boxes, word_preds, orientation, language in zip(
              pages,
              range(len(boxes)),
              page_shapes,
              boxes,
              text_preds,
              _orientations,
              _languages,
          )
      ]
  
      return KIEDocument(_pages)
  
file_path: doctr/doctr/models/builder.py
imports: from typing import Any, Dict, List, Optional, Tuple
  import numpy
  from scipy.cluster.hierarchy import fclusterdata
  from doctr.io.elements import Block, Document, KIEDocument, KIEPage, Line, Page, Prediction, Word
  from doctr.utils.geometry import estimate_page_angle, resolve_enclosing_bbox, resolve_enclosing_rbbox, rotate_boxes
  from doctr.utils.repr import NestedObject
label_desc: Use image node_id 1 for calling the KIEDocumentBuilder obj's underlying member method code with example usage: obj.__call__(pages, boxes, text_preds, page_shapes, orientations, languages) and returns: KIEDocument