ontocast.tool.chunk.proposition¶
Lightweight text splitting helpers (no ML dependencies).
The default path here is pure regex and stays that way. The opt-in bounds -- an encoder-token budget and the measurement-aware break guard -- take their extra knowledge as arguments (a token counter, the shared measurement lexicon), so this module never grows a dependency on an encoder or on a domain vocabulary.
Attributes¶
ABBREVIATIONS = frozenset({'al', 'cf', 'ed', 'eds', 'edn', 'eq', 'eqs', 'fig', 'figs', 'no', 'nos', 'pp', 'ref', 'refs', 'sec', 'sect', 'suppl', 'tab', 'tabs', 'vol', 'vols', 'approx', 'ca', 'e.g', 'i.e', 'vs', 'viz', 'dr', 'prof', 'mr', 'mrs', 'ms', 'st', 'jr', 'sr', 'inc', 'ltd'})
module-attribute
¶
FALLBACK_CHARS_PER_TOKEN = 3.5
module-attribute
¶
SENTENCE_SPLIT_REGEX = '(?:\\n\\s*\\n+)|(?<=[.!?])\\s+(?=[A-Z][a-z])'
module-attribute
¶
TokenCounter = Callable[[list[str]], list[int] | None]
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
Functions:¶
split_proposition_windows(text, max_sentences=2, max_windows=16, stride=None, max_chars=None, max_tokens=None, token_counter=None, abbreviation_aware=False, measurement_aware=False, overlap=0.0)
¶
Split text into short proposition-like windows for retrieval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Passage to split. |
required |
max_sentences
|
int
|
Sentences per window. Not applied when |
2
|
max_windows
|
int
|
Ceiling on the windows returned; over it, windows are sampled evenly across the passage rather than truncated, so the tail still contributes a query. |
16
|
stride
|
int | None
|
Sentences advanced between windows. |
None
|
max_chars
|
int | None
|
Characters per window. A sentence count is a poor bound on how much text a query carries. Two
sentences of technical prose range over an order of magnitude in
length, and the encoder truncates on tokens, so a sentence-bounded
window can silently lose its tail. The splitter also has no
abbreviation handling and breaks on every A character budget addresses both ends: it caps the long windows that truncate, and it coalesces short fragments, because it keeps taking sentences until the budget is reached. A single sentence longer than the budget is emitted whole rather than cut -- the encoder truncates it either way, and cutting first only loses more. |
None
|
max_tokens
|
int | None
|
Encoder word pieces per window, measured with This is the only bound that equalises what a query carries, because it is the unit the encoder itself counts in: characters per token drift with notation, so a character budget that fits one passage truncates the next. Set below the encoder's sequence limit, it makes truncation impossible by construction -- which a character budget can only approximate. Unlike the character budget it does cut a sentence that alone exceeds the budget, at a whitespace boundary, because a sentence emitted whole would be truncated by the encoder anyway and the tail would then reach no lane at all. |
None
|
token_counter
|
TokenCounter | None
|
Word pieces per text, typically
|
None
|
abbreviation_aware
|
bool
|
Merge fragments the period-splitter created inside an
abbreviation, an initial or a citation run. A prerequisite for any
equal-size packing rather than a candidate of its own: without it the
packer's atoms include "Chem. Lett.", and a window bound counted in
those atoms is not counting sentences. General English and
bibliographic shapes only -- see :data: |
False
|
measurement_aware
|
bool
|
Forbid a break between a number and the unit it is
written with, or inside a range, using the number/unit shapes in
:mod: |
False
|
overlap
|
float
|
Fraction of a window repeated at the start of the next one, for
the budget modes. |
0.0
|
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: Windows in document order. |
Source code in ontocast/tool/chunk/proposition.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |