ctxsplit 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ctxsplit-0.1.0/LICENSE +9 -0
- ctxsplit-0.1.0/PKG-INFO +46 -0
- ctxsplit-0.1.0/README.md +30 -0
- ctxsplit-0.1.0/ctxsplit.egg-info/PKG-INFO +46 -0
- ctxsplit-0.1.0/ctxsplit.egg-info/SOURCES.txt +9 -0
- ctxsplit-0.1.0/ctxsplit.egg-info/dependency_links.txt +1 -0
- ctxsplit-0.1.0/ctxsplit.egg-info/entry_points.txt +2 -0
- ctxsplit-0.1.0/ctxsplit.egg-info/top_level.txt +1 -0
- ctxsplit-0.1.0/ctxsplit.py +66 -0
- ctxsplit-0.1.0/pyproject.toml +27 -0
- ctxsplit-0.1.0/setup.cfg +4 -0
ctxsplit-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 episodely
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
ctxsplit-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ctxsplit
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Split long text into overlapping chunks for LLM context windows.
|
|
5
|
+
Author: episodely
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/episodely/ctxsplit
|
|
8
|
+
Project-URL: Issues, https://github.com/episodely/ctxsplit/issues
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.8
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Dynamic: license-file
|
|
16
|
+
|
|
17
|
+
# ctxsplit
|
|
18
|
+
|
|
19
|
+
Split long text into overlapping chunks for LLM context windows or RAG. Breaks on paragraphs first, then sentences (including Arabic `؟`), then words, and carries a short overlap into the next chunk so context is not lost. Pure Python, no dependencies.
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
from ctxsplit import split
|
|
23
|
+
chunks = split(open("book.txt").read(), max_chars=2000, overlap=200)
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
python -m ctxsplit book.txt --max-chars 1500 --overlap 150 # JSON list of chunks
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Sizes are in characters. Divide by about 4 for a rough English token count (see tokenwise).
|
|
31
|
+
|
|
32
|
+
## Install
|
|
33
|
+
|
|
34
|
+
```
|
|
35
|
+
pip install ctxsplit
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Or copy `ctxsplit.py` into your project, it has no dependencies.
|
|
39
|
+
|
|
40
|
+
## Test
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
python -m pytest
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
MIT licensed.
|
ctxsplit-0.1.0/README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# ctxsplit
|
|
2
|
+
|
|
3
|
+
Split long text into overlapping chunks for LLM context windows or RAG. Breaks on paragraphs first, then sentences (including Arabic `؟`), then words, and carries a short overlap into the next chunk so context is not lost. Pure Python, no dependencies.
|
|
4
|
+
|
|
5
|
+
```python
|
|
6
|
+
from ctxsplit import split
|
|
7
|
+
chunks = split(open("book.txt").read(), max_chars=2000, overlap=200)
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
python -m ctxsplit book.txt --max-chars 1500 --overlap 150 # JSON list of chunks
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Sizes are in characters. Divide by about 4 for a rough English token count (see tokenwise).
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
pip install ctxsplit
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Or copy `ctxsplit.py` into your project, it has no dependencies.
|
|
23
|
+
|
|
24
|
+
## Test
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
python -m pytest
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
MIT licensed.
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ctxsplit
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Split long text into overlapping chunks for LLM context windows.
|
|
5
|
+
Author: episodely
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/episodely/ctxsplit
|
|
8
|
+
Project-URL: Issues, https://github.com/episodely/ctxsplit/issues
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.8
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Dynamic: license-file
|
|
16
|
+
|
|
17
|
+
# ctxsplit
|
|
18
|
+
|
|
19
|
+
Split long text into overlapping chunks for LLM context windows or RAG. Breaks on paragraphs first, then sentences (including Arabic `؟`), then words, and carries a short overlap into the next chunk so context is not lost. Pure Python, no dependencies.
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
from ctxsplit import split
|
|
23
|
+
chunks = split(open("book.txt").read(), max_chars=2000, overlap=200)
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
python -m ctxsplit book.txt --max-chars 1500 --overlap 150 # JSON list of chunks
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Sizes are in characters. Divide by about 4 for a rough English token count (see tokenwise).
|
|
31
|
+
|
|
32
|
+
## Install
|
|
33
|
+
|
|
34
|
+
```
|
|
35
|
+
pip install ctxsplit
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Or copy `ctxsplit.py` into your project, it has no dependencies.
|
|
39
|
+
|
|
40
|
+
## Test
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
python -m pytest
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
MIT licensed.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ctxsplit
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""Split long text into overlapping chunks that respect headings, paragraphs and sentences."""
|
|
2
|
+
import re, sys, argparse, json
|
|
3
|
+
|
|
4
|
+
def _units(text):
|
|
5
|
+
out = []
|
|
6
|
+
for para in re.split(r"\n\s*\n", text.strip()):
|
|
7
|
+
if len(para) > 0:
|
|
8
|
+
out.append(para)
|
|
9
|
+
return out
|
|
10
|
+
|
|
11
|
+
def _sentences(p):
|
|
12
|
+
return [s for s in re.split(r"(?<=[.!?؟])\s+", p) if s]
|
|
13
|
+
|
|
14
|
+
def split(text: str, max_chars: int = 2000, overlap: int = 200):
|
|
15
|
+
"""Return a list of chunks of at most max_chars (except a single unbreakable word)."""
|
|
16
|
+
if not isinstance(text, str):
|
|
17
|
+
raise TypeError("text must be a string")
|
|
18
|
+
if max_chars < 1 or overlap < 0:
|
|
19
|
+
raise ValueError("max_chars must be at least 1 and overlap cannot be negative")
|
|
20
|
+
if max_chars <= overlap:
|
|
21
|
+
raise ValueError("max_chars must be larger than overlap")
|
|
22
|
+
pieces = []
|
|
23
|
+
for para in _units(text):
|
|
24
|
+
if len(para) <= max_chars:
|
|
25
|
+
pieces.append(para)
|
|
26
|
+
continue
|
|
27
|
+
for s in _sentences(para):
|
|
28
|
+
while len(s) > max_chars:
|
|
29
|
+
cut = s.rfind(" ", 0, max_chars)
|
|
30
|
+
cut = cut if cut > 0 else max_chars
|
|
31
|
+
pieces.append(s[:cut]); s = s[cut:].lstrip()
|
|
32
|
+
pieces.append(s)
|
|
33
|
+
chunks, cur = [], ""
|
|
34
|
+
for p in pieces:
|
|
35
|
+
sep = "\n\n" if cur else ""
|
|
36
|
+
if len(cur) + len(sep) + len(p) <= max_chars:
|
|
37
|
+
cur += sep + p
|
|
38
|
+
else:
|
|
39
|
+
if cur:
|
|
40
|
+
chunks.append(cur)
|
|
41
|
+
tail = cur[-overlap:] if overlap and cur else ""
|
|
42
|
+
if tail:
|
|
43
|
+
sp = tail.find(" ")
|
|
44
|
+
tail = tail[sp + 1:] if sp != -1 else tail
|
|
45
|
+
cur = (tail + " " + p).strip() if tail and len(tail) + 1 + len(p) <= max_chars else p
|
|
46
|
+
if cur:
|
|
47
|
+
chunks.append(cur)
|
|
48
|
+
return chunks
|
|
49
|
+
|
|
50
|
+
def main(argv=None):
|
|
51
|
+
ap = argparse.ArgumentParser(prog="ctxsplit")
|
|
52
|
+
ap.add_argument("file", nargs="?")
|
|
53
|
+
ap.add_argument("--max-chars", type=int, default=2000)
|
|
54
|
+
ap.add_argument("--overlap", type=int, default=200)
|
|
55
|
+
a = ap.parse_args(argv)
|
|
56
|
+
try:
|
|
57
|
+
text = open(a.file, encoding="utf-8").read() if a.file else sys.stdin.read()
|
|
58
|
+
chunks = split(text, a.max_chars, a.overlap)
|
|
59
|
+
except (OSError, UnicodeDecodeError, ValueError) as e:
|
|
60
|
+
print(f"error: {e}", file=sys.stderr)
|
|
61
|
+
return 1
|
|
62
|
+
print(json.dumps(chunks, ensure_ascii=False, indent=2))
|
|
63
|
+
return 0
|
|
64
|
+
|
|
65
|
+
if __name__ == "__main__":
|
|
66
|
+
sys.exit(main())
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "ctxsplit"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Split long text into overlapping chunks for LLM context windows."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
requires-python = ">=3.8"
|
|
12
|
+
authors = [{ name = "episodely" }]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"License :: OSI Approved :: MIT License",
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
[project.urls]
|
|
20
|
+
Homepage = "https://github.com/episodely/ctxsplit"
|
|
21
|
+
Issues = "https://github.com/episodely/ctxsplit/issues"
|
|
22
|
+
|
|
23
|
+
[project.scripts]
|
|
24
|
+
ctxsplit = "ctxsplit:main"
|
|
25
|
+
|
|
26
|
+
[tool.setuptools]
|
|
27
|
+
py-modules = ["ctxsplit"]
|
ctxsplit-0.1.0/setup.cfg
ADDED