Skip to content

Commit

Permalink
fix(rule): 〜かもしれない をより厳密にチェックするように
Browse files Browse the repository at this point in the history
fix #3
  • Loading branch information
azu committed Sep 6, 2017
1 parent 6428752 commit 019b870
Show file tree
Hide file tree
Showing 5 changed files with 2,901 additions and 25 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
},
"dependencies": {
"kuromojin": "^1.3.1",
"morpheme-match": "^1.0.1"
"morpheme-match": "^1.0.1",
"morpheme-match-all": "^1.1.0"
}
}
44 changes: 44 additions & 0 deletions src/dict.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = [
{
"message": `弱い表現: "かも" が使われています。`,
// https://azu.github.io/morpheme-match/?text=問題がある(かも。)
"tokens": [
{
"surface_form": "かも",
Expand All @@ -13,9 +14,52 @@ module.exports = [
"basic_form": "かも",
"reading": "カモ",
"pronunciation": "カモ"
},
{
"surface_form": "。",
"pos": "記号",
"pos_detail_1": "句点",
"pos_detail_2": "*",
"pos_detail_3": "*",
"conjugated_type": "*",
"conjugated_form": "*",
"basic_form": "。",
"reading": "。",
"pronunciation": "。"
}
]
},
{
"message": `弱い表現: "かも" が使われています。`,
// https://azu.github.io/morpheme-match/?text=私は弱い(かもしれ)ない
"tokens": [
{
"surface_form": "かも",
"pos": "助詞",
"pos_detail_1": "副助詞",
"pos_detail_2": "*",
"pos_detail_3": "*",
"conjugated_type": "*",
"conjugated_form": "*",
"basic_form": "かも",
"reading": "カモ",
"pronunciation": "カモ"
},
{
"surface_form": "しれ",
"pos": "動詞",
"pos_detail_1": "自立",
"pos_detail_2": "*",
"pos_detail_3": "*",
"conjugated_type": "一段",
"conjugated_form": ["連用形", "未然形"],
"basic_form": "しれる",
"reading": "シレ",
"pronunciation": "シレ"
}
]

},
{
"message": `弱い表現: "思う" が使われています。`,
"tokens": [
Expand Down
42 changes: 20 additions & 22 deletions src/textlint-rule-ja-no-weak-phrase.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
// LICENSE : MIT
"use strict";
const tokenize = require("kuromojin").tokenize;
import dictionaryList from "./dict";
const createTokenMatcher = require("morpheme-match");
module.exports = function (context) {
const {Syntax, RuleError, report, getSource} = context;
const matcherList = dictionaryList.map(dict => {
return {
matcher: createTokenMatcher(dict["tokens"]),
message: dict["message"]
};
});
const dictionaryList = require("./dict");
const createTokenMatcher = require("morpheme-match-all");
const matchAll = createTokenMatcher(dictionaryList);
module.exports = function(context) {
const { Syntax, RuleError, report, getSource } = context;
return {
[Syntax.Str](node){
[Syntax.Str](node) {
const text = getSource(node);
return tokenize(text).then(currentTokens => {
currentTokens.forEach(token => {
matcherList.forEach(({matcher, message}) => {
const {match, tokens} = matcher(token);
if (!match) {
return;
}
const firstToken = tokens[0];
report(node, new RuleError(message, {
index: firstToken.word_position - 1
}));
});
/**
* @type {MatchResult[]}
*/
const matchResults = matchAll(currentTokens);
matchResults.forEach(matchResult => {
const firstToken = matchResult.tokens[0];
const lastToken = matchResult.tokens[matchResult.tokens.length - 1];
const firstWordIndex = Math.max(firstToken.word_position - 1, 0);
const lastWorkIndex = Math.max(lastToken.word_position - 1, 0);
// replace $1
const message = matchResult.dict.message;
report(node, new RuleError(message, {
index: firstWordIndex
}));
});
});
}
Expand Down
14 changes: 12 additions & 2 deletions test/textlint-rule-ja-no-weak-phrase-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,22 @@ tester.run("textlint-rule-ja-no-weak-phrase", rule, {
invalid: [
// single match
{
text: "弱いかもしれない",
text: "問題があるかも。",
errors: [
{
message: `弱い表現: "かも" が使われています。`,
line: 1,
column: 3
column: 6
}
]
},
{
text: "私は弱いかもしれない。",
errors: [
{
message: `弱い表現: "かも" が使われています。`,
line: 1,
column: 5
}
]
},
Expand Down
Loading

0 comments on commit 019b870

Please sign in to comment.