Skip to content

Commit 019b870

Browse files
committed
fix(rule): 〜かもしれない をより厳密にチェックするように
fix #3
1 parent 6428752 commit 019b870

5 files changed

+2901
-25
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
},
3939
"dependencies": {
4040
"kuromojin": "^1.3.1",
41-
"morpheme-match": "^1.0.1"
41+
"morpheme-match": "^1.0.1",
42+
"morpheme-match-all": "^1.1.0"
4243
}
4344
}

src/dict.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module.exports = [
22
{
33
"message": `弱い表現: "かも" が使われています。`,
4+
// https://azu.github.io/morpheme-match/?text=問題がある(かも。)
45
"tokens": [
56
{
67
"surface_form": "かも",
@@ -13,9 +14,52 @@ module.exports = [
1314
"basic_form": "かも",
1415
"reading": "カモ",
1516
"pronunciation": "カモ"
17+
},
18+
{
19+
"surface_form": "。",
20+
"pos": "記号",
21+
"pos_detail_1": "句点",
22+
"pos_detail_2": "*",
23+
"pos_detail_3": "*",
24+
"conjugated_type": "*",
25+
"conjugated_form": "*",
26+
"basic_form": "。",
27+
"reading": "。",
28+
"pronunciation": "。"
1629
}
1730
]
1831
},
32+
{
33+
"message": `弱い表現: "かも" が使われています。`,
34+
// https://azu.github.io/morpheme-match/?text=私は弱い(かもしれ)ない
35+
"tokens": [
36+
{
37+
"surface_form": "かも",
38+
"pos": "助詞",
39+
"pos_detail_1": "副助詞",
40+
"pos_detail_2": "*",
41+
"pos_detail_3": "*",
42+
"conjugated_type": "*",
43+
"conjugated_form": "*",
44+
"basic_form": "かも",
45+
"reading": "カモ",
46+
"pronunciation": "カモ"
47+
},
48+
{
49+
"surface_form": "しれ",
50+
"pos": "動詞",
51+
"pos_detail_1": "自立",
52+
"pos_detail_2": "*",
53+
"pos_detail_3": "*",
54+
"conjugated_type": "一段",
55+
"conjugated_form": ["連用形", "未然形"],
56+
"basic_form": "しれる",
57+
"reading": "シレ",
58+
"pronunciation": "シレ"
59+
}
60+
]
61+
62+
},
1963
{
2064
"message": `弱い表現: "思う" が使われています。`,
2165
"tokens": [

src/textlint-rule-ja-no-weak-phrase.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
// LICENSE : MIT
22
"use strict";
33
const tokenize = require("kuromojin").tokenize;
4-
import dictionaryList from "./dict";
5-
const createTokenMatcher = require("morpheme-match");
6-
module.exports = function (context) {
7-
const {Syntax, RuleError, report, getSource} = context;
8-
const matcherList = dictionaryList.map(dict => {
9-
return {
10-
matcher: createTokenMatcher(dict["tokens"]),
11-
message: dict["message"]
12-
};
13-
});
4+
const dictionaryList = require("./dict");
5+
const createTokenMatcher = require("morpheme-match-all");
6+
const matchAll = createTokenMatcher(dictionaryList);
7+
module.exports = function(context) {
8+
const { Syntax, RuleError, report, getSource } = context;
149
return {
15-
[Syntax.Str](node){
10+
[Syntax.Str](node) {
1611
const text = getSource(node);
1712
return tokenize(text).then(currentTokens => {
18-
currentTokens.forEach(token => {
19-
matcherList.forEach(({matcher, message}) => {
20-
const {match, tokens} = matcher(token);
21-
if (!match) {
22-
return;
23-
}
24-
const firstToken = tokens[0];
25-
report(node, new RuleError(message, {
26-
index: firstToken.word_position - 1
27-
}));
28-
});
13+
/**
14+
* @type {MatchResult[]}
15+
*/
16+
const matchResults = matchAll(currentTokens);
17+
matchResults.forEach(matchResult => {
18+
const firstToken = matchResult.tokens[0];
19+
const lastToken = matchResult.tokens[matchResult.tokens.length - 1];
20+
const firstWordIndex = Math.max(firstToken.word_position - 1, 0);
21+
const lastWorkIndex = Math.max(lastToken.word_position - 1, 0);
22+
// replace $1
23+
const message = matchResult.dict.message;
24+
report(node, new RuleError(message, {
25+
index: firstWordIndex
26+
}));
2927
});
3028
});
3129
}

test/textlint-rule-ja-no-weak-phrase-test.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,22 @@ tester.run("textlint-rule-ja-no-weak-phrase", rule, {
1212
invalid: [
1313
// single match
1414
{
15-
text: "弱いかもしれない",
15+
text: "問題があるかも。",
1616
errors: [
1717
{
1818
message: `弱い表現: "かも" が使われています。`,
1919
line: 1,
20-
column: 3
20+
column: 6
21+
}
22+
]
23+
},
24+
{
25+
text: "私は弱いかもしれない。",
26+
errors: [
27+
{
28+
message: `弱い表現: "かも" が使われています。`,
29+
line: 1,
30+
column: 5
2131
}
2232
]
2333
},

0 commit comments

Comments
 (0)