11// LICENSE : MIT
22"use strict" ;
3- import { RuleHelper } from "textlint-rule-helper"
3+ import { RuleHelper } from "textlint-rule-helper" ;
44import { getTokenizer } from "kuromojin" ;
55import { splitAST , Syntax as SentenceSyntax } from "sentence-splitter" ;
66import { StringSource } from "textlint-util-to-string" ;
7+ import { SeparatorParser } from "sentence-splitter/lib/parser/SeparatorParser" ;
78
89const defaultOptions = {
9- max : 3 , // 1文に利用できる最大の、の数
10- strict : false // 例外ルールを適応するかどうか
10+ // 1文に利用できる最大の、の数
11+ max : 3 ,
12+ // 例外ルールを適応するかどうか,
13+ strict : false ,
14+ // 読点として扱う文字
15+ // https://ja.wikipedia.org/wiki/%E8%AA%AD%E7%82%B9
16+ touten : "、" ,
17+ // 句点として扱う文字
18+ // https://ja.wikipedia.org/wiki/%E5%8F%A5%E7%82%B9
19+ kuten : "。"
1120} ;
1221
13- function isSandwichedMeishi ( {
14- before,
15- token,
16- after
17- } ) {
22+ function isSandwichedMeishi ( { before, token, after } ) {
1823 if ( before === undefined || after === undefined || token === undefined ) {
1924 return false ;
2025 }
@@ -23,20 +28,31 @@ function isSandwichedMeishi({
2328
2429/**
2530 * @param {RuleContext } context
26- * @param {object } [options]
31+ * @param {typeof defaultOptions } [options]
2732 */
2833module . exports = function ( context , options = { } ) {
29- const maxLen = options . max || defaultOptions . max ;
30- const isStrict = options . strict || defaultOptions . strict ;
34+ const maxLen = options . max ?? defaultOptions . max ;
35+ const isStrict = options . strict ?? defaultOptions . strict ;
36+ const touten = options . touten ?? defaultOptions . touten ;
37+ const kuten = options . kuten ?? defaultOptions . kuten ;
3138 const helper = new RuleHelper ( context ) ;
3239 const { Syntax, RuleError, report, getSource } = context ;
3340 return {
3441 [ Syntax . Paragraph ] ( node ) {
3542 if ( helper . isChildNode ( node , [ Syntax . BlockQuote ] ) ) {
3643 return ;
3744 }
38- const resultNode = splitAST ( node ) ;
39- const sentences = resultNode . children . filter ( childNode => childNode . type === SentenceSyntax . Sentence ) ;
45+ const resultNode = splitAST ( node , {
46+ SeparatorParser : {
47+ separatorCharacters : [
48+ "?" , // question mark
49+ "!" , // exclamation mark
50+ "?" , // (ja) zenkaku question mark
51+ "!" // (ja) zenkaku exclamation mark
52+ ] . concat ( kuten )
53+ }
54+ } ) ;
55+ const sentences = resultNode . children . filter ( ( childNode ) => childNode . type === SentenceSyntax . Sentence ) ;
4056 /*
4157 <p>
4258 <str><code><img><str>
@@ -49,18 +65,18 @@ module.exports = function (context, options = {}) {
4965 2. sentence to tokens
5066 3. check tokens
5167 */
52- return getTokenizer ( ) . then ( tokenizer => {
53- sentences . forEach ( sentence => {
68+ return getTokenizer ( ) . then ( ( tokenizer ) => {
69+ sentences . forEach ( ( sentence ) => {
5470 const source = new StringSource ( sentence ) ;
5571 const text = source . toString ( ) ;
5672 const tokens = tokenizer . tokenizeForSentence ( text ) ;
5773 let currentTenCount = 0 ;
5874 let lastToken = null ;
5975 tokens . forEach ( ( token , index ) => {
60- let surface = token . surface_form ;
61- if ( surface === "、" ) {
76+ const surface = token . surface_form ;
77+ if ( surface === touten ) {
6278 // 名詞に囲まわれている場合は例外とする
63- let isSandwiched = isSandwichedMeishi ( {
79+ const isSandwiched = isSandwichedMeishi ( {
6480 before : tokens [ index - 1 ] ,
6581 token : token ,
6682 after : tokens [ index + 1 ]
@@ -80,15 +96,18 @@ module.exports = function (context, options = {}) {
8096 if ( currentTenCount >= maxLen ) {
8197 const positionInSentence = source . originalIndexFromIndex ( lastToken . word_position - 1 ) ;
8298 const index = sentence . range [ 0 ] + positionInSentence ;
83- const ruleError = new context . RuleError ( `一つの文で"、"を${ maxLen } つ以上使用しています` , {
84- index
85- } ) ;
99+ const ruleError = new context . RuleError (
100+ `一つの文で"${ touten } "を${ maxLen } つ以上使用しています` ,
101+ {
102+ index
103+ }
104+ ) ;
86105 report ( node , ruleError ) ;
87106 currentTenCount = 0 ;
88107 }
89108 } ) ;
90109 } ) ;
91110 } ) ;
92111 }
93- }
94- }
112+ } ;
113+ } ;
0 commit comments