Skip to content

Commit

Permalink
feat(options): separatorCharsオプションの追加 (#27)
Browse files Browse the repository at this point in the history
* feat(options): separatorCharsオプションの追加

* fix typo

* add docs

* test: add test case

* chore: rename separatorCharacters

- 1からアップデートしたときに意味が変わってしまうため

* fix
  • Loading branch information
azu authored Dec 7, 2019
1 parent 0c9d5ec commit ef6097c
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 57 deletions.
54 changes: 32 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,14 @@

npm install textlint-rule-no-doubled-joshi

Require: textlint 5.0 >=

Dependencies

- [azu/kuromojin](https://github.com/azu/kuromojin) a wrapper of [kuromoji.js](https://github.com/takuyaa/kuromoji.js "kuromoji.js")
- [azu/sentence-splitter](https://github.com/azu/sentence-splitter)

## Usage

Via `.textlintrc`(推奨)

```js
```json5
{
"rules": {
"no-doubled-joshi": {
"min_interval" : 1,
"strict": false,
"allow": []
}
"no-doubled-joshi": true
}
}
```
Expand All @@ -47,27 +36,43 @@ textlint --rule no-doubled-joshi README.md

`.textlintrc` options.

```js
```json5
{
"rules": {
"no-doubled-joshi": {
// 助詞のtoken同士の距離が2以下ならエラー
// 助詞のtoken同士の距離が2以下ならエラーにする
"min_interval" : 2,
// 例外を許可するかどうか
"strict": false,
// 助詞のうち「も」「や」は複数回の出現を許す
"allow": ["",""]
"allow": ["",""],
// 文の区切り文字となる配列
"separatorCharacters": [
".", // period
"", // (ja) 全角period
"", // (ja) 句点
"?", // question mark
"!", // exclamation mark
"", // (ja) 全角 question mark
"" // (ja) 全角 exclamation mark
]
}
}
}
```

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

**min_interval**について

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

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

## Related Libraries

- [azu/kuromojin](https://github.com/azu/kuromojin) a wrapper of [kuromoji.js](https://github.com/takuyaa/kuromoji.js "kuromoji.js")
- [azu/sentence-splitter](https://github.com/azu/sentence-splitter)

## Contributing

1. Fork it!
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
"textlintrule"
],
"devDependencies": {
"@textlint/types": "^1.2.2",
"@types/node": "^12.11.1",
"@textlint/types": "^1.2.3",
"@types/node": "^12.12.14",
"textlint-scripts": "^3.0.0",
"ts-node": "^8.4.1",
"typescript": "^3.6.4"
"ts-node": "^8.5.4",
"typescript": "^3.7.3"
},
"dependencies": {
"kuromojin": "^2.0.0",
"sentence-splitter": "^3.1.0",
"sentence-splitter": "^3.2.0",
"textlint-rule-helper": "^2.1.1",
"textlint-util-to-string": "^3.0.0"
}
Expand Down
17 changes: 15 additions & 2 deletions src/no-doubled-joshi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ const defaultOptions = {
min_interval: 1,
strict: false,
allow: [],
separatorChars: ["。", "?", "!", "?", "!"]
separatorCharacters: [
".", // period
".", // (ja) zenkaku-period
"。", // (ja) 句点
"?", // question mark
"!", // exclamation mark
"?", // (ja) zenkaku question mark
"!" // (ja) zenkaku exclamation mark
]
};


Expand All @@ -83,6 +91,7 @@ const report: TextlintRuleModule<Options> = function (context, options = {}) {
const minInterval = options.min_interval || defaultOptions.min_interval;
const isStrict = options.strict || defaultOptions.strict;
const allow = options.allow || defaultOptions.allow;
const separatorCharacters = options.separatorCharacters || defaultOptions.separatorCharacters;
const {Syntax, report, RuleError} = context;
return {
[Syntax.Paragraph](node) {
Expand All @@ -92,7 +101,11 @@ const report: TextlintRuleModule<Options> = function (context, options = {}) {
const isSentenceNode = (node: TxtNode): node is SentenceNode => {
return node.type === SentenceSyntax.Sentence;
};
const txtParentNode = splitSentences(node);
const txtParentNode = splitSentences(node, {
SeparatorParser: {
separatorCharacters
}
});
const sentences = txtParentNode.children.filter(isSentenceNode);
return getTokenizer().then((tokenizer: any) => {
const checkSentence = (sentence: SentenceNode) => {
Expand Down
22 changes: 18 additions & 4 deletions test/no-doubled-joshi-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ tester.run("no-double-joshi", rule, {
text: "太字も強調も同じように無視されます。",
options: {allow: ["も"]}
},
// 全角ピリオドを文区切り文字に含める
// 区切り文字をカスタムする
// ♪を区切り文字としたので、次の文は2つのセンテンスになる
{
text: "画面に表示されます.それ以外には表示されません.",
options: {separatorChars: ["。",".","?","!","?","!"]}
text: "これはペンです♪これは鉛筆です♪",
options: {separatorCharacters: ["♪"]},
}

],
invalid: [
// エラー位置は最後の助詞の位置を表示する
Expand Down Expand Up @@ -150,7 +152,7 @@ tester.run("no-double-joshi", rule, {
column: 6
}
]
},{
}, {
text: `この行にはtextlintによる警告は出ない。
この行にはtextlintにより警告が発せられる。この行に何かしようとすると起きるという
この行にはtextlintによる警告は出ない。
Expand Down Expand Up @@ -190,6 +192,18 @@ tester.run("no-double-joshi", rule, {
index: 21
}
]
},
// オプションで、全角ピリオドを読点として認識させなくする
// 次のtextは1つのセンテンスとして認識されるので、"は"が重複する
{
text: "これはペンです.これは鉛筆です.",
options: {separatorCharacters: ["。"]},
errors: [
{
message: `一文に二回以上利用されている助詞 "は" がみつかりました。`,
index: 10
}
]
}
]
});
81 changes: 57 additions & 24 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@
lodash "^4.17.13"
to-fast-properties "^2.0.0"

"@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":
"@textlint/ast-node-types@^4.2.1", "@textlint/ast-node-types@^4.2.4", "@textlint/ast-node-types@^4.2.5":
version "4.2.5"
resolved "https://registry.yarnpkg.com/@textlint/ast-node-types/-/ast-node-types-4.2.5.tgz#ae13981bc8711c98313a6ac1c361194d6bf2d39b"
integrity sha512-+rEx4jLOeZpUcdvll7jEg/7hNbwYvHWFy4IGW/tk2JdbyB3SJVyIP6arAwzTH/sp/pO9jftfyZnRj4//sLbLvQ==
Expand Down Expand Up @@ -832,6 +832,13 @@
dependencies:
"@textlint/ast-node-types" "^4.2.5"

"@textlint/types@^1.2.3":
version "1.2.3"
resolved "https://registry.yarnpkg.com/@textlint/types/-/types-1.2.3.tgz#6a27d6a2035873904d45c775b6205f162f169f91"
integrity sha512-48wXioiKIKAWPkgTWf/KyvZpjGH5L2ohmrSVgtx/u8q+KcY+q+/dslKy7QN0H+Yeu5Vj4GZRHOzCYWtzHMQleQ==
dependencies:
"@textlint/ast-node-types" "^4.2.5"

"@textlint/utils@^1.0.3":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@textlint/utils/-/utils-1.0.3.tgz#65196f21a5c1884b2dc6088484063f9ad52dfb67"
Expand All @@ -842,10 +849,10 @@
resolved "https://registry.yarnpkg.com/@types/bluebird/-/bluebird-3.5.28.tgz#04c1a520ff076649236bc8ca21198542ce2bdb09"
integrity sha512-0Vk/kqkukxPKSzP9c8WJgisgGDx5oZDbsLLWIP5t70yThO/YleE+GEm2S1GlRALTaack3O7U5OS5qEm7q2kciA==

"@types/node@^12.11.1":
version "12.11.1"
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.11.1.tgz#1fd7b821f798b7fa29f667a1be8f3442bb8922a3"
integrity sha512-TJtwsqZ39pqcljJpajeoofYRfeZ7/I/OMUQ5pR4q5wOKf2ocrUvBAZUMhWsOvKx3dVc/aaV5GluBivt0sWqA5A==
"@types/node@^12.12.14":
version "12.12.14"
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.12.14.tgz#1c1d6e3c75dba466e0326948d56e8bd72a1903d2"
integrity sha512-u/SJDyXwuihpwjXy7hOOghagLEV1KdAST6syfnOk6QZAMzZuWZqXy5aYYZbh8Jdpd4escVFP0MvftHNDb9pruA==

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

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

concat-stream@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/concat-stream/-/concat-stream-2.0.0.tgz#414cf5af790a48c60ab9be4527d56d5e41133cb1"
integrity sha512-MWufYdFw53ccGjCA+Ol7XJYpAlW6/prSMzuPOTRnJGcGzuhLn4Scrz7qf6o8bROZ514ltazcIFJZevcfbo0x7A==
dependencies:
buffer-from "^1.0.0"
inherits "^2.0.3"
readable-stream "^3.0.2"
typedarray "^0.0.6"

confirmer@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/confirmer/-/confirmer-1.1.2.tgz#df36b3eb5ca5992750de8eea9db24781bb4cc254"
Expand Down Expand Up @@ -2695,7 +2712,7 @@ object.pick@^1.3.0:
dependencies:
isobject "^3.0.1"

object.values@^1.0.4:
object.values@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/object.values/-/object.values-1.1.0.tgz#bf6810ef5da3e5325790eaaa2be213ea84624da9"
integrity sha512-8mf0nKLAoFX6VlNVdhGj31SVYpaNFtUnuoOXWyFEstsWRgU837AK+JYM0iAxwkSzGRbwn8cbFmgbyxj1j4VbXg==
Expand Down Expand Up @@ -3034,6 +3051,15 @@ readable-stream@^2.0.2, readable-stream@^2.0.6, readable-stream@^2.2.2:
string_decoder "~1.1.1"
util-deprecate "~1.0.1"

readable-stream@^3.0.2:
version "3.4.0"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-3.4.0.tgz#a51c26754658e0a3c21dbf59163bd45ba6f447fc"
integrity sha512-jItXPLmrSR8jmTRmRWJXCnGJsfy85mB3Wd/uINMXA65yrnFo0cPClFIUWzo2najVNSl+mx7/4W8ttlLWJe99pQ==
dependencies:
inherits "^2.0.3"
string_decoder "^1.1.1"
util-deprecate "^1.0.1"

readdirp@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/readdirp/-/readdirp-2.2.1.tgz#0e87622a3325aa33e892285caf8b4e846529a525"
Expand Down Expand Up @@ -3225,7 +3251,7 @@ rimraf@^2.6.1:
dependencies:
glob "^7.1.3"

safe-buffer@^5.1.2:
safe-buffer@^5.1.2, safe-buffer@~5.2.0:
version "5.2.0"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.0.tgz#b74daec49b1148f88c64b68d49b1e815c1f2f519"
integrity sha512-fZEwUGbVl7kouZs1jCdMLdt95hdIv0ZeHg6L7qPeciMZhZ+/gdesW4wgTARkrFWEpspjEATAzUGPG8N2jJiwbg==
Expand Down Expand Up @@ -3262,14 +3288,14 @@ semver@^6.3.0:
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==

sentence-splitter@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/sentence-splitter/-/sentence-splitter-3.1.0.tgz#85f5a6c1912a9ddb40e48186ee960d0b834a250c"
integrity sha512-S8ye/Ic5ryKynwgjcS0ggncqhVJW3mgmo/gFNIcKKU1cdLjYzqtQ/PCkD7l0rZtSSfcjLqqVjfdMVN6FKkwUuQ==
sentence-splitter@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/sentence-splitter/-/sentence-splitter-3.2.0.tgz#fb2cd2f61f40006643ba83d9acf4609233c1c68c"
integrity sha512-lKX2tZ1rsA9Tu0gW8vRmMDmIEJoZ1d7cKpzcbFZdUrSpCR6gy/7OPPh7jjT/6Oc6Z79ToUmC2l8tyTEGanVmiA==
dependencies:
"@textlint/ast-node-types" "^4.0.2"
concat-stream "^1.5.2"
object.values "^1.0.4"
"@textlint/ast-node-types" "^4.2.5"
concat-stream "^2.0.0"
object.values "^1.1.0"
structured-source "^3.0.2"

set-blocking@^2.0.0, set-blocking@~2.0.0:
Expand Down Expand Up @@ -3485,6 +3511,13 @@ string.prototype.trimright@^2.1.0:
define-properties "^1.1.3"
function-bind "^1.1.1"

string_decoder@^1.1.1:
version "1.3.0"
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.3.0.tgz#42f114594a46cf1a8e30b0a84f56c78c3edac21e"
integrity sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==
dependencies:
safe-buffer "~5.2.0"

string_decoder@~1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.1.1.tgz#9cf1611ba62685d7030ae9e4ba34149c3af03fc8"
Expand Down Expand Up @@ -3737,10 +3770,10 @@ try-resolve@^1.0.1:
resolved "https://registry.yarnpkg.com/try-resolve/-/try-resolve-1.0.1.tgz#cfde6fabd72d63e5797cfaab873abbe8e700e912"
integrity sha1-z95vq9ctY+V5fPqrhzq76OcA6RI=

ts-node@^8.4.1:
version "8.4.1"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-8.4.1.tgz#270b0dba16e8723c9fa4f9b4775d3810fd994b4f"
integrity sha512-5LpRN+mTiCs7lI5EtbXmF/HfMeCjzt7DH9CZwtkr6SywStrNQC723wG+aOWFiLNn7zT3kD/RnFqi3ZUfr4l5Qw==
ts-node@^8.5.4:
version "8.5.4"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-8.5.4.tgz#a152add11fa19c221d0b48962c210cf467262ab2"
integrity sha512-izbVCRV68EasEPQ8MSIGBNK9dc/4sYJJKYA+IarMQct1RtEot6Xp0bXuClsbUSnKpg50ho+aOAx8en5c+y4OFw==
dependencies:
arg "^4.1.0"
diff "^4.0.1"
Expand All @@ -3760,10 +3793,10 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

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

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

util-deprecate@~1.0.1:
util-deprecate@^1.0.1, util-deprecate@~1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
integrity sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=
Expand Down

0 comments on commit ef6097c

Please sign in to comment.