imbed.segmentation_util#
Tools for segmentation, batching, chunking…
The words segmentation, batching, chunking, along with slicing, partitioning, etc. are often used interchangeably in the context of data processing. Here we will try to clarify the meaning of these terms in the context of our package
We will use the term “segmentation” when the process is about producing (smaller) segments of text from a (larger) input text.
We will use the term “batching” when the process is about producing batches of data from a data input stream (for example, an iterable of text segments that need to be embedded, but we need to batch them to avoid resource limitation issues).
We will use the term “chunking” to denote a more general process of dividing a sequence of items into chunks, usually of a fixed size.
Functions
|
Use the chunker to chunk the items of mapping, yielding sub-mappings |
|
|
|
a function to get (an iterator of) segments (bt, tt) of chunks from an iterator (or list) of the for [it_1, it_2...], given a chk_size, chk_step, and a start_at and a stop_at. |
|
Takes an iterable of chks and produces another iterable of chunks. |
|
A function to extend chunking functionality to any source of iterables, with the ability to wrap the chunks in a function before yielding them. |
|
A function to do |
Classes
|
A class to represent a mapping between segments and documents. |
- class imbed.segmentation_util.SegmentStore(docs, segment_keys)[source]#
Bases:
objectA class to represent a mapping between segments and documents.
- imbed.segmentation_util.chunk_dataframe(src, chunker, *, ingress=operator.methodcaller('iterrows'), egress=<function identity>)#
Yield chunks of rows from a DataFrame. The yielded chunks are lists of (index, row) tuples.
- Return type:
Iterable[ChunkBasedObj]
- imbed.segmentation_util.chunk_mapping(mapping, chunker=None)[source]#
Use the chunker to chunk the items of mapping, yielding sub-mappings
- Parameters:
- Return type:
- Returns:
an iterator of sub-mappings in the form of dictionaries
Examples
>>> from functools import partial >>> chunker = partial(fixed_step_chunker, chk_size=2) >>> mapping = {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'} >>> list(chunk_mapping(mapping, chunker)) [{1: 'a', 2: 'b'}, {3: 'c', 4: 'd'}, {5: 'e'}]
- imbed.segmentation_util.fixed_step_chunker(it, chk_size, chk_step=None, *, start_at=None, stop_at=None, return_tail=True, chunk_egress=<class 'list'>)[source]#
a function to get (an iterator of) segments (bt, tt) of chunks from an iterator (or list) of the for [it_1, it_2…], given a chk_size, chk_step, and a start_at and a stop_at. The start_at, stop_at act like slices indices for a list: start_at is included and stop_at is excluded
- Parameters:
it (
Iterable[T]) – iterator of elements of any typechk_size (
int) – length of the chunksstart_at (
int|None) – index of the first term of the iterator at which we begin building the chunks (inclusive)stop_at (
int|None) – index of the last term from the iterator included in the chunksreturn_tail (
bool) – if set to false, only the chunks with max element with index less than stop_at are yielded if set to true, any chunks with minimum index value no more than stop_at are returned but they contain term with index no more than stop_at
- Return type:
- Returns:
an iterator of the chunks
- If stop_at is not None and return_tail is False:
will return all full chunks with maximum element index less than stop_at or until the iterator is exhausted. Only full chunks are returned here.
- If stop_at is not None and return_tail is True:
will return all full chunks as above along with possibly cut off chunks containing one term whose index is stop_at-1 or one (last) term which is the last element of it
- If stop_at is None and return_tail is False:
will return all full chunks with maximum element index less or equal to the last element of it
- If stop_at is None and return_tail is True:
will return all full chunks with maximum element index less or equal to the last element of it plus cut off chunks whose maximum term index is the last term of it
Examples
>>> list(fixed_step_chunker([1, 2, 3, 4, 5, 6, 7, 8], chk_size=3)) [[1, 2, 3], [4, 5, 6], [7, 8]] >>> list(fixed_step_chunker([1, 2, 3, 4, 5, 6, 7, 8], chk_size=3, return_tail=False)) [[1, 2, 3], [4, 5, 6]] >>> list(fixed_step_chunker([1, 2, 3, 4, 5, 6, 7, 8], chk_size=3)) [[1, 2, 3], [4, 5, 6], [7, 8]] >>> list(fixed_step_chunker([1, 2, 3, 4, 5, 6, 7, 8], chk_size=3, chk_step=2, return_tail=False)) [[1, 2, 3], [3, 4, 5], [5, 6, 7]] >>> chunks = fixed_step_chunker( ... range(1, 17, 1), chk_size=3, chk_step=4, ... start_at=1, stop_at=7, ... ) >>> list(chunks) [[2, 3, 4], [6, 7]]
- imbed.segmentation_util.rechunker(chks, chk_size, chk_step=None, start_at=None, stop_at=None, return_tail=False)[source]#
Takes an iterable of chks and produces another iterable of chunks. The chunks generated by the input chks iterable is assumed to be gap-less and without overlap, but these do not need to be of fixed size. The output will be though.
- Return type:
Iterable[Chunk]
- imbed.segmentation_util.wrapped_chunker(src, chunker, *, ingress=<function identity>, egress=<function identity>)[source]#
A function to extend chunking functionality to any source of iterables, with the ability to wrap the chunks in a function before yielding them.
- Parameters:
src (
IterableSrc) – an iterable of itemschunker (
Union[Callable[[IterableToChunk],Iterable[Chunk]],int]) – a chunker function or an integeringress (
Callable[[IterableSrc],IterableToChunk]) – a function to wrap the input iterableegress (
Callable[[Chunk],ChunkBasedObj]) – a function to wrap the output chunks
- Return type:
Iterable[ChunkBasedObj]- Returns:
an iterator of chunk-based objects
Examples
>>> list(wrapped_chunker(range(1, 6), 2)) [[1, 2], [3, 4], [5]]