UNPKG

chonkie

Version:

🦛 CHONK your texts in TS with Chonkie!✨The no-nonsense lightweight and efficient chunking library.

137 lines (136 loc) • 6.72 kB
import { Chunk } from './base'; /** * Represents the essential data for a sentence within a text. * * @property text - The actual sentence string as it appears in the source text. * @property startIndex - The zero-based index indicating where the sentence starts in the original text. * @property endIndex - The zero-based index indicating where the sentence ends in the original text (inclusive). * @property tokenCount - The number of tokens (words or subwords) in the sentence, useful for NLP tasks. */ export interface SentenceData { text: string; startIndex: number; endIndex: number; tokenCount: number; } /** * Class to represent a sentence. * * Represents a single sentence within a text, including its text, position, and token count. * * @class * @param {SentenceData} data - The data required to construct a Sentence instance. * @property {string} text - The text of the sentence. * @property {number} startIndex - The starting index of the sentence in the original text. * @property {number} endIndex - The ending index of the sentence in the original text. * @property {number} tokenCount - The number of tokens in the sentence. * @property {number[]} [embedding] - The embedding vector for the sentence (array of numbers, or null if not present). * * @method toString Returns a string representation of the Sentence. * @returns {string} * * @method toDict Returns the Sentence as a dictionary-like object. * @returns {SentenceData} * * @method static fromDict Creates a Sentence object from a dictionary-like object. * @param {SentenceData} data - The data to create the Sentence from. * @returns {Sentence} */ export declare class Sentence { /** The text of the sentence */ text: string; /** The starting index of the sentence in the original text */ startIndex: number; /** The ending index of the sentence in the original text */ endIndex: number; /** The number of tokens in the sentence */ tokenCount: number; constructor(data: SentenceData); /** Return a string representation of the Sentence */ toString(): string; /** Return the Sentence as a dictionary-like object */ toDict(): SentenceData; /** Create a Sentence object from a dictionary-like object */ static fromDict(data: SentenceData): Sentence; } /** * Represents the essential data for a chunk of sentences within a text. * * @property text - The combined text of all sentences in the chunk as it appears in the source text. * @property startIndex - The zero-based index indicating where the chunk starts in the original text. * @property endIndex - The zero-based index indicating where the chunk ends in the original text (inclusive). * @property tokenCount - The total number of tokens (words or subwords) in the chunk, useful for NLP tasks. * @property sentences - An array of SentenceData objects, each representing an individual sentence within the chunk. */ interface SentenceChunkData { text: string; startIndex: number; endIndex: number; tokenCount: number; sentences: SentenceData[]; embedding?: number[]; } /** * Represents a chunk of one or more sentences within a text. * * A SentenceChunk groups together multiple {@link Sentence} objects, providing their combined text, position, and token count within the original text. * * @class * @extends Chunk * * @param {Object} data - Data to construct a SentenceChunk instance. * @param {string} data.text - Combined text of all sentences in the chunk. * @param {number} data.startIndex - Zero-based index where the chunk starts in the original text. * @param {number} data.endIndex - Zero-based index where the chunk ends in the original text (inclusive). * @param {number} data.tokenCount - Total number of tokens in the chunk. * @param {Sentence[]} data.sentences - Array of {@link Sentence} objects in the chunk. * * @property {string} text - Combined text of all sentences in the chunk. * @property {number} startIndex - Starting index of the chunk in the original text. * @property {number} endIndex - Ending index of the chunk in the original text. * @property {number} tokenCount - Total number of tokens in the chunk. * @property {Sentence[]} sentences - List of {@link Sentence} objects in the chunk. * * @method toString Returns a detailed string representation of the SentenceChunk, including its text, start and end indices, token count, and a list of all contained sentences with their metadata. * @method toDict Returns the SentenceChunk as a plain object (see {@link SentenceChunkData}). * @method static fromDict Creates a SentenceChunk from a {@link SentenceChunkData} object. */ export declare class SentenceChunk extends Chunk { /** List of sentences in the chunk */ sentences: Sentence[]; constructor(data: { text: string; startIndex: number; endIndex: number; tokenCount: number; sentences: Sentence[]; embedding?: number[]; }); /** * Returns a detailed string representation of the SentenceChunk, including its text, start and end indices, token count, and a list of all contained sentences with their metadata. * * This method overrides the base {@link Chunk} toString method to provide a more informative output, which is especially useful for debugging and logging. Each sentence in the chunk is represented using its own toString method, and all sentences are included in the output. * * @returns {string} A string describing the SentenceChunk and all its sentences, e.g., * SentenceChunk(text=..., startIndex=..., endIndex=..., tokenCount=..., sentences=[Sentence(...), ...]) */ toString(): string; /** * Returns the SentenceChunk as a dictionary-like object. * * This method extends the base {@link Chunk} toDict method to include the sentences in the chunk. * * @returns {SentenceChunkData} A dictionary-like object containing the chunk's text, start and end indices, token count, and an array of sentence data. /** Return the SentenceChunk as a dictionary-like object */ toDict(): SentenceChunkData; /** * Creates a SentenceChunk object from a dictionary-like object. * * This method extends the base {@link Chunk} fromDict method to include the sentences in the chunk. * * @param {SentenceChunkData} data - A dictionary-like object containing the chunk's text, start and end indices, token count, and an array of sentence data. * @returns {SentenceChunk} A new SentenceChunk object created from the provided dictionary-like object. */ static fromDict(data: SentenceChunkData): SentenceChunk; } export {};