Skip to content

fix: sectioner does not consider separator length#1858

Merged
awalker4 merged 3 commits into
mainfrom
scanny/sectioner-considers-separator
Oct 26, 2023
Merged

fix: sectioner does not consider separator length#1858
awalker4 merged 3 commits into
mainfrom
scanny/sectioner-considers-separator

Conversation

@scanny

@scanny scanny commented Oct 24, 2023

Copy link
Copy Markdown
Contributor

sectioner-does-not-consider-separator-length

Executive Summary. A primary responsibility of the sectioner is to minimize the number of chunks that need to be split mid-text. It does this by computing text-length of the section being formed and "finishing" the section when adding another element would extend its text beyond the window size.

When element-text is consolidated into a chunk, the text of each element is joined, separated by a "blank-line" ("\n\n"). The sectioner does not currently consider the added length of separators (2-chars each) and so forms sections that need to be split mid-text when chunked.

Chunk-splitting should only be necessary when the text of a single element is longer than the chunking window.

Example

  elements: List[Element] = [
      Title("Chunking Priorities"),  # 19 chars
      ListItem("Divide text into manageable chunks"),  # 34 chars
      ListItem("Preserve semantic boundaries"),  # 28 chars
      ListItem("Minimize mid-text chunk-splitting"),  # 33 chars
  ]  # 114 chars total but 120 chars with separators

  chunks = chunk_by_title(elements, max_characters=115)

Want:

[
  CompositeElement(
      "Chunking Priorities"
      "\n\nDivide text into manageable chunks"
      "\n\nPreserve semantic boundaries"
  ),
  CompositeElement("Minimize mid-text chunk-splitting"),
]

Got:

[
  CompositeElement(
      "Chunking Priorities"
      "\n\nDivide text into manageable chunks"
      "\n\nPreserve semantic boundaries"
      "\n\nMinimize mid-text chunk-spli"),
  )
  CompositeElement("tting")

Technical Summary

Because the sectioner does not consider separator ("\n\n") length when it computes the space remaining in the section, it over-populates the section and when the chunker concatenates the element text (each separated by the separator) the text exceeds the window length and the chunk must be split mid-text, even though there was an even element boundary it could have been split on.

Fix

Consider separator length in the space-remaining computation.

The solution here extracts both the section.text_length and section.space_remaining computations to a _TextSectionBuilder object which removes the need for the sectioner (_split_elements_by_title_and_table()) to deal with primitives (List[Element], running text length, separator length, etc.) and allows it to focus on the rules of when to start a new section.

This solution may seem like overkill at the moment and indeed it would be except it forms the foundation for adding section-level chunk combination (fix: dissociated title elements) in the next PR. The objects introduced here will gain several additional responsibilities in the next few chunking PRs in the pipeline and will earn their place.

@scanny
scanny changed the base branch from main to scanny/chunk-by-title-is-rude October 24, 2023 16:40
@scanny
scanny force-pushed the scanny/sectioner-considers-separator branch from 3ec0d60 to 4491f36 Compare October 24, 2023 17:00
@scanny
scanny force-pushed the scanny/chunk-by-title-is-rude branch from 72022ad to a7ab984 Compare October 24, 2023 17:01
@scanny
scanny force-pushed the scanny/sectioner-considers-separator branch from 4491f36 to c6b558c Compare October 24, 2023 17:42
@scanny
scanny force-pushed the scanny/chunk-by-title-is-rude branch 3 times, most recently from 79e47bd to 11854fd Compare October 24, 2023 18:32
@scanny
scanny force-pushed the scanny/sectioner-considers-separator branch from 8198dc8 to 4d93ac5 Compare October 24, 2023 18:33
@scanny
scanny marked this pull request as ready for review October 24, 2023 19:24
@scanny
scanny force-pushed the scanny/chunk-by-title-is-rude branch from 11854fd to 6b8a3ce Compare October 24, 2023 21:22
@scanny
scanny force-pushed the scanny/sectioner-considers-separator branch 2 times, most recently from 7ed01c7 to 08bd213 Compare October 24, 2023 22:39
Base automatically changed from scanny/chunk-by-title-is-rude to main October 25, 2023 00:12
@scanny
scanny force-pushed the scanny/sectioner-considers-separator branch from 08bd213 to fa158e8 Compare October 25, 2023 00:40
@scanny
scanny requested a review from Coniferish October 25, 2023 05:15
@scanny scanny self-assigned this Oct 25, 2023
@scanny
scanny force-pushed the scanny/sectioner-considers-separator branch from fa158e8 to 640314b Compare October 25, 2023 05:22
Comment thread test_unstructured/chunking/test_title.py
Comment thread unstructured/chunking/title.py
Comment thread unstructured/chunking/title.py
Comment thread unstructured/chunking/title.py
Comment thread unstructured/chunking/title.py
@scanny
scanny force-pushed the scanny/sectioner-considers-separator branch 2 times, most recently from 72cfe88 to 20bb5a0 Compare October 26, 2023 19:37
Comment thread CHANGELOG.md Outdated
@scanny
scanny force-pushed the scanny/sectioner-considers-separator branch from 8c1141c to 400cac4 Compare October 26, 2023 20:26
@scanny
scanny force-pushed the scanny/sectioner-considers-separator branch from 400cac4 to 405a2cd Compare October 26, 2023 20:33

@Coniferish Coniferish left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@awalker4
awalker4 added this pull request to the merge queue Oct 26, 2023
Merged via the queue into main with commit f273a7c Oct 26, 2023
@awalker4
awalker4 deleted the scanny/sectioner-considers-separator branch October 26, 2023 22:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants