 FUZZY_OVERLAP_THRESHOLD = 2.5 # If areas are close enough, we see them as overlapping (in units)
 SECTION_INDENT_THRESHOLD = 1.0 / 40.0
 SPLIT_PARAGRAPH_THRESHOLD = 0.9
+SECTION_PARAGRAPH_DISTANCE_THRESHOLD = 0.5 # To allow for small variations (in units)
+PARAGRAPH_HEIGHT_THRESHOLD = 1.5 # Paragraph has to be larger than one line height
 
 # Merges sorted lists in sorted list with unique elements
 merge = (list1, list2) ->

  setTextContent: (pageNumber, textContent) =>
    @_pages[pageNumber - 1].textContent = textContent

  _dy: (pageNumber, i, j) =>
    j ?= i + 1
    page = @_pages[pageNumber - 1]
    page.textSegments[j].top - page.textSegments[i].top

  _computeLineHeight: (pageNumber) =>
    page = @_pages[pageNumber - 1]

      left = Math.min left, segment.left
      right = Math.max right, (segment.left + segment.width)

    left: left
    right: right
    indentThreshold: left + (right - left) * SECTION_INDENT_THRESHOLD

  _splitParagraphs: (pageNumber, segmentsIndices, sectionProperties) =>
    page = @_pages[pageNumber - 1]

    s = 0
    paragraphs = []
    for j in [1...segmentsIndices.length]
      segment = page.textSegments[segmentsIndices[j]]
      paragraphIndices = segmentsIndices[s...j]
      boundingBox = @_boundingBox pageNumber, paragraphIndices

      if boundingBox.top - segment.top > page.lineHeight * SPLIT_PARAGRAPH_THRESHOLD and segment.left > sectionProperties.indentThreshold
        paragraphs.push paragraphIndices
        s = j

    paragraphs.push segmentsIndices[s..segmentsIndices.length]
    paragraphs

  _processParagraph: (pageNumber, sectionProperties, paragraphIndices) =>
    page = @_pages[pageNumber - 1]

    boundingBox = @_boundingBox pageNumber, paragraphIndices
    # TODO: Improve which paragraphs we skip
    # TODO: We could try to require that right border of the paragraph bounding box is close to section border
    # TODO: We could try to require that paragraph height is at least 1.5 of the line height
    # TODO: We could try to require that area covered by segments should be more than 0.95 of the bounding box, so very little empty space
    if Math.abs(boundingBox.left - sectionProperties.left) < SECTION_PARAGRAPH_DISTANCE_THRESHOLD * SCALE
      page.paragraphs.push boundingBox

  _processSection: (pageNumber, segmentsIndices) =>
    sectionProperties = @_computeSectionProperties pageNumber, segmentsIndices

    for paragraphIndices in @_splitParagraphs pageNumber, segmentsIndices, sectionProperties
      @_processParagraph pageNumber, sectionProperties, paragraphIndices

  _roundArea: (area) =>
    areaRounded = _.clone area
