Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ textlint --rule no-doubled-joshi README.md
"strict": false,
// 助詞のうち「も」「や」は複数回の出現を許す
"allow": ["も","や"],
// 助詞に対してどの回数まで出現を許すか
"allowCount": {
"が": 2,
"も": 3
},
// 直接数字を指定すれば全ての助詞に対してその数だけ出現を許す
// "allowCount": 3,
// 文の区切り文字となる配列
"separatorCharacters": [
".", // period
Expand Down Expand Up @@ -112,6 +119,10 @@ textlint --rule no-doubled-joshi README.md
- Default: `[]`
- 並立の助詞など、複数回出現しても無視する助詞を指定します
- 例) `"も"`を許可したい場合は `{ "allow": ["も"] }`
- `allowCount`: 助詞の出現上限値
- Default: `1`
- 助詞ごとに一文の中で出現を許す最大回数を指定します。数値(すべての助詞に適用)またはオブジェクト(助詞ごとの上限)で指定できます。
- 例) `"allowCount": 3` または `"allowCount": { "が": 2, "も": 3 }`
- `separatorCharacters`: 文の区切り文字の配列
- Default: `[".", ".", "。", "?", "!", "?", "!"]`
- `separatorCharacters`を設定するとデフォルト値は上書きされます
Expand Down
37 changes: 29 additions & 8 deletions src/no-doubled-joshi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ export interface Options {
* 例): ["も", "や"]
*/
allow?: string[];
/**
* 助詞に対してどの回数まで出現を許すか
* 例): 3 または { "が": 2, "も": 3 }
*/
allowCount?: number | { [index: string]: number };
/**
* 文の区切りとなる文字(句点)の配列
*/
Expand All @@ -130,6 +135,22 @@ export interface Options {
commaCharacters?: string[];
}

const getAllowCount = (joshiName: string, allowCountOption?: number | { [index: string]: number }): number => {
if (allowCountOption === undefined) {
return 1;
}
if (typeof allowCountOption === "number") {
return Math.max(1, allowCountOption);
}
if (typeof allowCountOption === "object" && allowCountOption !== null) {
const count = allowCountOption[joshiName];
if (count !== undefined) {
return Math.max(1, count);
}
}
return 1;
};

/**
* "~~~~~~{助詞}" から {Token}"{助詞}" という形になるように、前の単語を含めた助詞の文字列を取得する
*
Expand Down Expand Up @@ -279,12 +300,13 @@ const report: TextlintRuleModule<Options> = function (context, options = {}) {
return;
}
}
if (joshiTokenSurfaceTokens.length <= 1) {
return; // no duplicated token
const allowCount = getAllowCount(joshiName, options.allowCount);
if (joshiTokenSurfaceTokens.length <= allowCount) {
return; // no duplicated token exceeding allowCount
}
// if found differenceIndex less than
// tokes are sorted ascending order
joshiTokenSurfaceTokens.reduce((prev, current) => {
for (let i = allowCount; i < joshiTokenSurfaceTokens.length; i++) {
const prev = joshiTokenSurfaceTokens[i - 1];
const current = joshiTokenSurfaceTokens[i];
const startPosition = countableJoshiTokens.indexOf(prev);
const otherPosition = countableJoshiTokens.indexOf(current);
// 助詞token同士の距離が設定値以下ならエラーを報告する
Expand All @@ -303,7 +325,7 @@ const report: TextlintRuleModule<Options> = function (context, options = {}) {
const originalIndex = sentenceSource.originalIndexFromIndex(current.word_position - 1);
// originalIndexがない場合は基本的にはないが、ない場合は無視する
if (originalIndex === undefined) {
return current;
continue;
}
report(
// @ts-expect-error: SentenceNodeは独自であるため
Expand All @@ -327,8 +349,7 @@ const report: TextlintRuleModule<Options> = function (context, options = {}) {
)
);
}
return current;
});
}
});
};
return Promise.all(sentences.map(checkSentence));
Expand Down
56 changes: 56 additions & 0 deletions test/no-doubled-joshi-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ tester.run("no-double-joshi", rule, {
{
text: "これがiPhone,これがAndroidです。",
options: { commaCharacters: [","] }
},
// allowCount: 3 の場合は、同じ助詞が3回出現してもエラーにならない
{
text: "僕が私があなたが言った。",
options: {
allowCount: 3
}
},
// allowCount: {"が": 2} の場合は、がが2回出現してもエラーにならない
{
text: "あちらへこちらへ飛んでいけ。",
options: {
allowCount: { へ: 2 }
}
}
],
invalid: [
Expand Down Expand Up @@ -403,6 +417,48 @@ tester.run("no-double-joshi", rule, {
index: 12
}
]
},
// allowCount: 2 の場合に、3回出現した場合はエラーになる
{
text: "僕が私があなたが言った。",
options: {
allowCount: 2
},
errors: [
{
message: `一文に二回以上利用されている助詞 "が" がみつかりました。

次の助詞が連続しているため、文を読みにくくしています。

- 私"が"
- あなた"が"

同じ助詞を連続して利用しない、文の中で順番を入れ替える、文を分割するなどを検討してください。
`,
index: 7
}
]
},
// allowCount: {"へ": 1} の場合に、2回出現した場合はエラーになる
{
text: "あちらへこちらへ飛んでいけ。",
options: {
allowCount: { へ: 1 }
},
errors: [
{
message: `一文に二回以上利用されている助詞 "へ" がみつかりました。

次の助詞が連続しているため、文を読みにくくしています。

- あちら"へ"
- こちら"へ"

同じ助詞を連続して利用しない、文の中で順番を入れ替える、文を分割するなどを検討してください。
`,
index: 7
}
]
}
]
});