Skip to content

Commit

Permalink
feat(rule): 名詞同士に囲まれた、はカウントしない
Browse files Browse the repository at this point in the history
箇条書きの、を判別するという目的

close #2
  • Loading branch information
azu committed Nov 27, 2015
1 parent 110b303 commit b65188d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
26 changes: 23 additions & 3 deletions src/max-ten.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions test/max-ten-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var tester = new TextLintTester();
tester.run("max-ten", rule, {
// default max:3
valid: [
"名詞、名詞、名詞、名詞の場合は例外",
{
text: textIncludeTen(3 - 1)
},
Expand Down Expand Up @@ -47,15 +48,15 @@ tester.run("max-ten", rule, {
]
},
{
text: `これは、長文、columnがちゃんと計算、されてるはずです。`,
text: `これは、長文の例ですが、columnがちゃんと計算、されてるはずです。`,
options: {
"max": 3
},
errors: [
{
message: `一つの文で"、"を3つ以上使用しています`,
line: 1,
column: 21
column: 26
}
]
},
Expand Down

0 comments on commit b65188d

Please sign in to comment.