Skip to content

Use tokenize instead of getTokenizer #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
40 changes: 19 additions & 21 deletions src/no-doubled-conjunctive-particle-ga.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// LICENSE : MIT
"use strict";
import { RuleHelper } from "textlint-rule-helper";
import { getTokenizer } from "kuromojin";
import { tokenize } from "kuromojin";
import { splitAST, Syntax as SentenceSyntax } from "sentence-splitter";
import { StringSource } from "textlint-util-to-string";

Expand Down Expand Up @@ -42,27 +42,25 @@ export default function (context, options = {}) {
}
}).children.filter(isSentenceNode);
const source = new StringSource(node);
return getTokenizer().then(tokenizer => {
const checkSentence = (sentence) => {
const sentenceText = getSource(sentence);
const tokens = tokenizer.tokenizeForSentence(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;
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;
}
sentences.forEach(checkSentence);
});
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));
}
}
};