-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathno-doubled-conjunctive-particle-ga.js
66 lines (64 loc) · 2.59 KB
/
no-doubled-conjunctive-particle-ga.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// LICENSE : MIT
"use strict";
import { RuleHelper } from "textlint-rule-helper";
import { tokenize } from "kuromojin";
import { splitAST, Syntax as SentenceSyntax } from "sentence-splitter";
import { StringSource } from "textlint-util-to-string";
const defaultOptions = {
separatorChars: [
".", // period
".", // (ja) zenkaku-period
"。", // (ja) 句点
"?", // question mark
"!", // exclamation mark
"?", // (ja) zenkaku question mark
"!" // (ja) zenkaku exclamation mark
]
};
/*
1. Paragraph Node -> text
2. text -> sentences
3. tokenize sentence
4. report error if found word that match the rule.
TODO: need abstraction
*/
export default function (context, options = {}) {
const separatorChars = options.separatorChars ?? defaultOptions.separatorChars;
const helper = new RuleHelper(context);
const { Syntax, report, getSource, RuleError } = context;
return {
[Syntax.Paragraph](node) {
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
return;
}
const isSentenceNode = node => {
return node.type === SentenceSyntax.Sentence;
};
const sentences = splitAST(node, {
SeparatorParser: {
separatorCharacters: separatorChars
}
}).children.filter(isSentenceNode);
const source = new StringSource(node);
const checkSentence = async (sentence) => {
const sentenceText = getSource(sentence);
const tokens = await tokenize(sentenceText);
const isConjunctiveParticleGaToken = token => {
return token.pos_detail_1 === "接続助詞" && token.surface_form === "が";
};
const conjunctiveParticleGaTokens = tokens.filter(isConjunctiveParticleGaToken);
if (conjunctiveParticleGaTokens.length <= 1) {
return;
}
const current = conjunctiveParticleGaTokens[0];
const sentenceIndex = source.originalIndexFromPosition(sentence.loc.start) || 0;
const currentIndex = sentenceIndex + (current.word_position - 1);
report(node, new RuleError(`文中に逆接の接続助詞 "が" が二回以上使われています。`, {
index: currentIndex
}));
return current;
};
return Promise.all(sentences.map(checkSentence));
}
}
};