UNPKG

chonkie

Version:

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

69 lines • 3.18 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SemanticChunk = exports.SemanticSentence = void 0; const sentence_1 = require("./sentence"); const sentence_2 = require("./sentence"); class SemanticSentence extends sentence_1.Sentence { constructor(data) { var _a; super(data); this.embedding = (_a = data.embedding) !== null && _a !== void 0 ? _a : null; } /** Return the SemanticSentence as a dictionary-like object */ toDict() { var _a; return Object.assign(Object.assign({}, super.toDict()), { embedding: (_a = this.embedding) !== null && _a !== void 0 ? _a : null }); } /** Create a SemanticSentence object from a dictionary */ static fromDict(data) { // Defensive copy to avoid mutating input const { embedding } = data, rest = __rest(data, ["embedding"]); return new SemanticSentence(Object.assign(Object.assign({}, rest), { embedding: embedding !== null && embedding !== void 0 ? embedding : null })); } /** Return a string representation of the SemanticSentence */ toString() { return `SemanticSentence(text=${this.text}, startIndex=${this.startIndex}, endIndex=${this.endIndex}, tokenCount=${this.tokenCount}, embedding=${JSON.stringify(this.embedding)})`; } } exports.SemanticSentence = SemanticSentence; class SemanticChunk extends sentence_2.SentenceChunk { constructor(data) { super({ text: data.text, startIndex: data.startIndex, endIndex: data.endIndex, tokenCount: data.tokenCount, sentences: data.sentences, embedding: data.embedding, }); this.sentences = data.sentences; } /** Return the SemanticChunk as a dictionary-like object */ toDict() { const base = super.toDict(); return Object.assign(Object.assign({}, base), { sentences: this.sentences.map((s) => s.toDict()) }); } /** Create a SemanticChunk object from a dictionary */ static fromDict(data) { const { sentences } = data, rest = __rest(data, ["sentences"]); const semanticSentences = sentences.map((s) => SemanticSentence.fromDict(s)); return new SemanticChunk(Object.assign(Object.assign({}, rest), { sentences: semanticSentences })); } /** Return a string representation of the SemanticChunk */ toString() { return `SemanticChunk(text=${this.text}, startIndex=${this.startIndex}, endIndex=${this.endIndex}, tokenCount=${this.tokenCount}, sentences=${JSON.stringify(this.sentences)})`; } } exports.SemanticChunk = SemanticChunk; //# sourceMappingURL=semantic.js.map