Gathering detailed insights and metrics for sentence-splitter
Gathering detailed insights and metrics for sentence-splitter
Gathering detailed insights and metrics for sentence-splitter
Gathering detailed insights and metrics for sentence-splitter
@docaohuynh/sentence-splitter
This project was created using `bun init` in bun v1.2.15. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
@chax-at/simple-sentence-splitter
A sentence splitter, written for https://storywi.se, which works quite well
@project-lakechain/sentence-text-splitter
Transforms text into chunks of tokens using a sentence text splitter.
llm-text-splitter
A super simple text splitter for RAG applications
npm install sentence-splitter
Typescript
Module System
Node Version
NPM Version
TypeScript (90.08%)
CSS (9%)
HTML (0.71%)
JavaScript (0.17%)
Shell (0.04%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
129 Stars
137 Commits
18 Forks
6 Watchers
1 Branches
6 Contributors
Updated on May 29, 2025
Latest Version
5.0.0
Package Id
sentence-splitter@5.0.0
Unpacked Size
209.12 kB
Size
31.67 kB
File Count
140
NPM Version
10.2.0
Node Version
18.18.2
Published on
Nov 25, 2023
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Split {Japanese, English} text into sentences.
This library split next text into 3 sentences.
We are talking about pens.
He said "This is a pen. I like it".
I could relate to that statement.
Result is:
You can check actual AST in online playground.
Second sentence includes "This is a pen. I like it"
, but this library can not split it into new sentence.
The reason is "..."
and 「...」
text is ambiguous as a sentence or a proper noun.
Also, HTML does not have suitable semantics for conversation.
As a result, The second line will be one sentence, but sentence-splitter add a contexts
info to the sentence node.
1{ 2 "type": "Sentence", 3 "children": [ 4 { 5 "type": "Str", 6 "value": "He said \"This is a pen. I like it\"" 7 }, 8 ... 9 ], 10 "contexts": [ 11 { 12 "type": "PairMark", 13 "pairMark": { 14 "key": "double quote", 15 "start": "\"", 16 "end": "\"" 17 }, 18 "range": [ 19 8, 20 33 21 ], 22 ... 23 ] 24 ] 25}
Probably, textlint rule should handle the "..."
and 「...」
text after parsing sentences by sentence-splitter.
npm install sentence-splitter
1export interface SeparatorParserOptions { 2 /** 3 * Recognize each characters as separator 4 * Example [".", "!", "?"] 5 */ 6 separatorCharacters?: string[] 7} 8 9export interface AbbrMarkerOptions { 10 language?: Language; 11} 12 13export interface splitOptions { 14 /** 15 * Separator & AbbrMarker options 16 */ 17 SeparatorParser?: SeparatorParserOptions; 18 AbbrMarker?: AbbrMarkerOptions; 19} 20 21/** 22 * split `text` into Sentence nodes. 23 * This function return array of Sentence nodes. 24 */ 25export declare function split(text: string, options?: splitOptions): TxtParentNodeWithSentenceNode["children"]; 26 27/** 28 * Convert Paragraph Node to Paragraph Node that includes Sentence Node. 29 * Paragraph Node is defined in textlint's TxtAST. 30 * See https://github.com/textlint/textlint/blob/master/docs/txtnode.md 31 */ 32export declare function splitAST(paragraphNode: TxtParentNode, options?: splitOptions): TxtParentNodeWithSentenceNode;
See also TxtAST.
This node is based on TxtAST.
Str
: Str node has value
. It is same as TxtAST's Str
node.Sentence
: Sentence Node has Str
, WhiteSpace
, or Punctuation
nodes as childrenWhiteSpace
: WhiteSpace Node has \n
.Punctuation
: Punctuation Node has .
, 。
Get these SentenceSplitterSyntax
constants value from the module:
1import { SentenceSplitterSyntax } from "sentence-splitter"; 2 3console.log(SentenceSplitterSyntax.Sentence);// "Sentence"
1export type SentencePairMarkContext = { 2 type: "PairMark"; 3 range: readonly [startIndex: number, endIndex: number]; 4 loc: { 5 start: { 6 line: number; 7 column: number; 8 }; 9 end: { 10 line: number; 11 column: number; 12 }; 13 }; 14}; 15export type TxtSentenceNode = Omit<TxtParentNode, "type"> & { 16 readonly type: "Sentence"; 17 readonly contexts?: TxtPairMarkNode[]; 18}; 19export type TxtWhiteSpaceNode = Omit<TxtTextNode, "type"> & { 20 readonly type: "WhiteSpace"; 21}; 22export type TxtPunctuationNode = Omit<TxtTextNode, "type"> & { 23 readonly type: "Punctuation"; 24};
Fore more details, Please see TxtAST.
Node layout image.
This is 1st sentence. This is 2nd sentence.
<Sentence>
<Str /> |This is 1st sentence|
<Punctuation /> |.|
</Sentence>
<WhiteSpace /> | |
<Sentence>
<Str /> |This is 2nd sentence|
<Punctuation /> |.|
</Sentence>
Note: This library will not split Str
into Str
and WhiteSpace
(tokenize)
Because, Tokenize need to implement language specific context.
You can use splitAST
for textlint rule.
splitAST
function can preserve original AST's position unlike split
function.
1import { splitAST, SentenceSplitterSyntax } from "sentence-splitter"; 2 3export default function(context, options = {}) { 4 const { Syntax, RuleError, report, getSource } = context; 5 return { 6 [Syntax.Paragraph](node) { 7 const parsedNode = splitAST(node); 8 const sentenceNodes = parsedNode.children.filter(childNode => childNode.type === SentenceSplitterSyntax.Sentence); 9 console.log(sentenceNodes); // => Sentence nodes 10 } 11 } 12}
Examples
This library use "Golden Rule" test of pragmatic_segmenter
for testing.
Run tests:
npm test
Create input.json
from _input.md
npm run createInputJson
Update snapshots(output.json
):
npm run updateSnapshot
test/fixtures/<test-case-name>/
directorytest/fixtures/<test-case-name>/_input.md
with testing contentnpm run updateSnapshot
test/fixtures/<test-case-name>/output.json
git checkout -b my-new-feature
git commit -am 'Add some feature'
git push origin my-new-feature
MIT
No vulnerabilities found.
No security vulnerabilities found.