Skip to content
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

## 表現の一覧

- "することが可能"は冗長な表現です。"することが可能"を省き簡潔な表現にすると文章が明瞭になります。
- "すること[助詞]可能"は冗長な表現です。"すること[助詞]可能"を省き簡潔な表現にすると文章が明瞭になります。
- 参考: <http://qiita.com/takahi-i/items/a93dc2ff42af6b93f6e0>
- "することができる"は冗長な表現です。"することが"を省き簡潔な表現にすると文章が明瞭になります。
- "すること[助詞]できる"は冗長な表現です。"すること[助詞]"を省き簡潔な表現にすると文章が明瞭になります。
- 参考: <http://qiita.com/takahi-i/items/a93dc2ff42af6b93f6e0>
- "であると言えます"は冗長な表現です。"である" または "と言えます"を省き簡潔な表現にすると文章が明瞭になります。
- 参考: <http://www.sekaihaasobiba.com/entry/2014/10/24/204024>
- "であると考えている"は冗長な表現です。"である" または "と考えている"を省き簡潔な表現にすると文章が明瞭になります。
- 参考: <http://www.atmarkit.co.jp/ait/articles/1001/19/news106_2.html>
- "を行う"は冗長な表現です。"$1する"など簡潔な表現にすると文章が明瞭になります。
- "を行う"は冗長な表現です。"する"など簡潔な表現にすると文章が明瞭になります。
- 参考: <http://www.atmarkit.co.jp/ait/articles/1001/19/news106_2.html>
- "を実行"は冗長な表現です。"$1する"など簡潔な表現にすると文章が明瞭になります。
- "を実行"は冗長な表現です。"する"など簡潔な表現にすると文章が明瞭になります。
- 参考: <http://www.atmarkit.co.jp/ait/articles/1001/19/news106_2.html>

## Install
Expand Down
36 changes: 15 additions & 21 deletions src/dictionary.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module.exports = [
{
// https://azu.github.io/morpheme-match/?text=省略(することが可能)。
message: `"することが可能$1"は冗長な表現です。"することが可能"を省き簡潔な表現にすると文章が明瞭になります。`,
message: `"すること$3可能$1"は冗長な表現です。"すること$3可能"を省き簡潔な表現にすると文章が明瞭になります。`,
url: "http://qiita.com/takahi-i/items/a93dc2ff42af6b93f6e0",
tokens: [
{
Expand All @@ -29,16 +29,9 @@ module.exports = [
"reading": "コト",
"pronunciation": "コト"
}, {
"surface_form": "が",
"pos": "助詞",
"pos_detail_1": "格助詞",
"pos_detail_2": "一般",
"pos_detail_3": "*",
"conjugated_type": "*",
"conjugated_form": "*",
"basic_form": "が",
"reading": "ガ",
"pronunciation": "ガ"
"_capture": "$3",
"_readme": "[助詞]",
}, {
"surface_form": "可能",
"pos": "名詞",
Expand All @@ -58,9 +51,9 @@ module.exports = [
},
{
// https://azu.github.io/morpheme-match/?text=解析(することができます)。
message: `"することが$1$2"は冗長な表現です。"することが"を省き簡潔な表現にすると文章が明瞭になります。`,
message: `"すること$3$1$2"は冗長な表現です。"すること$3"を省き簡潔な表現にすると文章が明瞭になります。`,
url: "http://qiita.com/takahi-i/items/a93dc2ff42af6b93f6e0",
expected: "$1$2",
expected: "$3$1$2",
tokens: [
{
"surface_form": "する",
Expand All @@ -87,16 +80,17 @@ module.exports = [
"pronunciation": "コト"
},
{
"surface_form": "が",
"pos": "助詞",
"pos_detail_1": "格助詞",
"pos_detail_2": "一般",
"pos_detail_3": "*",
"conjugated_type": "*",
"conjugated_form": "*",
"basic_form": "が",
"reading": "ガ",
"pronunciation": "ガ"
"_capture": "$3",
"_capture_to_expected": function(actualToken) {
if (actualToken.surface_form === "も") {
return "も"
} else if (actualToken.surface_form === "は") {
return "は"
}
return "";
},
"_readme": "[助詞]",
},
{
"pos": "動詞",
Expand Down
46 changes: 37 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,38 @@
const tokenize = require("kuromojin").tokenize;
const dictionaryList = require("./dictionary");
const createMatchAll = require("morpheme-match-all");
const replaceWithCaptureTokens = (text, tokens, actualTokens) => {

const replaceTokenWith = (matcherToken, actualToken, specialTo) => {
// _captureがないのは無視
if (!matcherToken._capture) {
return null;
}
if (matcherToken[specialTo]) {
return matcherToken[specialTo](actualToken);
}
return actualToken.surface_form;
};
const createExpected = ({text, matcherTokens, actualTokens}) => {
let resultText = text;
matcherTokens.forEach((token, index) => {
const to = replaceTokenWith(token, actualTokens[index], "_capture_to_expected");
if (to !== null) {
resultText = resultText.split(token._capture).join(to);
}
});
return resultText;
};
const createMessage = ({text, matcherTokens, actualTokens}) => {
let resultText = text;
tokens.forEach((token, index) => {
// _captureがないのは無視
if (!token._capture) {
return;
matcherTokens.forEach((token, index) => {
const to = replaceTokenWith(token, actualTokens[index], "_capture_to_message");
if (to !== null) {
resultText = resultText.split(token._capture).join(to);
}
const actualToken = actualTokens[index];
resultText = resultText.split(token._capture).join(actualToken.surface_form);
});
return resultText;
};

const reporter = (context) => {
const {Syntax, RuleError, report, fixer, getSource} = context;
const matchAll = createMatchAll(dictionaryList);
Expand All @@ -32,10 +52,18 @@ const reporter = (context) => {
const firstWordIndex = Math.max(firstToken.word_position - 1, 0);
const lastWorkIndex = Math.max(lastToken.word_position - 1, 0);
// replace $1
const message = replaceWithCaptureTokens(matchResult.dict.message, matchResult.dict.tokens, matchResult.tokens)
const message = createMessage({
text: matchResult.dict.message,
matcherTokens: matchResult.dict.tokens,
actualTokens: matchResult.tokens
})
+ (matchResult.dict.url ? `参考: ${matchResult.dict.url}` : "");
const expected = matchResult.dict.expected
? replaceWithCaptureTokens(matchResult.dict.expected, matchResult.dict.tokens, matchResult.tokens)
? createExpected({
text: matchResult.dict.expected,
matcherTokens: matchResult.dict.tokens,
actualTokens: matchResult.tokens
})
: undefined;
if (expected) {
report(node, new RuleError(message, {
Expand Down
48 changes: 48 additions & 0 deletions test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ tester.run("textlint-rule-ja-no-redundant-expression", rule, {
}
]
},
{
text: "これは省略することも可能だ。",
errors: [
{
message: `"することも可能だ"は冗長な表現です。"することも可能"を省き簡潔な表現にすると文章が明瞭になります。参考: http://qiita.com/takahi-i/items/a93dc2ff42af6b93f6e0`,
index: 5
}
]
},
{
text: "これは省略することは可能だ。",
errors: [
{
message: `"することは可能だ"は冗長な表現です。"することは可能"を省き簡潔な表現にすると文章が明瞭になります。参考: http://qiita.com/takahi-i/items/a93dc2ff42af6b93f6e0`,
index: 5
}
]
},
{
text: "必要なら解析することができます。",
output: "必要なら解析できます。",
Expand All @@ -40,6 +58,36 @@ tester.run("textlint-rule-ja-no-redundant-expression", rule, {
}
]
},
{
text: "解析することもできますよ。",
output: "解析もできますよ。",
errors: [
{
message: `"することもできます"は冗長な表現です。"することも"を省き簡潔な表現にすると文章が明瞭になります。参考: http://qiita.com/takahi-i/items/a93dc2ff42af6b93f6e0`,
index: 2
}
]
},
{
text: "解析することはできますよ。",
output: "解析はできますよ。",
errors: [
{
message: `"することはできます"は冗長な表現です。"することは"を省き簡潔な表現にすると文章が明瞭になります。参考: http://qiita.com/takahi-i/items/a93dc2ff42af6b93f6e0`,
index: 2
}
]
},
{
text: "解析することをできますよ。",
output: "解析できますよ。",
errors: [
{
message: `"することをできます"は冗長な表現です。"することを"を省き簡潔な表現にすると文章が明瞭になります。参考: http://qiita.com/takahi-i/items/a93dc2ff42af6b93f6e0`,
index: 2
}
]
},
{
text: "これは必要であると言えます。",
errors: [
Expand Down
15 changes: 9 additions & 6 deletions tools/update-readme.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,24 @@ const replaceWithCaptureTokens = (text, tokens) => {
if (!token._capture) {
return;
}
if (token.basic_form) {
resultText = resultText.replace(token._capture, token.basic_form);
}else{
resultText = resultText.replace(token._capture, "");
if (token._readme) {
resultText = resultText.split(token._capture).join(token._readme);
} else if (token.basic_form) {
resultText = resultText.split(token._capture).join(token.basic_form);
} else {
resultText = resultText.split(token._capture).join("");
}
});
return resultText;
};
const createExamples = (dictionaries) => {
return dictionaries.map((dict) => {
return `- ${replaceWithCaptureTokens(dict.message, dict.tokens)}` + (dict.url ? `\n - 参考: ${dict.url}` : "")
return `- ${replaceWithCaptureTokens(dict.message, dict.tokens)}` + (dict.url ? `
- 参考: ${dict.url}` : "")
}).join("\n");
};

const README_PATH = path.join(__dirname, "..", "README.md");
const README = fs.readFileSync(README_PATH, "utf-8");
const UpdatedREADM = addMarkdown(README, createExamples(dict), SectionName);
fs.writeFileSync(README_PATH, UpdatedREADM, "utf-8");
fs.writeFileSync(README_PATH, UpdatedREADM, "utf-8");