Skip to main content

Sentence Splitter

A Python package for sentence splitting using a pre-trained transformer model.

Description

Sentence Splitter is a Python package that provides accurate sentence segmentation using a transformer-based token classification model. The model is automatically downloaded from Hugging Face Hub on first use and cached locally for future use. It's designed to handle long texts efficiently and supports GPU acceleration if available.

Features

  • Transformer-Based Model: Leverages a pre-trained transformer model for high-accuracy sentence splitting.
  • Automatic Model Download: The model is automatically downloaded from Hugging Face Hub on first use and cached locally.
  • Easy to Use: Simple API for quick integration into your projects.
  • Handles Long Texts: Efficiently processes long texts by splitting them into manageable chunks.
  • GPU Acceleration: Automatically utilizes CUDA if available for faster processing.

Installation

Install the package via pip:

pip install iges-sentence-splitter

Requirements

  • Python 3.6 or higher
  • torch
  • transformers

Note: These dependencies will be installed automatically when you install the package via pip.

First Use

On first use, the model (~1GB) will be automatically downloaded from Hugging Face Hub and cached locally in ~/.cache/huggingface/. Subsequent uses will load the model from cache instantly.

Usage

Basic Example

from sentence_splitter.splitter import SentenceSplitter

# Initialize the splitter
splitter = SentenceSplitter()

# Input text
text = "This is a test. Here is another sentence. And yet another one!"

# Get sentences
sentences = splitter.split(text)

print(sentences)

Output:

['This is a test.', 'Here is another sentence.', 'And yet another one!']

Processing Long Texts

The split method can handle long texts by splitting them into chunks. You can adjust the parameters as needed:

sentences = splitter.split(
    text,
    max_seq_len=512,   # Maximum sequence length for each chunk
    stride=100,        # Overlap between chunks to preserve context
    batch_size=4       # Number of chunks to process at once
)

API Reference

SentenceSplitter

A class for splitting text into sentences using a pre-trained transformer model.

Initialization

splitter = SentenceSplitter(device=None, efficient_mode=False)
  • Parameters:
    • device (str, optional): The device to run the model on ('cuda' or 'cpu'). Defaults to 'cuda' if available, otherwise 'cpu'.
    • efficient_mode (bool, optional): Whether to run the model in 8-bit precision for faster computing

Methods

  • split(text, max_seq_len=512, stride=100, batch_size=4)

    Splits the input text into sentences.

    • Parameters:
      • text (str): The text to split.
      • max_seq_len (int, optional): Maximum sequence length for the model. Defaults to 512.
      • stride (int, optional): Number of tokens to overlap between chunks. Defaults to 100.
      • batch_size (int, optional): Number of chunks to process simultaneously. Defaults to 4.
    • Returns:
      • List[str]: A list of sentences.

How It Works

The package uses a token classification model that labels each token as:

  • B: Beginning of a sentence.
  • E: End of a sentence.
  • I: Inside a sentence.

By processing the tokens and their predicted labels, the splitter reconstructs the sentences accurately, even in complex texts.

Example: Splitting Complex Text

text = """
Despite the rain, the match continued. Players were determined; fans were cheering. 
"Unbelievable!" shouted the commentator. It's a night to remember.
"""

sentences = splitter.split(text)

for i, sentence in enumerate(sentences, 1):
    print(f"Sentence {i}: {sentence}")

Output:

Sentence 1: Despite the rain, the match continued.
Sentence 2: Players were determined; fans were cheering.
Sentence 3: "Unbelievable!" shouted the commentator.
Sentence 4: It's a night to remember.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Author

Acknowledgments

Contact

For any questions or suggestions, feel free to reach out via email.


Release files for iges-sentence-splitter 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for iges-sentence-splitter 0.2.1
File Size Uploaded
iges_sentence_splitter-0.2.1.tar.gz 5.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for iges-sentence-splitter 0.2.1
File Interpreter ABI Platform
iges_sentence_splitter-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.9 kB

Release files / iges_sentence_splitter-0.2.1.tar.gz

Download URL iges_sentence_splitter-0.2.1.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
11d7bffcc1de1ff1e73aaa5af4f5cf9d7e6a1b942478c58f9b95fc21b4fba314
BLAKE2b-256 checksum
How to use checksums
760dcce1030b76bd98f60610611086f0acbbc9e07bea0cbedb9ba878ffca98e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.8

Release files / iges_sentence_splitter-0.2.1-py3-none-any.whl

Download URL iges_sentence_splitter-0.2.1-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a41df1e7ff02b4c1d70cc39f0ca732634fcf7b472376c53cbc18dcd37f751756
BLAKE2b-256 checksum
How to use checksums
0ce6904d95eea0cd787526def2ba6639ce08129410a6fab7267c99f04eee006e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.8

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

1 release file

0.1.13

2 release files

0.1.12

2 release files

0.1.11

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page