Comments (2)
Hi there!. Thought you might find the following code snippet helpful. Please note that this is a basic example and might need further testing for edge cases. It aims to demonstrate how you can set both minimum and maximum character length for resulting chunks.
# Set minimum chunk size
min_chunk_size = 200
# Set maximum chunk size
chunk_size = 350
# Initialize empty lists to store the final chunks, accumulated text, and bounding box coordinates
final_chunks = []
texts = ""
cords_dict = []
# Iterate over the document chunks
for chunk in doc.chunks():
# Extract the text and bounding box coordinates for the current chunk
chunk_text = chunk.to_context_text()
chunk_cords = chunk.block_json
# Check if adding the current chunk to the accumulated text exceeds the maximum chunk size
if len(texts) + len(chunk_text) <= chunk_size:
# If within the limit, append the text and coordinates to the current chunk
texts += chunk_text
cords_dict.append(chunk_cords)
# Check if the accumulated text meets or exceeds the minimum chunk size
elif len(texts) >= min_chunk_size:
# If yes, create a new chunk with the accumulated text and coordinates
final_chunks.append({
'context_text': texts,
'cords': cords_dict
})
# Reset the accumulated text and coordinates for the new chunk
texts = chunk_text
cords_dict = [chunk_cords]
else:
# If adding the chunk would make the current chunk size less than min_chunk_size,
# skip to the next chunk without creating a new chunk
texts = chunk_text
cords_dict = [chunk_cords]
# Add the last chunk if it has content and meets the minimum chunk size
if len(texts) >= min_chunk_size:
final_chunks.append({
'context_text': texts,
'cords': cords_dict
})
@ansukla What do you think will this work? or will it degrade the chunk quality?
from llmsherpa.
Agreed! Will add it to the list of things and happy to take PRs if anyone wants to pick this up.
from llmsherpa.
Related Issues (20)
- Two column PDFs HOT 2
- [BUG] Duplicated nodes in output from `to_html()` and `to_text()` HOT 3
- Certificate issues HOT 4
- Receiving 'urllib3.exceptions.LocationValueError: No host specified.' even though I have a local server up and running HOT 2
- LayoutReader.read handling levels incorrectly
- Does the llm give sentence wise bbox or title wise bbox
- absolute path Issue in read_pdf method HOT 1
- llmsherpa is Missing Information
- Local files parsing Error HOT 2
- KeyError: 'style' while running on local using docker image HOT 2
- Server not responsive HOT 4
- Class TableHeader to_html method does not use correct table tags
- colSpan should be colspan in TableCell class to_html method
- Issue with Example Code HOT 3
- Suggestions for Improvements (compiled)
- pdf_reader.read_pdf(pdf_url) cannot read local pdf path HOT 1
- Mismatch between pypi and repo version HOT 1
- Issue while accessing llmsherpa api url HOT 1
- LLMSHERPA API LOCAL HOSTING HOT 2
- Multi-column sections extending to to multiple pages fails
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from llmsherpa.