|
| 1 | +(BaseSplitter)= |
| 2 | +# BaseSplitter |
| 3 | + |
| 4 | +## Overview |
| 5 | + |
| 6 | +`BaseSplitter` is the abstract base class for all text splitter transformers in the ScaleDP library. It extends PySpark's `Transformer` class and provides common functionality for splitting documents into chunks. This class defines the interface and shared parameters for all splitter implementations. |
| 7 | + |
| 8 | +## Inheritance |
| 9 | + |
| 10 | +- Extends PySpark's `Transformer` for ML pipeline compatibility. |
| 11 | +- Mixes in the following parameter mixins: |
| 12 | + - `HasInputCol` - Input column containing documents |
| 13 | + - `HasOutputCol` - Output column for results |
| 14 | + - `HasKeepInputData` - Whether to preserve input data |
| 15 | + - `HasChunkSize` - Maximum chunk size |
| 16 | + - `HasChunkOverlap` - Overlap between chunks |
| 17 | + - `HasNumPartitions` - Partition control |
| 18 | + - `HasPartitionMap` - Enable distributed processing mode |
| 19 | + - `HasWhiteList` - Whitelist filtering support |
| 20 | + |
| 21 | +## Key Features |
| 22 | + |
| 23 | +- **PySpark Integration**: Full compatibility with PySpark ML pipelines |
| 24 | +- **Serialization**: Support for reading and writing model parameters |
| 25 | +- **Flexible Configuration**: Extensive parameters for customization |
| 26 | +- **Extensible Design**: Foundation for specialized splitter implementations |
| 27 | +- **Batch Processing**: Support for both local and distributed processing modes |
| 28 | + |
| 29 | +## Class Hierarchy |
| 30 | + |
| 31 | +``` |
| 32 | +BaseSplitter |
| 33 | +├── BaseTextSplitter |
| 34 | +│ └── TextSplitter (concrete implementation) |
| 35 | +``` |
| 36 | + |
| 37 | +## Parameters |
| 38 | + |
| 39 | +| Parameter | Type | Description | Default | |
| 40 | +|-------------------|---------|--------------------------------------------------|-----------------------------| |
| 41 | +| inputCol | str | Input column name | varies by implementation | |
| 42 | +| outputCol | str | Output column name | varies by implementation | |
| 43 | +| keepInputData | bool | Keep input columns in output | True | |
| 44 | +| chunkSize | int | Size of each chunk | 500 | |
| 45 | +| chunkOverlap | int | Overlap between consecutive chunks | 0 | |
| 46 | +| numPartitions | int | Number of partitions | 1 | |
| 47 | +| partitionMap | bool | Use partitioned mapping (pandas_udf mode) | False | |
| 48 | +| whiteList | list | Whitelist of allowed items | [] | |
| 49 | + |
| 50 | +## Abstract Methods |
| 51 | + |
| 52 | +Subclasses must implement the following abstract methods: |
| 53 | + |
| 54 | +### transform(dataset) |
| 55 | +Transforms a Spark DataFrame by applying the splitter logic. |
| 56 | + |
| 57 | +**Parameters:** |
| 58 | +- `dataset` (pyspark.sql.DataFrame): Input DataFrame |
| 59 | + |
| 60 | +**Returns:** |
| 61 | +- (pyspark.sql.DataFrame): DataFrame with split results |
| 62 | + |
| 63 | +## Usage Guidelines |
| 64 | + |
| 65 | +`BaseSplitter` is an abstract class and should not be instantiated directly. Instead, use concrete implementations like: |
| 66 | + |
| 67 | +- [`TextSplitter`](./text_splitter.md) - Semantic text splitting |
| 68 | + |
| 69 | +```python |
| 70 | +# Correct: Use concrete implementation |
| 71 | +from scaledp.models.splitters.TextSplitter import TextSplitter |
| 72 | + |
| 73 | +splitter = TextSplitter(chunk_size=500, chunk_overlap=50) |
| 74 | +``` |
| 75 | + |
| 76 | +```python |
| 77 | +# Incorrect: Do not instantiate BaseSplitter directly |
| 78 | +from scaledp.models.splitters.BaseSplitter import BaseSplitter |
| 79 | + |
| 80 | +# This will raise an error |
| 81 | +splitter = BaseSplitter() # Error! |
| 82 | +``` |
| 83 | + |
| 84 | +## Creating Custom Splitters |
| 85 | + |
| 86 | +To create a custom splitter, inherit from `BaseSplitter` or `BaseTextSplitter`: |
| 87 | + |
| 88 | +```python |
| 89 | +from scaledp.models.splitters.BaseTextSplitter import BaseTextSplitter |
| 90 | +from scaledp.schemas.Document import Document |
| 91 | +from scaledp.schemas.TextChunks import TextChunks |
| 92 | + |
| 93 | +class CustomSplitter(BaseTextSplitter): |
| 94 | + """Custom splitter implementation.""" |
| 95 | + |
| 96 | + def split(self, document: Document) -> TextChunks: |
| 97 | + """Implement custom splitting logic.""" |
| 98 | + # Your splitting algorithm here |
| 99 | + chunks = self._split_text(document.text) |
| 100 | + return TextChunks( |
| 101 | + path=document.path, |
| 102 | + chunks=chunks, |
| 103 | + exception="", |
| 104 | + processing_time=0.0 |
| 105 | + ) |
| 106 | +``` |
| 107 | + |
| 108 | +## Pipeline Integration |
| 109 | + |
| 110 | +`BaseSplitter` and its subclasses are designed to work seamlessly with PySpark pipelines: |
| 111 | + |
| 112 | +```python |
| 113 | +from pyspark.ml import Pipeline |
| 114 | +from scaledp.models.splitters.TextSplitter import TextSplitter |
| 115 | + |
| 116 | +# Create pipeline stages |
| 117 | +splitter = TextSplitter(chunk_size=500) |
| 118 | + |
| 119 | +# Create and fit pipeline |
| 120 | +pipeline = Pipeline(stages=[splitter]) |
| 121 | +model = pipeline.fit(training_data) |
| 122 | + |
| 123 | +# Transform data |
| 124 | +results = model.transform(test_data) |
| 125 | +``` |
| 126 | + |
| 127 | +## Serialization |
| 128 | + |
| 129 | +All splitters support PySpark's read/write functionality: |
| 130 | + |
| 131 | +```python |
| 132 | +# Save a model |
| 133 | +splitter = TextSplitter(chunk_size=500) |
| 134 | +splitter.write().overwrite().save("path/to/splitter") |
| 135 | + |
| 136 | +# Load a model |
| 137 | +loaded_splitter = TextSplitter.load("path/to/splitter") |
| 138 | +``` |
| 139 | + |
| 140 | +## Related Classes |
| 141 | + |
| 142 | +- [`BaseTextSplitter`](./base_text_splitter.md) - Abstract base for text splitters |
| 143 | +- [`TextSplitter`](./text_splitter.md) - Concrete semantic text splitter implementation |
| 144 | +- [`Document`](#Document) - Input document schema |
| 145 | +- [`TextChunks`](#TextChunks) - Output text chunks schema |
| 146 | + |
| 147 | +## See Also |
| 148 | + |
| 149 | +- [PySpark ML Transformers](https://spark.apache.org/docs/latest/ml-pipeline.html) |
| 150 | +- [Transformer API](https://spark.apache.org/docs/latest/api/python/reference/api/pyspark.ml.Transformer.html) |
0 commit comments