Skip to content

Commit f71c272

Browse files
committed
refactor: use kuromojin@2
1 parent 003ead3 commit f71c272

File tree

7 files changed

+29
-31
lines changed

7 files changed

+29
-31
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@
3333
"textlintrule"
3434
],
3535
"devDependencies": {
36-
"@types/node": "^12.0.12",
3736
"@textlint/types": "^1.1.5",
37+
"@types/node": "^12.0.12",
3838
"textlint-scripts": "^3.0.0-beta.1",
3939
"ts-node": "^8.3.0",
4040
"typescript": "^3.5.2"
4141
},
4242
"dependencies": {
43-
"kuromojin": "^1.2.1",
43+
"kuromojin": "^2.0.0",
4444
"sentence-splitter": "^3.0.11",
4545
"textlint-rule-helper": "^2.1.1",
4646
"textlint-util-to-string": "^2.1.1"

src/no-doubled-joshi.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
"use strict";
33
import {RuleHelper} from "textlint-rule-helper";
44
import {splitAST as splitSentences, Syntax as SentenceSyntax, SentenceNode} from "sentence-splitter";
5-
const StringSource = require("textlint-util-to-string");
6-
const {getTokenizer} = require("kuromojin");
5+
import {getTokenizer, KuromojiToken} from "kuromojin";
76
import {
87
is助詞Token, is読点Token,
98
concatJoishiTokens,
109
createKeyFromKey,
11-
restoreToSurfaceFromKey, Token
10+
restoreToSurfaceFromKey
1211
} from "./token-utils";
1312
import {TextlintRuleModule} from "@textlint/types";
1413
import {TxtNode} from "@textlint/types/lib/ast-node-types/src";
1514

15+
const StringSource = require("textlint-util-to-string");
16+
1617
/**
1718
* Create token map object
1819
* {
@@ -21,7 +22,7 @@ import {TxtNode} from "@textlint/types/lib/ast-node-types/src";
2122
* @param tokens
2223
* @returns {*}
2324
*/
24-
function createSurfaceKeyMap(tokens: Token[]) {
25+
function createSurfaceKeyMap(tokens: KuromojiToken[]): { [index: string]: KuromojiToken[] } {
2526
// 助詞のみを対象とする
2627
return tokens.filter(is助詞Token).reduce((keyMap, token) => {
2728
// "は:助詞.係助詞" : [token]
@@ -31,10 +32,10 @@ function createSurfaceKeyMap(tokens: Token[]) {
3132
}
3233
keyMap[tokenKey].push(token);
3334
return keyMap;
34-
}, {});
35+
}, {} as { [index: string]: KuromojiToken[] });
3536
}
3637

37-
function matchExceptionRule(tokens: Token[]) {
38+
function matchExceptionRule(tokens: KuromojiToken[]) {
3839
let token = tokens[0];
3940
// "の" の重なりは例外
4041
if (token.pos_detail_1 === "連体化") {
@@ -69,7 +70,7 @@ const defaultOptions = {
6970
7071
TODO: need abstraction
7172
*/
72-
module.exports = function (context, options = {}) {
73+
const report: TextlintRuleModule = function (context, options = {}) {
7374
const helper = new RuleHelper(context);
7475
// 最低間隔値
7576
const minInterval = options.min_interval || defaultOptions.min_interval;
@@ -116,7 +117,7 @@ module.exports = function (context, options = {}) {
116117
}
117118
*/
118119
Object.keys(joshiTokenSurfaceKeyMap).forEach(key => {
119-
const tokens: Token[] = joshiTokenSurfaceKeyMap[key];
120+
const tokens: KuromojiToken[] = joshiTokenSurfaceKeyMap[key];
120121
const joshiName = restoreToSurfaceFromKey(key);
121122
// check allow
122123
if (allow.indexOf(joshiName) >= 0) {
@@ -153,4 +154,5 @@ module.exports = function (context, options = {}) {
153154
});
154155
}
155156
}
156-
} as TextlintRuleModule;
157+
};
158+
export default report;

src/token-utils.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// LICENSE : MIT
22
"use strict";
3-
export type Token = {
4-
[index: string]: any;
5-
}
3+
4+
import { KuromojiToken } from "kuromojin";
5+
66
// 助詞どうか
7-
export const is助詞Token = (token: Token) => {
7+
export const is助詞Token = (token: KuromojiToken) => {
88
// 結合しているtokenは助詞助詞のようになってるため先頭一致で見る
99
return token && /^/.test(token.pos);
1010
};
1111

12-
export const is読点Token = (token: Token) => {
12+
export const is読点Token = (token: KuromojiToken) => {
1313
return token.surface_form === "、" && token.pos === "名詞";
1414
};
1515
/**
@@ -18,7 +18,7 @@ export const is読点Token = (token: Token) => {
1818
* @param {Object} bToken
1919
* @returns {Object}
2020
*/
21-
const concatToken = (aToken: Token, bToken: Token) => {
21+
const concatToken = (aToken: KuromojiToken, bToken: KuromojiToken) => {
2222
aToken.surface_form += bToken.surface_form;
2323
aToken.pos += bToken.pos;
2424
aToken.pos_detail_1 += bToken.surface_form;
@@ -29,8 +29,8 @@ const concatToken = (aToken: Token, bToken: Token) => {
2929
* @param {Array} tokens
3030
* @returns {Array}
3131
*/
32-
export const concatJoishiTokens = (tokens: Token[]) => {
33-
const newTokens: Token[] = [];
32+
export const concatJoishiTokens = (tokens: KuromojiToken[]) => {
33+
const newTokens: KuromojiToken[] = [];
3434
tokens.forEach((token) => {
3535
const prevToken = newTokens[newTokens.length - 1];
3636
if (is助詞Token(token) && is助詞Token(prevToken)) {
@@ -44,7 +44,7 @@ export const concatJoishiTokens = (tokens: Token[]) => {
4444
// 助詞tokenから品詞細分類1までを元にしたkeyを作る
4545
// http://www.unixuser.org/~euske/doc/postag/index.html#chasen
4646
// http://chasen.naist.jp/snapshot/ipadic/ipadic/doc/ipadic-ja.pdf
47-
export const createKeyFromKey = (token: Token) => {
47+
export const createKeyFromKey = (token: KuromojiToken) => {
4848
// e.g.) "は:助詞.係助詞"
4949
return `${token.surface_form}:${token.pos}.${token.pos_detail_1}`;
5050
};

test/mocha.opts

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/no-doubled-joshi-test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import TextLintTester from "textlint-tester";
2-
const rule = require("../src/no-doubled-joshi");
2+
import rule from "../src/no-doubled-joshi";
3+
34
const tester = new TextLintTester();
4-
/*
5-
`**`のような装飾は取り除かれてから評価されているので、
6-
テストでの強調という意味合いのみで利用する。
7-
*/
85
tester.run("no-double-joshi", rule, {
9-
106
valid: [
117
"私は彼が好きだ",
128
"既存のコードの利用", // "の" の例外

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"moduleResolution": "node",
66
"esModuleInterop": true,
77
"noEmit": true,
8+
"skipLibCheck": true,
89
"target": "es2015",
910
/* Strict Type-Checking Options */
1011
"strict": true,

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2311,10 +2311,10 @@ kuromoji@0.1.1:
23112311
doublearray "0.0.2"
23122312
zlibjs "^0.2.0"
23132313

2314-
kuromojin@^1.2.1:
2315-
version "1.4.0"
2316-
resolved "https://registry.yarnpkg.com/kuromojin/-/kuromojin-1.4.0.tgz#2d955746736981e73d02dbfd20eeee8110cddab2"
2317-
integrity sha512-ZoG3NAW6ATKgo2jBgjVWwE66GeaT2jHgdf0v1dL8+WffuSfvTvX7l6ePgYusSAhHW+dwtneYcbav8N0NhsA0uw==
2314+
kuromojin@^2.0.0:
2315+
version "2.0.0"
2316+
resolved "https://registry.yarnpkg.com/kuromojin/-/kuromojin-2.0.0.tgz#23e74a5ed2578432c9703ae75a69ba0a09ccb12d"
2317+
integrity sha512-60j/yLkFSc4t4roj8tI8ZNNSiAFnrkgXw8SqXz/9nakfs6mkCvPbrd7S8LDr4YNwEt1IyLys5JQTR9EnYyGHhA==
23182318
dependencies:
23192319
kuromoji "0.1.1"
23202320

0 commit comments

Comments
 (0)