Skip to content

sentence splitting implementation #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
59 changes: 52 additions & 7 deletions Embeddings.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,16 @@ protected function splitIntoChunks($text)
while ($sentence = array_shift($sentences)) {
$slen = count($tiktok->encode($sentence));
if ($slen > $this->getChunkSize()) {
// sentence is too long, we need to split it further
if ($this->logger instanceof CLI) $this->logger->warning(
'Sentence too long, splitting not implemented yet'
);
continue;
// Sentence is too long, split into smaller parts
if ($this->logger instanceof CLI) {
$this->logger->warning(
'Sentence too long, splitting it into smaller parts'
);
}

// Push split sentences to the front of the queue
array_unshift($sentences, ...$this->splitLongSentence($sentence, $tiktok));
continue; // Restart loop with the newly inserted sentences
}

if ($chunklen + $slen < $this->getChunkSize()) {
Expand All @@ -360,11 +365,51 @@ protected function splitIntoChunks($text)
$chunklen = count($tiktok->encode($chunk));
}
}
$chunks[] = $chunk;


// Add the last chunk if not empty
if (trim($chunk) !== '') $chunks[] = trim($chunk);

return $chunks;
}

protected function splitLongSentence($sentence, $tiktok)
{
$words = preg_split('/(\s+)/', $sentence, -1, PREG_SPLIT_DELIM_CAPTURE);
$subSentences = [];
$currentSubSentence = '';
$currentSubSentenceLen = 0;
$chunkSize = $this->getChunkSize();

foreach ($words as $word) {
$wordLen = count($tiktok->encode($word));

if ($wordLen > $chunkSize) {
// If a single word is too long, split it into smaller chunks
$wordChunks = str_split($word, intval($chunkSize / 2)); // Split into smaller parts
foreach ($wordChunks as $chunk) {
$subSentences[] = trim($chunk);
}
} elseif ($currentSubSentenceLen + $wordLen < $chunkSize) {
// Add to current sub-sentence
$currentSubSentence .= $word;
$currentSubSentenceLen += $wordLen;
} else {
// Add current sub-sentence to result
$subSentences[] = trim($currentSubSentence);
// Start new sub-sentence
$currentSubSentence = $word;
$currentSubSentenceLen = $wordLen;
}
}

// Add last sub-sentence to result
if ($currentSubSentence !== '') {
$subSentences[] = trim($currentSubSentence);
}

return $subSentences;
}

/**
* Add a sentence to the queue of remembered sentences
*
Expand Down
Binary file added lang/.DS_Store
Binary file not shown.