A lightweight text segmentation and tokenization library for Python.
Project description
rp-segmentation
rp-segmentation is a lightweight Python library for text segmentation, token normalization, and NLP-oriented preprocessing.
The package provides a simple and consistent API for splitting text into meaningful units, including sentences, paragraphs, and stopword-based segments. It is designed for text processing pipelines, NLP experimentation, semantic search, retrieval-augmented generation, and document preprocessing workflows.
Features
- Sentence segmentation using NLTK.
- Paragraph segmentation based on structural line breaks.
- Stopword-based segmentation every
Nstopwords. - Unicode-aware token extraction.
- Optional stopword removal.
- Typed package support through
py.typed. - Lightweight and easy to integrate into NLP pipelines.
Installation
pip install rp-segmentation
Requirements
- Python 3.10 or higher.
- NLTK.
- regex.
NLTK Resources
rp-segmentation relies on external NLTK resources for sentence tokenization and stopword handling.
You can install the required resources manually:
python -m nltk.downloader punkt_tab
python -m nltk.downloader stopwords
Or install them directly from Python:
from rp_segmentation import ensure_required_nltk_resources
ensure_required_nltk_resources()
Basic Usage
from rp_segmentation import (
sentence_segmentation,
paragraph_segmentation,
n_stop_words_segmentation,
)
text = """
Hello, Pablo. This is a simple test.
This is another paragraph with additional content.
It can be used for text processing workflows.
"""
print(sentence_segmentation(text))
print(paragraph_segmentation(text))
print(n_stop_words_segmentation(text, n=3))
Available Methods
sentence_segmentation
sentence_segmentation(
text: str,
language: str = "english",
remove_stopwords: bool = False,
) -> list[str]
Segments a text into sentences using NLTK and applies the package's internal normalization strategy to each resulting segment.
Example
from rp_segmentation import sentence_segmentation
text = "Hello, John. How are you?"
segments = sentence_segmentation(text)
print(segments)
Output:
["hello john", "how are you"]
With stopword removal:
segments = sentence_segmentation(
text,
language="english",
remove_stopwords=True,
)
print(segments)
Output:
["hello john"]
paragraph_segmentation
paragraph_segmentation(
text: str,
language: str = "english",
remove_stopwords: bool = False,
) -> list[str]
Segments a text into paragraphs using double or multiple line breaks. Each paragraph is normalized before being returned.
Example
from rp_segmentation import paragraph_segmentation
text = "First paragraph.\n\nSecond paragraph."
segments = paragraph_segmentation(text)
print(segments)
Output:
["first paragraph", "second paragraph"]
n_stop_words_segmentation
n_stop_words_segmentation(
text: str,
language: str = "english",
n: int = 5,
remove_stopwords: bool = False,
) -> list[str]
Segments a text every N stopwords. This strategy is useful when working with natural language texts where stopword distribution can help define semantic or syntactic boundaries.
Example
from rp_segmentation import n_stop_words_segmentation
text = "Alpha the beta and gamma is delta of omega."
segments = n_stop_words_segmentation(
text,
language="english",
n=2,
)
print(segments)
Output:
[
"alpha the beta and",
"gamma is delta of",
"omega",
]
With stopword removal:
segments = n_stop_words_segmentation(
text,
language="english",
n=2,
remove_stopwords=True,
)
print(segments)
Output:
[
"alpha beta",
"gamma delta",
"omega",
]
Use Cases
rp-segmentation can be used in a wide range of text processing tasks, including:
- Natural Language Processing.
- Text normalization.
- Document preprocessing.
- Semantic search.
- Embedding preparation.
- Retrieval-Augmented Generation pipelines.
- Educational and research-oriented NLP projects.
Local Development
Clone the repository:
git clone https://github.com/pablonicolasr777/rp-segmentation.git
cd rp-segmentation
Create and activate a virtual environment:
python -m venv .venv
.venv\Scripts\Activate.ps1
Install the package with development dependencies:
pip install -e ".[dev]"
Install the required NLTK resources:
python -m nltk.downloader punkt_tab
python -m nltk.downloader stopwords
Run code quality checks:
ruff check .
mypy src
pytest --cov=rp_segmentation --cov-report=term-missing
Project Structure
rp-segmentation/
├── src/
│ └── rp_segmentation/
│ ├── __init__.py
│ ├── segmenters.py
│ ├── nltk_resources.py
│ ├── exceptions.py
│ └── py.typed
├── tests/
│ └── test_segmenters.py
├── docs/
├── .github/
│ └── workflows/
│ ├── ci.yml
│ └── publish.yml
├── README.md
├── CHANGELOG.md
├── CONTRIBUTING.md
├── SECURITY.md
├── LICENSE
├── pyproject.toml
├── requirements-dev.txt
└── .gitignore
Authors
- Pablo Nicolás Ramos
- Ricardo Daniel Perez
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rp_segmentation-0.1.1.tar.gz.
File metadata
- Download URL: rp_segmentation-0.1.1.tar.gz
- Upload date:
- Size: 14.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4475ab89b13a8bc6e1a86d0061e2664782b45d157a336c40b7892f4794ac0322
|
|
| MD5 |
4102914ec94c69efb4f3190cea7121d5
|
|
| BLAKE2b-256 |
ffb3a4a2959fd85a1001fe4b0472674d2ec66f80a3f44986d364d2b205616e88
|
Provenance
The following attestation bundles were made for rp_segmentation-0.1.1.tar.gz:
Publisher:
publish.yml on pablonicolasr/rp-segmentation
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rp_segmentation-0.1.1.tar.gz -
Subject digest:
4475ab89b13a8bc6e1a86d0061e2664782b45d157a336c40b7892f4794ac0322 - Sigstore transparency entry: 2010479382
- Sigstore integration time:
-
Permalink:
pablonicolasr/rp-segmentation@5e52f2c07c98a3f401ae6f6ac062d602f162540a -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/pablonicolasr
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5e52f2c07c98a3f401ae6f6ac062d602f162540a -
Trigger Event:
push
-
Statement type:
File details
Details for the file rp_segmentation-0.1.1-py3-none-any.whl.
File metadata
- Download URL: rp_segmentation-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc7cc9080906f3ffac0dad0c9f15c0e554908573b921ea90dfff70be19ab6b54
|
|
| MD5 |
f7fc08c18632391ba4b7067d022f04fd
|
|
| BLAKE2b-256 |
c70d7bb5c4ebfe2190b0ee70d2104348a28e661b23285ac45563044d41c14fc6
|
Provenance
The following attestation bundles were made for rp_segmentation-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on pablonicolasr/rp-segmentation
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rp_segmentation-0.1.1-py3-none-any.whl -
Subject digest:
cc7cc9080906f3ffac0dad0c9f15c0e554908573b921ea90dfff70be19ab6b54 - Sigstore transparency entry: 2010479512
- Sigstore integration time:
-
Permalink:
pablonicolasr/rp-segmentation@5e52f2c07c98a3f401ae6f6ac062d602f162540a -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/pablonicolasr
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5e52f2c07c98a3f401ae6f6ac062d602f162540a -
Trigger Event:
push
-
Statement type: