|
1 | 1 | // LICENSE : MIT
|
2 | 2 | "use strict";
|
3 | 3 | import { RuleHelper } from "textlint-rule-helper";
|
4 |
| -import { getTokenizer } from "kuromojin"; |
| 4 | +import { tokenize } from "kuromojin"; |
5 | 5 | import { split as splitSentences, Syntax as SentenceSyntax } from "sentence-splitter";
|
6 | 6 | import { StringSource } from "textlint-util-to-string";
|
7 | 7 |
|
@@ -42,46 +42,44 @@ export default function (context, options = {}) {
|
42 | 42 | if (sentences.length === 0) {
|
43 | 43 | return;
|
44 | 44 | }
|
45 |
| - return getTokenizer().then(tokenizer => { |
46 |
| - const selectConjenction = (sentence) => { |
47 |
| - const tokens = tokenizer.tokenizeForSentence(sentence.raw); |
48 |
| - const conjunctionTokens = tokens.filter((token, index) => { |
49 |
| - const prevToken = tokens[index - 1]; |
50 |
| - // スペースが切れ目として認識されてしまう問題を回避 |
51 |
| - // https://github.com/textlint-ja/textlint-rule-no-doubled-conjunction/issues/14 |
52 |
| - if (prevToken && prevToken.pos_detail_1 === "空白" && token.pos === "接続詞") { |
53 |
| - return false; |
54 |
| - } |
55 |
| - return token.pos === "接続詞" |
56 |
| - }); |
57 |
| - return [sentence, conjunctionTokens]; |
58 |
| - }; |
59 |
| - let prev_token = null; |
60 |
| - sentences.map(selectConjenction).reduce((prev, current) => { |
61 |
| - const [sentence, current_tokens] = current; |
62 |
| - const [prev_sentence, prev_tokens] = prev; |
63 |
| - let token = prev_token; |
64 |
| - if (prev_tokens && prev_tokens.length > 0) { |
65 |
| - token = prev_tokens[0]; |
| 45 | + const selectConjenction = async (sentence) => { |
| 46 | + const tokens = await tokenize(sentence.raw); |
| 47 | + const conjunctionTokens = tokens.filter((token, index) => { |
| 48 | + const prevToken = tokens[index - 1]; |
| 49 | + // スペースが切れ目として認識されてしまう問題を回避 |
| 50 | + // https://github.com/textlint-ja/textlint-rule-no-doubled-conjunction/issues/14 |
| 51 | + if (prevToken && prevToken.pos_detail_1 === "空白" && token.pos === "接続詞") { |
| 52 | + return false; |
66 | 53 | }
|
67 |
| - if (current_tokens.length > 0) { |
68 |
| - if (token && current_tokens[0].surface_form === token.surface_form) { |
69 |
| - const conjunctionSurface = token.surface_form; |
70 |
| - const originalIndex = source.originalIndexFromPosition({ |
71 |
| - line: sentence.loc.start.line, |
72 |
| - column: sentence.loc.start.column + (current_tokens[0].word_position - 1) |
73 |
| - }); |
74 |
| - // padding position |
75 |
| - const padding = { |
76 |
| - index: originalIndex |
77 |
| - }; |
78 |
| - report(node, new RuleError(`同じ接続詞(${conjunctionSurface})が連続して使われています。`, padding)); |
79 |
| - } |
80 |
| - } |
81 |
| - prev_token = token; |
82 |
| - return current; |
| 54 | + return token.pos === "接続詞" |
83 | 55 | });
|
84 |
| - }); |
| 56 | + return [sentence, conjunctionTokens]; |
| 57 | + } |
| 58 | + let prev_token = null; |
| 59 | + return Promise.all(sentences.map(selectConjenction)).then((result) => result.reduce((prev, current) => { |
| 60 | + const [sentence, current_tokens] = current; |
| 61 | + const [prev_sentence, prev_tokens] = prev; |
| 62 | + let token = prev_token; |
| 63 | + if (prev_tokens && prev_tokens.length > 0) { |
| 64 | + token = prev_tokens[0]; |
| 65 | + } |
| 66 | + if (current_tokens.length > 0) { |
| 67 | + if (token && current_tokens[0].surface_form === token.surface_form) { |
| 68 | + const conjunctionSurface = token.surface_form; |
| 69 | + const originalIndex = source.originalIndexFromPosition({ |
| 70 | + line: sentence.loc.start.line, |
| 71 | + column: sentence.loc.start.column + (current_tokens[0].word_position - 1) |
| 72 | + }); |
| 73 | + // padding position |
| 74 | + const padding = { |
| 75 | + index: originalIndex |
| 76 | + }; |
| 77 | + report(node, new RuleError(`同じ接続詞(${conjunctionSurface})が連続して使われています。`, padding)); |
| 78 | + } |
| 79 | + } |
| 80 | + prev_token = token; |
| 81 | + return current; |
| 82 | + })); |
85 | 83 | }
|
86 | 84 | }
|
87 | 85 | };
|
0 commit comments