Skip to content

Commit 5185de5

Browse files
shirayuazu
authored andcommitted
feat(rule): 読点の挿入,"可能"の類似表現に対応 (#11)
* Added a skippable token * Added a skippable token * Designate punctuations * Added skippable tokens * Added acceptable tokens * Early return to reduce variables * Refactoring
1 parent ed638d3 commit 5185de5

File tree

5 files changed

+150
-47
lines changed

5 files changed

+150
-47
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
## 表現の一覧
88

9-
- "すること[助詞]可能"は冗長な表現です。"すること[助詞]可能"を省き簡潔な表現にすると文章が明瞭になります。
9+
- "すること[助詞]()可能"は冗長な表現です。"すること[助詞]()可能"を省き簡潔な表現にすると文章が明瞭になります。
1010
- 参考: <http://qiita.com/takahi-i/items/a93dc2ff42af6b93f6e0>
1111
- "すること[助詞]できる"は冗長な表現です。"すること[助詞]"を省き簡潔な表現にすると文章が明瞭になります。
1212
- 参考: <http://qiita.com/takahi-i/items/a93dc2ff42af6b93f6e0>

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
},
3535
"dependencies": {
3636
"kuromojin": "^1.3.2",
37-
"morpheme-match": "^1.0.1",
38-
"morpheme-match-all": "^1.1.0"
37+
"morpheme-match": "^1.2.1",
38+
"morpheme-match-all": "^1.2.0"
3939
},
4040
"keywords": [
4141
"textlintrule"

src/dictionary.js

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// MIT © 2016 azu
22
"use strict";
3+
const punctuations = ["、", "、", ",", ","];
4+
35
module.exports = [
46
{
57
// https://azu.github.io/morpheme-match/?text=省略(することが可能)。
6-
message: `"する$2$3可能$1"は冗長な表現です。"する$2$3可能"を省き簡潔な表現にすると文章が明瞭になります。`,
8+
message: `"する$2$3$4$5$1"は冗長な表現です。"する$2$3$4$5"を省き簡潔な表現にすると文章が明瞭になります。`,
79
url: "http://qiita.com/takahi-i/items/a93dc2ff42af6b93f6e0",
810
tokens: [
911
{
@@ -27,16 +29,13 @@ module.exports = [
2729
"_capture": "$3",
2830
"_readme": "[助詞]",
2931
}, {
30-
"surface_form": "可能",
31-
"pos": "名詞",
32-
"pos_detail_1": "形容動詞語幹",
33-
"pos_detail_2": "*",
34-
"pos_detail_3": "*",
35-
"conjugated_type": "*",
36-
"conjugated_form": "*",
37-
"basic_form": "可能",
38-
"reading": "カノウ",
39-
"pronunciation": "カノー"
32+
"surface_form": punctuations,
33+
"_skippable": true,
34+
"_capture": "$4",
35+
}, {
36+
"basic_form": ["可", "可能", "不可能", "不能", "不可"],
37+
"_capture": "$5",
38+
"_readme": "(不)可能",
4039
}, {
4140
"pos": "助動詞",
4241
"_capture": "$1"
@@ -45,7 +44,7 @@ module.exports = [
4544
},
4645
{
4746
// https://azu.github.io/morpheme-match/?text=解析(することができます)。
48-
message: `"する$4$3$1$2"は冗長な表現です。"する$4$3"を省き簡潔な表現にすると文章が明瞭になります。`,
47+
message: `"する$4$3$5$1$2"は冗長な表現です。"する$4$3$5"を省き簡潔な表現にすると文章が明瞭になります。`,
4948
url: "http://qiita.com/takahi-i/items/a93dc2ff42af6b93f6e0",
5049
expected: "$3$1$2",
5150
tokens: [
@@ -80,6 +79,11 @@ module.exports = [
8079
},
8180
"_readme": "[助詞]",
8281
},
82+
{
83+
"surface_form": punctuations,
84+
"_skippable": true,
85+
"_capture": "$5",
86+
},
8387
{
8488
"pos": "動詞",
8589
"pos_detail_1": "自立",
@@ -95,7 +99,7 @@ module.exports = [
9599
},
96100
{
97101
// https://azu.github.io/morpheme-match/?text=必要(であると言えます)
98-
message: `"で$1と$2ます"は冗長な表現です。"である" または "と言えます"を省き簡潔な表現にすると文章が明瞭になります。`,
102+
message: `"で$1$6と$5$2ます"は冗長な表現です。"である$6" または "と$5言えます"を省き簡潔な表現にすると文章が明瞭になります。`,
99103
url: "http://www.sekaihaasobiba.com/entry/2014/10/24/204024",
100104
tokens: [
101105
{
@@ -115,17 +119,19 @@ module.exports = [
115119
"_capture": "$1",
116120
"_readme": "ある",
117121
},
122+
{
123+
"surface_form": punctuations,
124+
"_skippable": true,
125+
"_capture": "$6",
126+
},
118127
{
119128
"surface_form": "と",
120129
"pos": "助詞",
121-
"pos_detail_1": "格助詞",
122-
"pos_detail_2": "引用",
123-
"pos_detail_3": "*",
124-
"conjugated_type": "*",
125-
"conjugated_form": "*",
126-
"basic_form": "と",
127-
"reading": "ト",
128-
"pronunciation": "ト"
130+
},
131+
{
132+
"surface_form": punctuations,
133+
"_skippable": true,
134+
"_capture": "$5",
129135
},
130136
{
131137
"pos": "動詞",
@@ -155,7 +161,7 @@ module.exports = [
155161
},
156162
{
157163
// https://azu.github.io/morpheme-match/?text=必要(であると考えている)
158-
message: `"であると考えている"は冗長な表現です。"である" または "と考えている"を省き簡潔な表現にすると文章が明瞭になります。`,
164+
message: `"である$7と$5考えて$6いる"は冗長な表現です。"である$7" または "と$5考えて$6いる"を省き簡潔な表現にすると文章が明瞭になります。`,
159165
url: "http://www.atmarkit.co.jp/ait/articles/1001/19/news106_2.html",
160166
expected: "である",
161167
tokens: [
@@ -176,17 +182,19 @@ module.exports = [
176182
"_capture": "$1",
177183
"_readme": "ある",
178184
},
185+
{
186+
"surface_form": punctuations,
187+
"_skippable": true,
188+
"_capture": "$7",
189+
},
179190
{
180191
"surface_form": "と",
181192
"pos": "助詞",
182-
"pos_detail_1": "格助詞",
183-
"pos_detail_2": "引用",
184-
"pos_detail_3": "*",
185-
"conjugated_type": "*",
186-
"conjugated_form": "*",
187-
"basic_form": "と",
188-
"reading": "ト",
189-
"pronunciation": "ト"
193+
},
194+
{
195+
"surface_form": punctuations,
196+
"_skippable": true,
197+
"_capture": "$5",
190198
},
191199
{
192200
"surface_form": "考え",
@@ -212,6 +220,11 @@ module.exports = [
212220
"reading": "テ",
213221
"pronunciation": "テ"
214222
},
223+
{
224+
"surface_form": punctuations,
225+
"_skippable": true,
226+
"_capture": "$6",
227+
},
215228
{
216229
"pos": "動詞",
217230
"pos_detail_1": "非自立",
@@ -223,7 +236,7 @@ module.exports = [
223236
},
224237
{
225238
// https://azu.github.io/morpheme-match/?text=動作の(確認を行わなければ)ならない
226-
message: `"$1を行う"は冗長な表現です。"$1する"など簡潔な表現にすると文章が明瞭になります。`,
239+
message: `"$1を$5行う"は冗長な表現です。"$1する"など簡潔な表現にすると文章が明瞭になります。`,
227240
url: "http://www.atmarkit.co.jp/ait/articles/1001/19/news106_2.html",
228241
tokens: [
229242
{
@@ -243,6 +256,11 @@ module.exports = [
243256
"reading": "ヲ",
244257
"pronunciation": "ヲ"
245258
},
259+
{
260+
"surface_form": punctuations,
261+
"_skippable": true,
262+
"_capture": "$5",
263+
},
246264
{
247265
"pos": "動詞",
248266
"pos_detail_1": "自立",
@@ -254,7 +272,7 @@ module.exports = [
254272
]
255273
},
256274
{
257-
message: `"$1を実行"は冗長な表現です。"$1する"など簡潔な表現にすると文章が明瞭になります。`,
275+
message: `"$1を$5実行"は冗長な表現です。"$1する"など簡潔な表現にすると文章が明瞭になります。`,
258276
url: "http://www.atmarkit.co.jp/ait/articles/1001/19/news106_2.html",
259277
tokens: [
260278
{
@@ -274,6 +292,11 @@ module.exports = [
274292
"reading": "ヲ",
275293
"pronunciation": "ヲ"
276294
},
295+
{
296+
"surface_form": punctuations,
297+
"_skippable": true,
298+
"_capture": "$5",
299+
},
277300
{
278301
"surface_form": "実行",
279302
"pos": "名詞",

src/index.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,46 @@ const tokenize = require("kuromojin").tokenize;
44
const dictionaryList = require("./dictionary");
55
const createMatchAll = require("morpheme-match-all");
66

7+
const replaceAll = (text, from, to) => {
8+
return text.split(from).join(to);
9+
}
10+
711
const replaceTokenWith = (matcherToken, actualToken, specialTo) => {
8-
// _captureがないのは無視
9-
if (!matcherToken._capture) {
10-
return null;
11-
}
1212
if (matcherToken[specialTo]) {
1313
return matcherToken[specialTo](actualToken);
1414
}
1515
return actualToken.surface_form;
1616
};
17-
const createExpected = ({text, matcherTokens, actualTokens}) => {
17+
const createExpected = ({text, matcherTokens, skipped, actualTokens}) => {
1818
let resultText = text;
19+
let actualTokenIndex = 0;
1920
matcherTokens.forEach((token, index) => {
20-
const to = replaceTokenWith(token, actualTokens[index], "_capture_to_expected");
21-
if (to !== null) {
22-
resultText = resultText.split(token._capture).join(to);
21+
if (skipped[index]) {
22+
resultText = replaceAll(resultText, token._capture, "");
23+
return;
24+
}
25+
if (token._capture) {
26+
const to = replaceTokenWith(token, actualTokens[actualTokenIndex], "_capture_to_expected");
27+
resultText = replaceAll(resultText, token._capture, to);
2328
}
29+
++actualTokenIndex ;
2430
});
2531
return resultText;
2632
};
27-
const createMessage = ({text, matcherTokens, actualTokens}) => {
33+
const createMessage = ({text, matcherTokens, skipped, actualTokens}) => {
2834
let resultText = text;
35+
let actualTokenIndex = 0;
2936
matcherTokens.forEach((token, index) => {
30-
const to = replaceTokenWith(token, actualTokens[index], "_capture_to_message");
31-
if (to !== null) {
32-
resultText = resultText.split(token._capture).join(to);
37+
if (skipped[index]) {
38+
resultText = replaceAll(resultText, token._capture, "");
39+
return;
40+
}
41+
42+
if (token._capture) {
43+
const to = replaceTokenWith(token, actualTokens[actualTokenIndex], "_capture_to_message");
44+
resultText = replaceAll(resultText, token._capture, to);
3345
}
46+
++actualTokenIndex ;
3447
});
3548
return resultText;
3649
};
@@ -55,13 +68,15 @@ const reporter = (context) => {
5568
const message = createMessage({
5669
text: matchResult.dict.message,
5770
matcherTokens: matchResult.dict.tokens,
71+
skipped: matchResult.skipped,
5872
actualTokens: matchResult.tokens
5973
})
6074
+ (matchResult.dict.url ? `参考: ${matchResult.dict.url}` : "");
6175
const expected = matchResult.dict.expected
6276
? createExpected({
6377
text: matchResult.dict.expected,
6478
matcherTokens: matchResult.dict.tokens,
79+
skipped: matchResult.skipped,
6580
actualTokens: matchResult.tokens
6681
})
6782
: undefined;

test/index-test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ tester.run("textlint-rule-ja-no-redundant-expression", rule, {
4848
}
4949
]
5050
},
51+
{
52+
text: "これは省略することは,可能だ。",
53+
errors: [
54+
{
55+
message: `"することは,可能だ"は冗長な表現です。"することは,可能"を省き簡潔な表現にすると文章が明瞭になります。参考: http://qiita.com/takahi-i/items/a93dc2ff42af6b93f6e0`,
56+
index: 5
57+
}
58+
]
59+
},
5160
{
5261
text: "これは省略することは可能だ。",
5362
errors: [
@@ -57,6 +66,15 @@ tester.run("textlint-rule-ja-no-redundant-expression", rule, {
5766
}
5867
]
5968
},
69+
{
70+
text: "これは省略することは不可能だ。",
71+
errors: [
72+
{
73+
message: `"することは不可能だ"は冗長な表現です。"することは不可能"を省き簡潔な表現にすると文章が明瞭になります。参考: http://qiita.com/takahi-i/items/a93dc2ff42af6b93f6e0`,
74+
index: 5
75+
}
76+
]
77+
},
6078
{
6179
text: "必要なら解析することができます。",
6280
output: "必要なら解析できます。",
@@ -67,6 +85,26 @@ tester.run("textlint-rule-ja-no-redundant-expression", rule, {
6785
}
6886
]
6987
},
88+
{
89+
text: "必要なら解析することが、できます。",
90+
output: "必要なら解析できます。",
91+
errors: [
92+
{
93+
message: `"することが、できます"は冗長な表現です。"することが、"を省き簡潔な表現にすると文章が明瞭になります。参考: http://qiita.com/takahi-i/items/a93dc2ff42af6b93f6e0`,
94+
index: 6
95+
}
96+
]
97+
},
98+
{
99+
text: "必要なら解析することが,できます。",
100+
output: "必要なら解析できます。",
101+
errors: [
102+
{
103+
message: `"することが,できます"は冗長な表現です。"することが,"を省き簡潔な表現にすると文章が明瞭になります。参考: http://qiita.com/takahi-i/items/a93dc2ff42af6b93f6e0`,
104+
index: 6
105+
}
106+
]
107+
},
70108
{
71109
text: "解析することもできますよ。",
72110
output: "解析もできますよ。",
@@ -117,6 +155,15 @@ tester.run("textlint-rule-ja-no-redundant-expression", rule, {
117155
}
118156
]
119157
},
158+
{
159+
text: "これは必要であると、言えます。",
160+
errors: [
161+
{
162+
message: `"であると、言えます"は冗長な表現です。"である" または "と、言えます"を省き簡潔な表現にすると文章が明瞭になります。参考: http://www.sekaihaasobiba.com/entry/2014/10/24/204024`,
163+
index: 5
164+
}
165+
]
166+
},
120167
{
121168
text: "これは必要で有るといえます。",
122169
errors: [
@@ -126,6 +173,15 @@ tester.run("textlint-rule-ja-no-redundant-expression", rule, {
126173
}
127174
]
128175
},
176+
{
177+
text: "これは必要である,と、考えて,います。",
178+
errors: [
179+
{
180+
message: `"である,と、考えて,いる"は冗長な表現です。"である," または "と、考えて,いる"を省き簡潔な表現にすると文章が明瞭になります。参考: http://www.atmarkit.co.jp/ait/articles/1001/19/news106_2.html`,
181+
index: 5
182+
}
183+
]
184+
},
129185
{
130186
text: "実験を行えば分かります。",
131187
errors: [
@@ -135,6 +191,15 @@ tester.run("textlint-rule-ja-no-redundant-expression", rule, {
135191
}
136192
]
137193
},
194+
{
195+
text: "実験を,行えば分かります。",
196+
errors: [
197+
{
198+
message: `"実験を,行う"は冗長な表現です。"実験する"など簡潔な表現にすると文章が明瞭になります。参考: http://www.atmarkit.co.jp/ait/articles/1001/19/news106_2.html`,
199+
index: 0
200+
}
201+
]
202+
},
138203
{
139204
text: "検査を実行すれば分かります。",
140205
errors: [

0 commit comments

Comments
 (0)