Skip to content

Commit

Permalink
feat(morpheme-match-textlint): add textlint wrapper (#7)
Browse files Browse the repository at this point in the history
* feat(morpheme-match-textlint): add textlint wrapper

* docs(morpheme-match-textlint): Update README

* Update README

* fix(morpheme-match): fix token type

* fix(morpheme-match): Update Token type

* update test

* fix type

* fix(website): fix (xxx) pattern

* fix to use correct token
  • Loading branch information
azu authored Jun 14, 2019
1 parent f8a35d9 commit 8f3be6a
Show file tree
Hide file tree
Showing 20 changed files with 750 additions and 83 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
# morpheme-match
# morpheme-match [![Build Status](https://travis-ci.org/azu/morpheme-match.svg?branch=master)](https://travis-ci.org/azu/morpheme-match)

A monorepo for morpheme-match.

`morpheme-match`は日本語を形態素解析したトークンを元に、文章にマッチするかを判定するライブラリです。
文字列比較ベースでは誤検知しやすい辞書ベースのチェックを、形態素解析を使うことでより厳密に行うことが目的です。

## Demo

- Visit: <https://azu.github.io/morpheme-match/>

## packages

- [packages/morpheme-match](packages/morpheme-match)
- [morpheme-match](packages/morpheme-match)
- Core library
- [morpheme-match-all](packages/morpheme-match-all)
- A wrapper library of morpheme-match
- Easy to use edition
- [morpheme-match-textlint](packages/morpheme-match-textlint)
- A wrapper for textlint
- Use it in textlint rule
- [website](website)
- Demo site
- <https://azu.github.io/morpheme-match/>
10 changes: 5 additions & 5 deletions packages/morpheme-match-all/src/Expector.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// MIT © 2016 azu
"use strict";
import * as assert from "assert";
import { createTokenMatcher, Token, ExpectedToken, MatchResult } from "morpheme-match";
import {createTokenMatcher, Token, ExpectedToken, MatchResult} from "morpheme-match";

export type ExpectedDictionary = {
tokens: ExpectedToken[];
export type ExpectedDictionary<T extends ExpectedToken> = {
tokens: T[];
message: string;
expected?: string;
};

export class Expector {
export class Expector<T extends ExpectedToken> {
private matcher: any;

constructor(public dict: ExpectedDictionary) {
constructor(public dict: ExpectedDictionary<T>) {
assert.ok(Array.isArray(dict["tokens"]), `"tokens" property is required. ${dict}`);
this.matcher = createTokenMatcher(dict.tokens);
}
Expand Down
16 changes: 9 additions & 7 deletions packages/morpheme-match-all/src/morpheme-match-all.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
// MIT © 2016 azu
"use strict";
import { ExpectedDictionary, Expector } from "./Expector";
import { Token } from "morpheme-match/lib/morpheme-match";
import {ExpectedDictionary, Expector} from "./Expector";
import {ExpectedToken, Token} from "morpheme-match";

export type MatchResult = {
export type ExpectedDictionaries<T extends ExpectedToken> = ExpectedDictionary<T>[]
export {ExpectedDictionary};
export type MatchResult<T extends ExpectedToken> = {
tokens: Token[];
index: number;
skipped: boolean[];
dict: ExpectedDictionary;
dict: ExpectedDictionary<T>;
};

/**
* Create Matcher function for match all multiple tokens
*/
export function createMatcher(dictionaries: ExpectedDictionary[]) {
export function createMatcher<T extends ExpectedToken>(dictionaries: ExpectedDictionaries<T>) {
const expectors = dictionaries.map(dict => {
return new Expector(dict);
});
Expand All @@ -25,10 +27,10 @@ export function createMatcher(dictionaries: ExpectedDictionary[]) {
* @public
*/
return function morphemeMatchAll(actualTokens: Token[]) {
const matchResults: MatchResult[] = [];
const matchResults: MatchResult<T>[] = [];
actualTokens.forEach(actualToken => {
expectors.forEach(expector => {
const { match, tokens, skipped } = expector.match(actualToken);
const {match, tokens, skipped} = expector.match(actualToken);
if (!match) {
return;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/morpheme-match-all/test/morpheme-match-all-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ describe("morpheme-match-all", () => {
const results = matchAll(actualTokens);
/**
[ { tokens: [ [Object], [Object], [Object], [Object] ],
index: 1,
dict:
{ message: '"することができる"は有害 http://qiita.com/takahi-i/items/a93dc2ff42af6b93f6e0',
tokens: [Object] } } ]
index: 1,
dict:
{ message: '"することができる"は有害 http://qiita.com/takahi-i/items/a93dc2ff42af6b93f6e0',
tokens: [Object] } } ]
*/
assert.strictEqual(results.length, 1);
assert.deepStrictEqual(results[0].skipped, [false, false, false, true, false]);
Expand Down
1 change: 1 addition & 0 deletions packages/morpheme-match-textlint/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib/
19 changes: 19 additions & 0 deletions packages/morpheme-match-textlint/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2016 azu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
99 changes: 99 additions & 0 deletions packages/morpheme-match-textlint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# morpheme-match-textlint

morpheme-match for textlint.

This library provide matcher function that return reportable object for textlint.

-

## Install

Install with [npm](https://www.npmjs.com/):

npm install morpheme-match-textlint

## Usage

- [ ] TBO

### Tips

### `_capture`

`_capture` is special key for replacing message and expected.


```js
const matchToken = createTokenMatcher([
{
"surface_form": "かも",
"pos": "助詞",
"pos_detail_1": "副助詞",
"pos_detail_2": "*",
"pos_detail_3": "*",
"conjugated_type": "*",
"conjugated_form": "*",
"basic_form": "かも",
"reading": "カモ",
"pronunciation": "カモ",
"_cature": "$1"
}
]);
```

### `_skippable`

If the token has `_skippable`, this token will be handled as optional token.


```js
const matchToken = createTokenMatcher([
{
"surface_form": "かも",
},
{
"surface_form": "",
"_skippable": true,
},
{
"surface_form": "しれ",
},
]);
```


## 関連

- [azu/morpheme-match-all: A wrapper of morpheme-match API. Match all kuromoji's tokens.](https://github.com/azu/morpheme-match-all)
- A wrapper for morpheme-match
- [textlint-ja/textlint-rule-morpheme-match: 形態素解析結果のTokenベースの辞書でマッチするtextlintルール](https://github.com/textlint-ja/textlint-rule-morpheme-match)

## Changelog

See [Releases page](https://github.com/azu/morpheme-match/releases).

## Running tests

Install devDependencies and Run `npm test`:

npm i -d && npm test

## Contributing

Pull requests and stars are always welcome.
For bugs and feature requests, [please create an issue](https://github.com/azu/morpheme-match/issues).

1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request :D

## Author

- [github/azu](https://github.com/azu)
- [twitter/azu_re](http://twitter.com/azu_re)

## License

MIT © azu
51 changes: 51 additions & 0 deletions packages/morpheme-match-textlint/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "morpheme-match-textlint",
"version": "1.2.1",
"description": "morpheme-match for textlint rule.",
"keywords": [
"japanese",
"morpheme",
"text",
"形態素"
],
"homepage": "https://github.com/azu/morpheme-match",
"bugs": {
"url": "https://github.com/azu/morpheme-match/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/azu/morpheme-match.git"
},
"license": "MIT",
"author": "azu",
"files": [
"src/",
"lib/"
],
"main": "lib/morpheme-match-textlint.js",
"types": "lib/morpheme-match-textlint.d.ts",
"directories": {
"test": "test"
},
"scripts": {
"build": "cross-env NODE_ENV=production tsc -p .",
"build:website": "cd website && npm i && npm run build",
"prepublish": "npm run --if-present build",
"test": "mocha \"test/**/*.{js,ts}\"",
"watch": "tsc -p . --watch"
},
"dependencies": {
"morpheme-match-all": "^1.2.0"
},
"devDependencies": {
"@types/mocha": "^5.2.7",
"@types/node": "^12.0.7",
"cross-env": "^5.2.0",
"mocha": "^6.1.4",
"ts-node": "^8.2.0",
"ts-node-test-register": "^8.0.1",
"typescript": "^3.5.1",
"kuromojin": "^1.3.2"
},
"email": "[email protected]"
}
Loading

0 comments on commit 8f3be6a

Please sign in to comment.