ontocast.tool.chunk.util¶
Attributes¶
SENTENCE_SPLIT_REGEX = '(?:\\n\\s*\\n+)|(?<=[.!?])\\s+(?=[A-Z][a-z])'
module-attribute
¶
__all__ = ['SENTENCE_SPLIT_REGEX', 'SemanticChunker', 'split_proposition_windows']
module-attribute
¶
Classes¶
SemanticChunker
¶
Bases: BaseDocumentTransformer
Source code in ontocast/tool/chunk/util.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
Attributes¶
chunk_config = chunk_config
instance-attribute
¶
embeddings = embeddings
instance-attribute
¶
max_size = chunk_config.max_size
instance-attribute
¶
min_size = chunk_config.min_size
instance-attribute
¶
sentence_split_regex = sentence_split_regex
instance-attribute
¶
Methods:¶
__init__(embeddings, chunk_config, sentence_split_regex)
¶
Initialize SemanticChunker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embeddings
|
Embeddings
|
Embeddings model for generating sentence embeddings. |
required |
chunk_config
|
ChunkConfig
|
Chunking configuration containing min_size, max_size, etc. |
required |
sentence_split_regex
|
str
|
Regular expression pattern for splitting text into sentences. |
required |
Source code in ontocast/tool/chunk/util.py
create_documents(texts, metadatas=None)
¶
Source code in ontocast/tool/chunk/util.py
split_documents(documents)
¶
Source code in ontocast/tool/chunk/util.py
split_text(text)
¶
Source code in ontocast/tool/chunk/util.py
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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
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 | |