sentence-splitter
Split {Japanese, English} text into sentences.
Installation
npm install sentence-splitter
Requirements:
CLI
$ npm install -g sentence-splitter
$ echo "This is a pen. But, this is not pen" | sentence-splitter
This is a pen.
But This is not pen
Usage
export interface SeparatorParserOptions {
separatorCharacters?: string[]
}
export interface splitOptions {
SeparatorParser?: SeparatorParserOptions;
}
export declare function split(text: string, options?: splitOptions): (TxtParentNode | TxtNode)[];
export declare function splitAST(paragraphNode: TxtParentNode, options?: splitOptions): TxtParentNode;
TxtParentNode
and TxtNode
is defined
in TxtAST.
Example
import { split, Syntax } from "sentence-splitter";
let sentences = split(`There it is! I found it.
Hello World. My name is Jonas.`);
console.log(JSON.stringify(sentences, null, 4));
Node
This node is based on TxtAST.
Node's type
Str
: Str node has value
Sentence
: Sentence Node has Str
, WhiteSpace
, or Punctuation
nodes as childrenWhiteSpace
: WhiteSpace Node has \n
.Punctuation
: Punctuation Node has .
, 。
Get these Syntax
constants value from the module:
import { Syntax } from "sentence-splitter";
console.log(Syntax.Sentence);
Node's interface
export interface WhiteSpaceNode extends TxtTextNode {
readonly type: "WhiteSpace";
}
export interface PunctuationNode extends TxtTextNode {
readonly type: "Punctuation";
}
export interface StrNode extends TxtTextNode {
readonly type: "Str";
}
export interface SentenceNode extends TxtParentNode {
readonly type: "Sentence";
}
Fore more details, Please see TxtAST.
Node layout
Node layout image.
<WhiteSpace />
<Sentence>
<Str />
<Punctuation />
<Str />
<Punctuation />
</Sentence>
<WhiteSpace />
Note: This library will not split Str
into Str
and WhiteSpace
(tokenize)
Because, Tokenize need to implement language specific context.
in textlint rule
You can use splitAST
in textlint rule.
splitAST
function can preverse original AST's position unlike split
function.
import { splitAST, Syntax as SentenceSyntax } from "sentence-splitter";
export default function(context, options = {}) {
const { Syntax, RuleError, report, getSource } = context;
return {
[Syntax.Paragraph](node) {
const resultNode = splitAST(node);
const sentenceNodes = resultNode.children.filter(childNode => childNode.type === SentenceSyntax.Sentence);
console.log(sentenceNodes);
}
}
}
Example
Reference
This library use "Golden Rule" test of pragmatic_segmenter
.
Tests
Run tests:
npm test
Create input.json
from _input.md
npm run createInputJson
Update snapshots(output.json
):
npm run updateSnapshot
Adding snapshot testcase
- Create
test/fixtures/<test-case-name>/
directory - Put
test/fixtures/<test-case-name>/_input.md
with testing content - Run
npm run updateSnapshot
- Check the
test/fixtures/<test-case-name>/output.json
- If it is ok, commit it
Contributing
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
License
MIT