add trimSentences option to SentenceTokenizer, to let users choose to preserve whitespace - #760
Merged
Merged
Conversation
Pull Request Test Coverage Report for Build 15187783489Details
💛 - Coveralls |
Contributor
Author
|
Thanks very much @Hugo-ter-Doest ! |
jeremybmerrill
added a commit
to jeremybmerrill/meaningfully
that referenced
this pull request
Jun 7, 2025
Contributor
Author
|
@Hugo-ter-Doest This might be my mistake (I only pretend to know typescript!) but I think https://github.com/NaturalNode/natural/blob/master/lib/natural/tokenizers/index.d.ts#L139 needs to be updated to know about this option for the SentenceTokenizer constructor. |
Collaborator
|
You are right! Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I use
naturaldownstream of [https://github.com/run-llama/LlamaIndexTS/blob/189d8a83ac8c7d0f8ac9402f24fd1516569555dd/packages/core/src/node-parser/sentence-splitter.ts](LlamaIndex's sentence-splitter) -- which aims to split natural language text into chunks of a (roughly) arbitrary-size, preferring to split at sentence boundaries. I use that in https://github.com/jeremybmerrill/meaningfully, a desktop tool for semantic search across CSVs containing text.LlamaIndex appears to have a bug whereby text chunks end up with sentences joined without a space, e.g.
This is a sentence.This is also a sentence.. This is a problem because that violates English's orthography rules.The problem occurs because LlamaIndex naively joins sentences with an empty string -- even though
naturalhas trimmed its output sentences of any leading/trailing whitespace.This PR adds a
trimSentencesoption to SentenceTokenizer's constructor() that, when set to false, doesn't trim the whitespace from output sentences, instead leaving it intact. As a result, when LlamaIndex splits and re-joins the sentences, proper whitespace is preserved. The option defaults to true, which means that the existing behavior is retained when code downstream of natural is left unchanged.I've added a passing test.