Skip to content

Commit 65dc1a4

Browse files
authored
fix: Use tokenize instead of getTokenizer (#16)
* Use tokenize instead of getTokenizer * Bug fix
1 parent 8730de0 commit 65dc1a4

File tree

1 file changed

+51
-52
lines changed

1 file changed

+51
-52
lines changed

src/max-ten.js

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// LICENSE : MIT
22
"use strict";
33
import { RuleHelper } from "textlint-rule-helper";
4-
import { getTokenizer } from "kuromojin";
4+
import { tokenize } from "kuromojin";
55
import { splitAST, Syntax as SentenceSyntax } from "sentence-splitter";
66
import { StringSource } from "textlint-util-to-string";
77

@@ -103,59 +103,58 @@ module.exports = function (context, options = {}) {
103103
2. sentence to tokens
104104
3. check tokens
105105
*/
106-
return getTokenizer().then((tokenizer) => {
107-
sentences.forEach((sentence) => {
108-
const source = new StringSource(sentence);
109-
const text = source.toString();
110-
const tokens = tokenizer.tokenizeForSentence(text);
111-
let currentTenCount = 0;
112-
let lastToken = null;
113-
tokens.forEach((token, index) => {
114-
const surface = token.surface_form;
115-
if (surface === touten) {
116-
// 名詞に囲まわれている場合は例外とする
117-
const isSandwiched = isSandwichedMeishi({
118-
before: findSiblingMeaningToken({
119-
tokens,
120-
currentIndex: index,
121-
direction: "prev"
122-
}),
123-
token: token,
124-
after: findSiblingMeaningToken({
125-
tokens,
126-
currentIndex: index,
127-
direction: "next"
128-
})
129-
});
130-
// strictなら例外を例外としない
131-
if (!isStrict && isSandwiched) {
132-
return;
133-
}
134-
currentTenCount++;
135-
lastToken = token;
136-
}
137-
if (surface === kuten) {
138-
// reset
139-
currentTenCount = 0;
140-
}
141-
// report
142-
if (currentTenCount > maxLen) {
143-
const positionInSentence = source.originalIndexFromIndex(lastToken.word_position - 1);
144-
// relative index from Paragraph Node
145-
// Sentence start(relative) + word position(relative)
146-
const index = sentence.range[0] - node.range[0] + positionInSentence;
147-
const ruleError = new context.RuleError(
148-
`一つの文で"${touten}"を${maxLen + 1}つ以上使用しています`,
149-
{
150-
index
151-
}
152-
);
153-
report(node, ruleError);
154-
currentTenCount = 0;
106+
const checkSentence = async (sentence) => {
107+
const source = new StringSource(sentence);
108+
const text = source.toString();
109+
const tokens = await tokenize(text);
110+
let currentTenCount = 0;
111+
let lastToken = null;
112+
tokens.forEach((token, index) => {
113+
const surface = token.surface_form;
114+
if (surface === touten) {
115+
// 名詞に囲まわれている場合は例外とする
116+
const isSandwiched = isSandwichedMeishi({
117+
before: findSiblingMeaningToken({
118+
tokens,
119+
currentIndex: index,
120+
direction: "prev"
121+
}),
122+
token: token,
123+
after: findSiblingMeaningToken({
124+
tokens,
125+
currentIndex: index,
126+
direction: "next"
127+
})
128+
});
129+
// strictなら例外を例外としない
130+
if (!isStrict && isSandwiched) {
131+
return;
155132
}
156-
});
133+
currentTenCount++;
134+
lastToken = token;
135+
}
136+
if (surface === kuten) {
137+
// reset
138+
currentTenCount = 0;
139+
}
140+
// report
141+
if (currentTenCount > maxLen) {
142+
const positionInSentence = source.originalIndexFromIndex(lastToken.word_position - 1);
143+
// relative index from Paragraph Node
144+
// Sentence start(relative) + word position(relative)
145+
const index = sentence.range[0] - node.range[0] + positionInSentence;
146+
const ruleError = new context.RuleError(
147+
`一つの文で"${touten}"を${maxLen + 1}つ以上使用しています`,
148+
{
149+
index
150+
}
151+
);
152+
report(node, ruleError);
153+
currentTenCount = 0;
154+
}
157155
});
158-
});
156+
};
157+
return Promise.all(sentences.map(checkSentence));
159158
}
160159
};
161160
};

0 commit comments

Comments
 (0)