Code Monkey home page Code Monkey logo

Comments (2)

amn-max avatar amn-max commented on August 24, 2024 1

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.

ansukla avatar ansukla commented on August 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.