Skip to content

Commit ef6097c

Browse files
authored
feat(options): separatorCharsオプションの追加 (#27)
* feat(options): separatorCharsオプションの追加 * fix typo * add docs * test: add test case * chore: rename separatorCharacters - 1からアップデートしたときに意味が変わってしまうため * fix
1 parent 0c9d5ec commit ef6097c

File tree

5 files changed

+127
-57
lines changed

5 files changed

+127
-57
lines changed

README.md

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,14 @@
1313

1414
npm install textlint-rule-no-doubled-joshi
1515

16-
Require: textlint 5.0 >=
17-
18-
Dependencies
19-
20-
- [azu/kuromojin](https://github.com/azu/kuromojin) a wrapper of [kuromoji.js](https://github.com/takuyaa/kuromoji.js "kuromoji.js")
21-
- [azu/sentence-splitter](https://github.com/azu/sentence-splitter)
22-
2316
## Usage
2417

2518
Via `.textlintrc`(推奨)
2619

27-
```js
20+
```json5
2821
{
2922
"rules": {
30-
"no-doubled-joshi": {
31-
"min_interval" : 1,
32-
"strict": false,
33-
"allow": []
34-
}
23+
"no-doubled-joshi": true
3524
}
3625
}
3726
```
@@ -47,27 +36,43 @@ textlint --rule no-doubled-joshi README.md
4736

4837
`.textlintrc` options.
4938

50-
```js
39+
```json5
5140
{
5241
"rules": {
5342
"no-doubled-joshi": {
54-
// 助詞のtoken同士の距離が2以下ならエラー
43+
// 助詞のtoken同士の距離が2以下ならエラーにする
5544
"min_interval" : 2,
5645
// 例外を許可するかどうか
5746
"strict": false,
5847
// 助詞のうち「も」「や」は複数回の出現を許す
59-
"allow": ["",""]
48+
"allow": ["",""],
49+
// 文の区切り文字となる配列
50+
"separatorCharacters": [
51+
".", // period
52+
"", // (ja) 全角period
53+
"", // (ja) 句点
54+
"?", // question mark
55+
"!", // exclamation mark
56+
"", // (ja) 全角 question mark
57+
"" // (ja) 全角 exclamation mark
58+
]
6059
}
6160
}
6261
}
6362
```
6463

65-
- `min_interval`(default: 1) : 助詞の最低間隔値
66-
- 指定した間隔値以下で同じ助詞が出現した場合エラーが出力されます
67-
- `strict`(default: false) :例外もエラーとするかどうか
64+
- `min_interval`(default: `1`) : 助詞の最低間隔値
65+
- 指定した間隔値以下で同じ助詞が出現した場合エラーが出力されます
66+
- `strict`(default: `false`) : 例外もエラーとするかどうか
6867
- 下記参照。例外としているものもエラーとするかどうか
69-
- `allow`(default: []) :複数回の出現を許す助詞
70-
- 並立の助詞など、複数回出現しても無視する助詞を指定します。
68+
- `allow`(default: `[]`) :複数回の出現を許す助詞
69+
- 並立の助詞など、複数回出現しても無視する助詞を指定します
70+
- 例) `"も"`を許可したい場合は `{ "allow": ["も"] }`
71+
- `separatorCharacters`(default: `[".", ".", "。", "?", "!", "?", "!"]`) : 文の区切り文字として使用する文字の配列
72+
- `separatorCharacters`を設定するとデフォルト値は上書きされます
73+
- ``のみを文の区切り文字にしたい場合は。`{ "separatorCharacters" : ["。"] }`のように指定します
74+
75+
**min_interval**について
7176

7277
> ********好き
7378
@@ -140,7 +145,7 @@ NG: 文字列**には**そこ**には***問題がある。
140145
141146
オプションで`"allow": ["も"]`を指定することで、****を例として扱うことができる。
142147

143-
```js
148+
```json5
144149
{
145150
"rules": {
146151
"no-doubled-joshi": {
@@ -171,6 +176,11 @@ NG: 文字列**には**そこ**には***問題がある。
171176
- [作文入門](http://www.slideshare.net/takahi-i/ss-13429892 "作文入門")
172177
- [形態素解析ツールの品詞体系](http://www.unixuser.org/~euske/doc/postag/index.html#chasen "形態素解析ツールの品詞体系")
173178

179+
## Related Libraries
180+
181+
- [azu/kuromojin](https://github.com/azu/kuromojin) a wrapper of [kuromoji.js](https://github.com/takuyaa/kuromoji.js "kuromoji.js")
182+
- [azu/sentence-splitter](https://github.com/azu/sentence-splitter)
183+
174184
## Contributing
175185

176186
1. Fork it!

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@
3333
"textlintrule"
3434
],
3535
"devDependencies": {
36-
"@textlint/types": "^1.2.2",
37-
"@types/node": "^12.11.1",
36+
"@textlint/types": "^1.2.3",
37+
"@types/node": "^12.12.14",
3838
"textlint-scripts": "^3.0.0",
39-
"ts-node": "^8.4.1",
40-
"typescript": "^3.6.4"
39+
"ts-node": "^8.5.4",
40+
"typescript": "^3.7.3"
4141
},
4242
"dependencies": {
4343
"kuromojin": "^2.0.0",
44-
"sentence-splitter": "^3.1.0",
44+
"sentence-splitter": "^3.2.0",
4545
"textlint-rule-helper": "^2.1.1",
4646
"textlint-util-to-string": "^3.0.0"
4747
}

src/no-doubled-joshi.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,15 @@ const defaultOptions = {
5858
min_interval: 1,
5959
strict: false,
6060
allow: [],
61-
separatorChars: ["。", "?", "!", "?", "!"]
61+
separatorCharacters: [
62+
".", // period
63+
".", // (ja) zenkaku-period
64+
"。", // (ja) 句点
65+
"?", // question mark
66+
"!", // exclamation mark
67+
"?", // (ja) zenkaku question mark
68+
"!" // (ja) zenkaku exclamation mark
69+
]
6270
};
6371

6472

@@ -83,6 +91,7 @@ const report: TextlintRuleModule<Options> = function (context, options = {}) {
8391
const minInterval = options.min_interval || defaultOptions.min_interval;
8492
const isStrict = options.strict || defaultOptions.strict;
8593
const allow = options.allow || defaultOptions.allow;
94+
const separatorCharacters = options.separatorCharacters || defaultOptions.separatorCharacters;
8695
const {Syntax, report, RuleError} = context;
8796
return {
8897
[Syntax.Paragraph](node) {
@@ -92,7 +101,11 @@ const report: TextlintRuleModule<Options> = function (context, options = {}) {
92101
const isSentenceNode = (node: TxtNode): node is SentenceNode => {
93102
return node.type === SentenceSyntax.Sentence;
94103
};
95-
const txtParentNode = splitSentences(node);
104+
const txtParentNode = splitSentences(node, {
105+
SeparatorParser: {
106+
separatorCharacters
107+
}
108+
});
96109
const sentences = txtParentNode.children.filter(isSentenceNode);
97110
return getTokenizer().then((tokenizer: any) => {
98111
const checkSentence = (sentence: SentenceNode) => {

test/no-doubled-joshi-test.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ tester.run("no-double-joshi", rule, {
2525
text: "太字も強調も同じように無視されます。",
2626
options: {allow: ["も"]}
2727
},
28-
// 全角ピリオドを文区切り文字に含める
28+
// 区切り文字をカスタムする
29+
// ♪を区切り文字としたので、次の文は2つのセンテンスになる
2930
{
30-
text: "画面に表示されます.それ以外には表示されません.",
31-
options: {separatorChars: ["。",".","?","!","?","!"]}
31+
text: "これはペンです♪これは鉛筆です♪",
32+
options: {separatorCharacters: ["♪"]},
3233
}
34+
3335
],
3436
invalid: [
3537
// エラー位置は最後の助詞の位置を表示する
@@ -150,7 +152,7 @@ tester.run("no-double-joshi", rule, {
150152
column: 6
151153
}
152154
]
153-
},{
155+
}, {
154156
text: `この行にはtextlintによる警告は出ない。
155157
この行にはtextlintにより警告が発せられる。この行に何かしようとすると起きるという
156158
この行にはtextlintによる警告は出ない。
@@ -190,6 +192,18 @@ tester.run("no-double-joshi", rule, {
190192
index: 21
191193
}
192194
]
195+
},
196+
// オプションで、全角ピリオドを読点として認識させなくする
197+
// 次のtextは1つのセンテンスとして認識されるので、"は"が重複する
198+
{
199+
text: "これはペンです.これは鉛筆です.",
200+
options: {separatorCharacters: ["。"]},
201+
errors: [
202+
{
203+
message: `一文に二回以上利用されている助詞 "は" がみつかりました。`,
204+
index: 10
205+
}
206+
]
193207
}
194208
]
195209
});

yarn.lock

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@
713713
lodash "^4.17.13"
714714
to-fast-properties "^2.0.0"
715715

716-
"@textlint/ast-node-types@^4.0.2", "@textlint/ast-node-types@^4.2.1", "@textlint/ast-node-types@^4.2.4", "@textlint/ast-node-types@^4.2.5":
716+
"@textlint/ast-node-types@^4.2.1", "@textlint/ast-node-types@^4.2.4", "@textlint/ast-node-types@^4.2.5":
717717
version "4.2.5"
718718
resolved "https://registry.yarnpkg.com/@textlint/ast-node-types/-/ast-node-types-4.2.5.tgz#ae13981bc8711c98313a6ac1c361194d6bf2d39b"
719719
integrity sha512-+rEx4jLOeZpUcdvll7jEg/7hNbwYvHWFy4IGW/tk2JdbyB3SJVyIP6arAwzTH/sp/pO9jftfyZnRj4//sLbLvQ==
@@ -832,6 +832,13 @@
832832
dependencies:
833833
"@textlint/ast-node-types" "^4.2.5"
834834

835+
"@textlint/types@^1.2.3":
836+
version "1.2.3"
837+
resolved "https://registry.yarnpkg.com/@textlint/types/-/types-1.2.3.tgz#6a27d6a2035873904d45c775b6205f162f169f91"
838+
integrity sha512-48wXioiKIKAWPkgTWf/KyvZpjGH5L2ohmrSVgtx/u8q+KcY+q+/dslKy7QN0H+Yeu5Vj4GZRHOzCYWtzHMQleQ==
839+
dependencies:
840+
"@textlint/ast-node-types" "^4.2.5"
841+
835842
"@textlint/utils@^1.0.3":
836843
version "1.0.3"
837844
resolved "https://registry.yarnpkg.com/@textlint/utils/-/utils-1.0.3.tgz#65196f21a5c1884b2dc6088484063f9ad52dfb67"
@@ -842,10 +849,10 @@
842849
resolved "https://registry.yarnpkg.com/@types/bluebird/-/bluebird-3.5.28.tgz#04c1a520ff076649236bc8ca21198542ce2bdb09"
843850
integrity sha512-0Vk/kqkukxPKSzP9c8WJgisgGDx5oZDbsLLWIP5t70yThO/YleE+GEm2S1GlRALTaack3O7U5OS5qEm7q2kciA==
844851

845-
"@types/node@^12.11.1":
846-
version "12.11.1"
847-
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.11.1.tgz#1fd7b821f798b7fa29f667a1be8f3442bb8922a3"
848-
integrity sha512-TJtwsqZ39pqcljJpajeoofYRfeZ7/I/OMUQ5pR4q5wOKf2ocrUvBAZUMhWsOvKx3dVc/aaV5GluBivt0sWqA5A==
852+
"@types/node@^12.12.14":
853+
version "12.12.14"
854+
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.12.14.tgz#1c1d6e3c75dba466e0326948d56e8bd72a1903d2"
855+
integrity sha512-u/SJDyXwuihpwjXy7hOOghagLEV1KdAST6syfnOk6QZAMzZuWZqXy5aYYZbh8Jdpd4escVFP0MvftHNDb9pruA==
849856

850857
"@types/structured-source@^3.0.0":
851858
version "3.0.0"
@@ -1279,7 +1286,7 @@ [email protected]:
12791286
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
12801287
integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=
12811288

1282-
concat-stream@^1.5.1, concat-stream@^1.5.2:
1289+
concat-stream@^1.5.1:
12831290
version "1.6.2"
12841291
resolved "https://registry.yarnpkg.com/concat-stream/-/concat-stream-1.6.2.tgz#904bdf194cd3122fc675c77fc4ac3d4ff0fd1a34"
12851292
integrity sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==
@@ -1289,6 +1296,16 @@ concat-stream@^1.5.1, concat-stream@^1.5.2:
12891296
readable-stream "^2.2.2"
12901297
typedarray "^0.0.6"
12911298

1299+
concat-stream@^2.0.0:
1300+
version "2.0.0"
1301+
resolved "https://registry.yarnpkg.com/concat-stream/-/concat-stream-2.0.0.tgz#414cf5af790a48c60ab9be4527d56d5e41133cb1"
1302+
integrity sha512-MWufYdFw53ccGjCA+Ol7XJYpAlW6/prSMzuPOTRnJGcGzuhLn4Scrz7qf6o8bROZ514ltazcIFJZevcfbo0x7A==
1303+
dependencies:
1304+
buffer-from "^1.0.0"
1305+
inherits "^2.0.3"
1306+
readable-stream "^3.0.2"
1307+
typedarray "^0.0.6"
1308+
12921309
confirmer@^1.1.2:
12931310
version "1.1.2"
12941311
resolved "https://registry.yarnpkg.com/confirmer/-/confirmer-1.1.2.tgz#df36b3eb5ca5992750de8eea9db24781bb4cc254"
@@ -2695,7 +2712,7 @@ object.pick@^1.3.0:
26952712
dependencies:
26962713
isobject "^3.0.1"
26972714

2698-
object.values@^1.0.4:
2715+
object.values@^1.1.0:
26992716
version "1.1.0"
27002717
resolved "https://registry.yarnpkg.com/object.values/-/object.values-1.1.0.tgz#bf6810ef5da3e5325790eaaa2be213ea84624da9"
27012718
integrity sha512-8mf0nKLAoFX6VlNVdhGj31SVYpaNFtUnuoOXWyFEstsWRgU837AK+JYM0iAxwkSzGRbwn8cbFmgbyxj1j4VbXg==
@@ -3034,6 +3051,15 @@ readable-stream@^2.0.2, readable-stream@^2.0.6, readable-stream@^2.2.2:
30343051
string_decoder "~1.1.1"
30353052
util-deprecate "~1.0.1"
30363053

3054+
readable-stream@^3.0.2:
3055+
version "3.4.0"
3056+
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-3.4.0.tgz#a51c26754658e0a3c21dbf59163bd45ba6f447fc"
3057+
integrity sha512-jItXPLmrSR8jmTRmRWJXCnGJsfy85mB3Wd/uINMXA65yrnFo0cPClFIUWzo2najVNSl+mx7/4W8ttlLWJe99pQ==
3058+
dependencies:
3059+
inherits "^2.0.3"
3060+
string_decoder "^1.1.1"
3061+
util-deprecate "^1.0.1"
3062+
30373063
readdirp@^2.2.1:
30383064
version "2.2.1"
30393065
resolved "https://registry.yarnpkg.com/readdirp/-/readdirp-2.2.1.tgz#0e87622a3325aa33e892285caf8b4e846529a525"
@@ -3225,7 +3251,7 @@ rimraf@^2.6.1:
32253251
dependencies:
32263252
glob "^7.1.3"
32273253

3228-
safe-buffer@^5.1.2:
3254+
safe-buffer@^5.1.2, safe-buffer@~5.2.0:
32293255
version "5.2.0"
32303256
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.0.tgz#b74daec49b1148f88c64b68d49b1e815c1f2f519"
32313257
integrity sha512-fZEwUGbVl7kouZs1jCdMLdt95hdIv0ZeHg6L7qPeciMZhZ+/gdesW4wgTARkrFWEpspjEATAzUGPG8N2jJiwbg==
@@ -3262,14 +3288,14 @@ semver@^6.3.0:
32623288
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
32633289
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==
32643290

3265-
sentence-splitter@^3.1.0:
3266-
version "3.1.0"
3267-
resolved "https://registry.yarnpkg.com/sentence-splitter/-/sentence-splitter-3.1.0.tgz#85f5a6c1912a9ddb40e48186ee960d0b834a250c"
3268-
integrity sha512-S8ye/Ic5ryKynwgjcS0ggncqhVJW3mgmo/gFNIcKKU1cdLjYzqtQ/PCkD7l0rZtSSfcjLqqVjfdMVN6FKkwUuQ==
3291+
sentence-splitter@^3.2.0:
3292+
version "3.2.0"
3293+
resolved "https://registry.yarnpkg.com/sentence-splitter/-/sentence-splitter-3.2.0.tgz#fb2cd2f61f40006643ba83d9acf4609233c1c68c"
3294+
integrity sha512-lKX2tZ1rsA9Tu0gW8vRmMDmIEJoZ1d7cKpzcbFZdUrSpCR6gy/7OPPh7jjT/6Oc6Z79ToUmC2l8tyTEGanVmiA==
32693295
dependencies:
3270-
"@textlint/ast-node-types" "^4.0.2"
3271-
concat-stream "^1.5.2"
3272-
object.values "^1.0.4"
3296+
"@textlint/ast-node-types" "^4.2.5"
3297+
concat-stream "^2.0.0"
3298+
object.values "^1.1.0"
32733299
structured-source "^3.0.2"
32743300

32753301
set-blocking@^2.0.0, set-blocking@~2.0.0:
@@ -3485,6 +3511,13 @@ string.prototype.trimright@^2.1.0:
34853511
define-properties "^1.1.3"
34863512
function-bind "^1.1.1"
34873513

3514+
string_decoder@^1.1.1:
3515+
version "1.3.0"
3516+
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.3.0.tgz#42f114594a46cf1a8e30b0a84f56c78c3edac21e"
3517+
integrity sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==
3518+
dependencies:
3519+
safe-buffer "~5.2.0"
3520+
34883521
string_decoder@~1.1.1:
34893522
version "1.1.1"
34903523
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.1.1.tgz#9cf1611ba62685d7030ae9e4ba34149c3af03fc8"
@@ -3737,10 +3770,10 @@ try-resolve@^1.0.1:
37373770
resolved "https://registry.yarnpkg.com/try-resolve/-/try-resolve-1.0.1.tgz#cfde6fabd72d63e5797cfaab873abbe8e700e912"
37383771
integrity sha1-z95vq9ctY+V5fPqrhzq76OcA6RI=
37393772

3740-
ts-node@^8.4.1:
3741-
version "8.4.1"
3742-
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-8.4.1.tgz#270b0dba16e8723c9fa4f9b4775d3810fd994b4f"
3743-
integrity sha512-5LpRN+mTiCs7lI5EtbXmF/HfMeCjzt7DH9CZwtkr6SywStrNQC723wG+aOWFiLNn7zT3kD/RnFqi3ZUfr4l5Qw==
3773+
ts-node@^8.5.4:
3774+
version "8.5.4"
3775+
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-8.5.4.tgz#a152add11fa19c221d0b48962c210cf467262ab2"
3776+
integrity sha512-izbVCRV68EasEPQ8MSIGBNK9dc/4sYJJKYA+IarMQct1RtEot6Xp0bXuClsbUSnKpg50ho+aOAx8en5c+y4OFw==
37443777
dependencies:
37453778
arg "^4.1.0"
37463779
diff "^4.0.1"
@@ -3760,10 +3793,10 @@ typedarray@^0.0.6:
37603793
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
37613794
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
37623795

3763-
typescript@^3.6.4:
3764-
version "3.6.4"
3765-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.6.4.tgz#b18752bb3792bc1a0281335f7f6ebf1bbfc5b91d"
3766-
integrity sha512-unoCll1+l+YK4i4F8f22TaNVPRHcD9PA3yCuZ8g5e0qGqlVlJ/8FSateOLLSagn+Yg5+ZwuPkL8LFUc0Jcvksg==
3796+
typescript@^3.7.3:
3797+
version "3.7.3"
3798+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.3.tgz#b36840668a16458a7025b9eabfad11b66ab85c69"
3799+
integrity sha512-Mcr/Qk7hXqFBXMN7p7Lusj1ktCBydylfQM/FZCk5glCNQJrCUKPkMHdo9R0MTFWsC/4kPFvDS0fDPvukfCkFsw==
37673800

37683801
unherit@^1.0.4:
37693802
version "1.1.2"
@@ -3895,7 +3928,7 @@ use@^3.1.0:
38953928
resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f"
38963929
integrity sha512-cwESVXlO3url9YWlFW/TA9cshCEhtu7IKJ/p5soJ/gGpj7vbvFrAY/eIioQ6Dw23KjZhYgiIo8HOs1nQ2vr/oQ==
38973930

3898-
util-deprecate@~1.0.1:
3931+
util-deprecate@^1.0.1, util-deprecate@~1.0.1:
38993932
version "1.0.2"
39003933
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
39013934
integrity sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=

0 commit comments

Comments
 (0)