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
@chax-at/simple-sentence-splitter
A sentence splitter, written for https://storywi.se, which works quite well
graphemer
A JavaScript library that breaks strings into their individual user-perceived characters (including emojis!)
textlint-rule-sentence-length
textlint rule check sentence length
@zag-js/splitter
Core logic for the splitter widget implemented as a state machine
Split {Japanese, English} text into sentences.
npm install sentence-splitter
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
119 Stars
137 Commits
14 Forks
7 Watching
1 Branches
6 Contributors
Updated on 28 Nov 2024
Minified
Minified + Gzipped
TypeScript (90.08%)
CSS (9%)
HTML (0.71%)
JavaScript (0.17%)
Shell (0.04%)
Cumulative downloads
Total Downloads
Last day
18.9%
14,101
Compared to previous day
Last week
4.1%
66,269
Compared to previous week
Last month
15.9%
277,011
Compared to previous month
Last year
23.2%
2,636,855
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.