Skip to content

Commit eeabc4b

Browse files
committed
refactor(deps): use morpheme-match-textlint
1 parent 79825d7 commit eeabc4b

File tree

4 files changed

+36
-38
lines changed

4 files changed

+36
-38
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ module.exports = [
145145

146146
## 参考
147147

148-
- [azu/morpheme-match: match function that match token(形態素解析) with sentence.](https://github.com/azu/morpheme-match)
149-
- [azu/morpheme-match-all: A wrapper of morpheme-match API. Match all kuromoji's tokens.](https://github.com/azu/morpheme-match-all)
148+
- [morpheme-match](https://github.com/azu/morpheme-match)
150149

151150
## Changelog
152151

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
},
6060
"dependencies": {
6161
"kuromojin": "^1.4.0",
62-
"morpheme-match-all": "^1.1.0",
62+
"morpheme-match-textlint": "^2.0.0",
6363
"untildify": "^3.0.3"
6464
}
6565
}

src/textlint-rule-morpheme-match.js

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
2+
import { createTextlintMatcher } from "morpheme-match-textlint";
3+
24
const tokenize = require("kuromojin").tokenize;
3-
const createMatchAll = require("morpheme-match-all");
45
const path = require("path");
56
const untildify = require("untildify");
67

@@ -64,54 +65,43 @@ const reporter = (context, options) => {
6465
{
6566
dictionaryPathList: ["./path/to/dictionary.js", "./path/to/dictionary.json"]
6667
}
67-
`)
68+
`);
6869
}
6970

7071
if (!Array.isArray(options.dictionaryPathList)) {
7172
throw new Error(`"dictionaryPathList" option should be array.
7273
{
7374
dictionaryPathList: ["./path/to/dictionary.js", "./path/to/dictionary.json"]
7475
}
75-
`)
76+
`);
7677
}
7778

7879
const textlintRcDir = context.getConfigBaseDir() || process.cwd();
7980
const dictionaryList = loadDictionaries(textlintRcDir, options.dictionaryPathList);
80-
const matchAll = createMatchAll(dictionaryList);
81+
const matchAll = createTextlintMatcher({
82+
tokenize: tokenize,
83+
dictionaries: dictionaryList
84+
});
8185
return {
8286
[Syntax.Str](node) {
8387
const text = getSource(node);
84-
return tokenize(text).then(currentTokens => {
85-
/**
86-
* @type {MatchResult[]}
87-
*/
88-
const matchResults = matchAll(currentTokens);
89-
matchResults.forEach(matchResult => {
90-
const firstToken = matchResult.tokens[0];
91-
const lastToken = matchResult.tokens[matchResult.tokens.length - 1];
92-
const firstWordIndex = Math.max(firstToken.word_position - 1, 0);
93-
const lastWorkIndex = Math.max(lastToken.word_position - 1, 0);
94-
// replace $1
95-
const message = replaceWithCaptureTokens(matchResult.dict.message, matchResult.dict.tokens, matchResult.tokens);
96-
const expected = matchResult.dict.expected
97-
? replaceWithCaptureTokens(matchResult.dict.expected, matchResult.dict.tokens, matchResult.tokens)
98-
: undefined;
99-
if (expected) {
100-
report(node, new RuleError(message, {
101-
index: firstWordIndex,
102-
fix: fixer.replaceTextRange([
103-
firstWordIndex, lastWorkIndex + lastToken.surface_form.length
104-
], expected)
88+
return matchAll(text).then(results => {
89+
results.forEach(result => {
90+
if (result.expected) {
91+
report(node, new RuleError(result.message, {
92+
index: result.index,
93+
fix: fixer.replaceTextRange(result.range, result.expected)
10594
}));
10695
} else {
107-
report(node, new RuleError(message, {
108-
index: firstWordIndex
96+
report(node, new RuleError(result.message, {
97+
index: result.index
10998
}));
11099
}
100+
111101
});
112102
});
113103
}
114-
}
104+
};
115105
};
116106
module.exports = {
117107
linter: reporter,

yarn.lock

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2315,15 +2315,24 @@ moment@^2.13.0:
23152315
version "2.23.0"
23162316
resolved "https://registry.npmjs.org/moment/-/moment-2.23.0.tgz#759ea491ac97d54bac5ad776996e2a58cc1bc225"
23172317

2318-
morpheme-match-all@^1.1.0:
2319-
version "1.1.0"
2320-
resolved "https://registry.npmjs.org/morpheme-match-all/-/morpheme-match-all-1.1.0.tgz#97f05382eb89e3755d50839d77f959c111eae43e"
2318+
morpheme-match-all@^2.0.0:
2319+
version "2.0.0"
2320+
resolved "https://registry.yarnpkg.com/morpheme-match-all/-/morpheme-match-all-2.0.0.tgz#fcf518ea2a1462bc99e7cfacee1ca47942b0fcd5"
2321+
integrity sha512-eiONyhZLP0bZuXFTaoqfxigXdg5XKd+vZLwaSKFRLAhvSHY5Zrbu8iJl74HqQj+gCm1OV18OocWCAfr3FXiFXg==
23212322
dependencies:
2322-
morpheme-match "^1.1.0"
2323+
morpheme-match "^2.0.0"
23232324

2324-
morpheme-match@^1.1.0:
2325-
version "1.1.0"
2326-
resolved "https://registry.npmjs.org/morpheme-match/-/morpheme-match-1.1.0.tgz#d8f3330b95e5f27d1cb610f763285c724c25874a"
2325+
morpheme-match-textlint@^2.0.0:
2326+
version "2.0.0"
2327+
resolved "https://registry.yarnpkg.com/morpheme-match-textlint/-/morpheme-match-textlint-2.0.0.tgz#034f05dcfe7838286c675b4446f086698adb3188"
2328+
integrity sha512-6bMD5nDKPcxJ/Qd4Y1R9MwI8GQWnpFVFif8nwdRtzfibEo6FNkBRj98PzjEwXOefLMlnbjJ+DOpnPBE9yTWLqw==
2329+
dependencies:
2330+
morpheme-match-all "^2.0.0"
2331+
2332+
morpheme-match@^2.0.0:
2333+
version "2.0.0"
2334+
resolved "https://registry.yarnpkg.com/morpheme-match/-/morpheme-match-2.0.0.tgz#d95916683e119d916335ffe54e79dfb0b2ceee6a"
2335+
integrity sha512-6k8vkOliZVX4p3Ufknnlv+QshCRdkxc72b7MVq3Zo8l5dBVLuvM7L7VPnaQJwsWntI3xLLDzDLy84YMHrl35Qw==
23272336

23282337
23292338
version "2.0.0"

0 commit comments

Comments
 (0)