Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rule): カッコで囲まれた場合に外との連続判定ではないとする修正 #32

Merged
merged 2 commits into from
Sep 15, 2020
Merged
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ textlint --rule no-doubled-joshi README.md

読点文字は `commaCharacters` オプションで指定できます。

### カッコでの区切り

> 次の`escapeHTML`関数は**タグ関数**です(詳細は文字列の章を参照)

括弧(`(`や`)`)が助詞の間にある場合、間隔値は+1されます。
そのため、この例の助詞`は`の間隔値は`2`となりデフォルトではエラーとなりません。

括弧記号はkuromoji.jsで定義されている記号を元に判定しています。

## 例外

以下の項目については、曖昧性があるため助詞が連続していてもデフォルトではエラーとして扱いません。
Expand Down
19 changes: 14 additions & 5 deletions src/no-doubled-joshi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
create読点Matcher,
concatJoishiTokens,
createKeyFromKey,
restoreToSurfaceFromKey
restoreToSurfaceFromKey, is括弧Token
} from "./token-utils";
import { TxtNode } from "@textlint/ast-node-types";
import { TextlintRuleModule } from "@textlint/types";
Expand Down Expand Up @@ -113,7 +113,7 @@ export interface Options {

TODO: need abstraction
*/
const report: TextlintRuleModule<Options> = function(context, options = {}) {
const report: TextlintRuleModule<Options> = function (context, options = {}) {
const helper = new RuleHelper(context);
// 最低間隔値
const minInterval = options.min_interval !== undefined ? options.min_interval : defaultOptions.min_interval;
Expand All @@ -124,7 +124,7 @@ const report: TextlintRuleModule<Options> = function(context, options = {}) {
const allow = options.allow || defaultOptions.allow;
const separatorCharacters = options.separatorCharacters || defaultOptions.separatorCharacters;
const commaCharacters = options.commaCharacters || defaultOptions.commaCharacters;
const { Syntax, report, RuleError } = context;
const {Syntax, report, RuleError} = context;
const is読点Token = create読点Matcher(commaCharacters);
return {
[Syntax.Paragraph](node) {
Expand Down Expand Up @@ -154,10 +154,19 @@ const report: TextlintRuleModule<Options> = function(context, options = {}) {
if (isStrict) {
return is助詞Token(token);
}
// デフォルトでは、"、"を間隔値の距離としてカウントする
// "("や")"などもトークンとしてカウントする
// xxxx(xxx) xxx でカッコの中と外に距離を一つ増やす目的
// https://github.com/textlint-ja/textlint-rule-no-doubled-joshi/issues/31
if (is括弧Token(token)) {
return true;
}
// "、" があると助詞同士の距離が開くようにすることで、並列的な"、"の使い方を許容する目的
// https://github.com/azu/textlint-rule-no-doubled-joshi/issues/2
return is助詞Token(token) || is読点Token(token);
if (is読点Token(token)) {
return true;
}
// デフォルトでは、"、"を間隔値の距離としてカウントする
return is助詞Token(token);
});
const joshiTokenSurfaceKeyMap = createSurfaceKeyMap(countableTokens);
/*
Expand Down
4 changes: 4 additions & 0 deletions src/token-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export const is助詞Token = (token: KuromojiToken) => {
return token && /^助詞/.test(token.pos);
};

export const is括弧Token = (token: KuromojiToken) => {
return token && token.pos === "記号" && (token.pos_detail_1 === "括弧開" || token.pos_detail_1 === "括弧閉")
}

/**
* 読点を判定する関数を返す
* 注意: 名詞や記号ではないトークンは読点として扱えない
Expand Down
7 changes: 5 additions & 2 deletions test/no-doubled-joshi-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ tester.run("no-double-joshi", rule, {
"AとBとCを持ってきて",
// fix regression - https://travis-ci.org/textlint-ja/textlint-rule-preset-ja-technical-writing/builds/207700760#L720
"慣用的表現、熟語、概数、固有名詞、副詞など、漢数字を使用することが一般的な語句では漢数字を使います。",
// カッコ内は別のセンテンスとしてみなす
// https://github.com/textlint-ja/textlint-rule-no-doubled-joshi/issues/31
" 次の`escapeHTML`関数は**タグ関数**です(詳細は文字列の章を参照)。",
// 1個目の「と」は格助詞、2個めの「と」は接続助詞
"ターミナルで「test」**と**入力する**と**、画面に表示されます。",
// 格助詞の種類が異なる
Expand Down Expand Up @@ -185,7 +188,7 @@ tester.run("no-double-joshi", rule, {
},
//
{
text: `今まで「サイトはNetlify」「スライドはGitLab Pages」といった配信分けをしていたのですが
text: `今まで、サイトはNetlifyスライドはGitLab Pagesといった配信分けをしていたのですが
「 \`/slides\` にビルドしたスライドを置きたい」という動機のものと、こんな構成を検討しています。

* 最初にtextlintで文法チェック
Expand All @@ -194,7 +197,7 @@ tester.run("no-double-joshi", rule, {
errors: [
{
message: `一文に二回以上利用されている助詞 "は" がみつかりました。`,
index: 21
index: 19
}
]
},
Expand Down