AI Text Utils - GenTextDataset
This repository contains a reusable GenTextDataset, text extractor from gutenberg texts.
Installation
pip install ai-text-utils
Usage:
Download and Process Project Gutenberg Books
There are two ways to download and process books from Project Gutenberg:
1. Using GutenbergBooks Class (Recommended)
from ai_text_utils.text import GutenbergBooks
# Initialize with download directory
books = GutenbergBooks("my_gutenberg_books")
# Method 1: Download specific books by ID
book_ids = [1342, 74, 11] # Pride and Prejudice, Treasure Island, Alice in Wonderland
combined_text = books.get_books(book_ids)
# Method 2: Download range of books
range_ids = [
{'start_id': 108, 'num_books': 10}, # 10 books starting from 108
{'start_id': 3157, 'num_books': 5} # 5 books starting from 3157
]
combined_text = books.get_books(range_ids)
Details
-
GutenbergBooks Class Features:
- Smart caching: Downloads books only if not already present
- Progress tracking with tqdm
- Metadata tracking in JSON
- Proper text cleaning and processing
- Handles HTML content
- Removes headers, footers, and Project Gutenberg boilerplate
- Combines multiple books with separator
-
Arguments for get_books():
book_ids: Can be either:- List of individual book IDs:
[123, 105, 90] - List of range objects:
[{'start_id':108, 'num_books':30}]
- List of individual book IDs:
delay: Time to wait between downloads (default: 0.001s)
-
Returns: Combined text from all successfully downloaded books, with "<end_of_text>" separator between books
2. Legacy Method (Basic)
from ai_text_utils.text import get_text_from_gutenberg_books
txt = get_text_from_gutenberg_books(start_book_id=2007,
num_books=1,
keep_headers=False)
Legacy Details
- Returns: text start from book id 2007
- Arguments:
num_bookstells number of books from which text has to be extracted starting fromstart_book_id.keep_headerstells if header info like author name, date of publish, copyright info, payment details, etc needs to be included in extracted text. settingkeep_headersto False extracts only the content of the book.
convert text to tokens
from ai_text_utils.text import Tokenizer
tkn = Tokenizer()
tokens = tkn.encode("This is an example of tokenization")
print(f'Tokens={tokens}')
tokens = torch.Tensor(tokens).numpy()
print(f'Getting text back from tokens = {tkn.decode(tokens)}')
tokens = tkn.encode("This is an example of tokenization")
tokens = torch.Tensor(tokens)
print(f'Getting text back from tokens = {tkn.decode(tokens)}')
tokens = tkn.encode("This is an example of tokenization")
print(f'Getting text back from tokens = {tkn.decode(tokens)}')
tokens = tkn.encode("This is an example of tokenization")
tokens = tokens[-1]
print(f'Getting text back from tokens = {tkn.decode(tokens)}')
Details
- functions: tokenizer has 2 functions
encodeanddecode.encodeconverts text to list of tokens .decodeconverts list of tokens into text.decodefunction can take in list of tokens or numpy array of tokens or tensor array of tokens or single token of type int or type float and converts to text.
convert token list to dataset
from ai_text_utils.text import GenTextDataset, Tokenizer, get_text_from_gutenberg_books
txt = get_text_from_gutenberg_books(start_book_id=2007,
num_books=1,
keep_headers=False)
tokenizer = Tokenizer()
tokens = tokenizer.encode(txt)
dataset = GenTextDataset(tokens=tokens,
last_token_only=True,
seq_len=seq_len)
Details
- Returns: GenTextDataset returns dataset with (input, output)
- Arguments:
tokensis list of tokens . - say tokens =[1,2,3,4,5,6,7,8,9,10,11,12]
- last_token_only=False generates data as ([1,2,3,4,5],[2,3,4,5,6]), ([7,8,9,10,11],[8,9,10,11,12]). This type of dataset used for transformer next word prediction
- last_token_only=True generates data as ([1,2,3,4,5],[6]), ([2,3,4,5,6],[7]), ([3,4,5,6,7],[8]). This type of dataset used for LSTM next word prediction
seq_lentells how many tokens in each input . in above example seq_len=5
train val split and create dataloader
from ai_text_utils.text import train_val_split, create_dataloader
train_txt, val_txt = train_val_split(txt, train_ratio=0.9)
train_dl = create_dataloader(train_txt,seq_len=10, batch_size=3, shuffle=True,last_token_only=True)
val_dl=create_dataloader(val_txt,seq_len=10, batch_size=3, shuffle=True,last_token_only=True)
Details
Handy tools for splitting txt based on ratio and create_dataloader handy tool internally calls tokenizer and GenTextDataset .
you can also use tokenizer and GenTextDataset classes directly to create your own dataloader
Metadata
Release files for ai-text-utils 0.1.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ai_text_utils-0.1.6.tar.gz | 12.5 kB | Without provenance | Details for ai_text_utils-0.1.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ai_text_utils-0.1.6-py3-none-any.whl | Python 3 | none | any | Details for ai_text_utils-0.1.6-py3-none-any.whl |
Total release size: 25.1 kB
Release files / ai_text_utils-0.1.6.tar.gz
| Download URL | ai_text_utils-0.1.6.tar.gz |
|---|---|
| Size | 12.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c2c43d0786c309fb8c3e8a61ac524070fa95167bdf9f4ac65756a05693f395ef
|
|
BLAKE2b-256 checksum How to use checksums |
92de542af31107026b152a7f00cfefd08d6d6173f77b5ecae983815cf1981441
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.13.5 Windows/11
|
Release files / ai_text_utils-0.1.6-py3-none-any.whl
| Download URL | ai_text_utils-0.1.6-py3-none-any.whl |
|---|---|
| Size | 12.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0fc562d477efaa691416f5d5f41934dd5cbc9d36a076690d48b1a4bfffb49ddb
|
|
BLAKE2b-256 checksum How to use checksums |
745101f3d8c977afb19472cad757d4a4b288840ac01933cf04854f9c65cf6253
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.13.5 Windows/11
|
Security for ai-text-utils 0.1.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.