From b65188dd54d645887fd1e01509fba02f43bfb8f4 Mon Sep 17 00:00:00 2001 From: azu Date: Fri, 27 Nov 2015 19:48:45 +0900 Subject: [PATCH] =?UTF-8?q?feat(rule):=20=E5=90=8D=E8=A9=9E=E5=90=8C?= =?UTF-8?q?=E5=A3=AB=E3=81=AB=E5=9B=B2=E3=81=BE=E3=82=8C=E3=81=9F=E3=80=81?= =?UTF-8?q?=E3=81=AF=E3=82=AB=E3=82=A6=E3=83=B3=E3=83=88=E3=81=97=E3=81=AA?= =?UTF-8?q?=E3=81=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 箇条書きの、を判別するという目的 close #2 --- src/max-ten.js | 26 +++++++++++++++++++++++--- test/max-ten-test.js | 5 +++-- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/max-ten.js b/src/max-ten.js index df92f39..1b59bc6 100644 --- a/src/max-ten.js +++ b/src/max-ten.js @@ -6,6 +6,17 @@ import {getTokenizer} from "kuromojin"; import splitSentences from "sentence-splitter"; import Source from "structured-source"; const defaultOptions = {max: 3}; + +function isSandwichedMeishi({ + before, + token, + after + }) { + if (before === undefined || after === undefined || token === undefined) { + return false; + } + return before.pos === "名詞" && after.pos === "名詞"; +} /** * @param {RuleContext} context * @param {object} options @@ -37,9 +48,18 @@ export default function (context, options = {}) { let currentTenCount = 0; let tokens = tokenizer.tokenizeForSentence(text); let lastToken = null; - tokens.forEach(token => { + tokens.forEach((token, index) => { let surface = token.surface_form; if (surface === "、") { + // 名詞に過去まわれている場合は例外とする + let isSandwiched = isSandwichedMeishi({ + before: tokens[index - 1], + token: token, + after: tokens[index + 1] + }); + if (isSandwiched) { + return; + } currentTenCount++; lastToken = token; } @@ -51,8 +71,8 @@ export default function (context, options = {}) { if (currentTenCount >= maxLen) { let position = source.indexToPosition(lastToken.word_position - 1); let ruleError = new context.RuleError(`一つの文で"、"を${maxLen}つ以上使用しています`, { - line: position.line-1, - column:position.column + line: position.line - 1, + column: position.column }); report(node, ruleError); currentTenCount = 0; diff --git a/test/max-ten-test.js b/test/max-ten-test.js index 64610f1..40533db 100644 --- a/test/max-ten-test.js +++ b/test/max-ten-test.js @@ -8,6 +8,7 @@ var tester = new TextLintTester(); tester.run("max-ten", rule, { // default max:3 valid: [ + "名詞、名詞、名詞、名詞の場合は例外", { text: textIncludeTen(3 - 1) }, @@ -47,7 +48,7 @@ tester.run("max-ten", rule, { ] }, { - text: `これは、長文、columnがちゃんと計算、されてるはずです。`, + text: `これは、長文の例ですが、columnがちゃんと計算、されてるはずです。`, options: { "max": 3 }, @@ -55,7 +56,7 @@ tester.run("max-ten", rule, { { message: `一つの文で"、"を3つ以上使用しています`, line: 1, - column: 21 + column: 26 } ] },