Skip to content

Commit

Permalink
Merge pull request #2 from shirayu/skippable_key
Browse files Browse the repository at this point in the history
feat(src): Added a special key "_skippable"
  • Loading branch information
azu authored Dec 31, 2018
2 parents c071d29 + 7d3dcfd commit 81e9be5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,26 @@ const expectToken = createTokenMatcher([
]);
```

キー`_skippable``true`の場合はマッチしない場合は無視されます。


```js
const expectToken = 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)
Expand Down
24 changes: 15 additions & 9 deletions src/morpheme-match.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@ module.exports = function createTokenMatcher(matchedTokens) {
const tokenCount = matchedTokens.length;
const matchTokens = [];
return (token) => {
const expectedToken = matchedTokens[currentTokenPosition];
if (matchToken(token, expectedToken)) {
matchTokens.push(token);
currentTokenPosition += 1;
} else {
// reset position
matchTokens.length = 0;
currentTokenPosition = 0;
while (currentTokenPosition < tokenCount) {
const expectedToken = matchedTokens[currentTokenPosition];
if (matchToken(token, expectedToken)) {
matchTokens.push(token);
currentTokenPosition += 1;
break;
} else if (expectedToken["_skippable"]) {
currentTokenPosition += 1;
} else {
// reset position
matchTokens.length = 0;
currentTokenPosition = 0;
break;
}
}
// match all tokens
if (currentTokenPosition === tokenCount) {
Expand All @@ -50,4 +56,4 @@ module.exports = function createTokenMatcher(matchedTokens) {
match: false
};
}
};
};
31 changes: 25 additions & 6 deletions test/morpheme-match-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"use strict";
import expectTokenStream from "../src/morpheme-match";
const assert = require("power-assert");
describe("expectTokenStream", function () {
it("should return {match, tokens}", function () {
describe("expectTokenStream", function() {
it("should return {match, tokens}", function() {
// http://localhost:8080/#名詞(かも)しれない
const expectToken = expectTokenStream([
{
Expand All @@ -17,7 +17,26 @@ describe("expectTokenStream", function () {
"basic_form": "かも",
"reading": "カモ",
"pronunciation": "カモ"
}
},
{
"surface_form": "、",
"_skippable": true,
},
{
"surface_form": "、",
"_skippable": true,
},
{
"surface_form": "しれ",
},
{
"surface_form": "、",
"_skippable": true,
},
{
"surface_form": "、",
"_skippable": true,
},
]);
const tokens = [
{
Expand Down Expand Up @@ -73,13 +92,13 @@ describe("expectTokenStream", function () {
let resultTokens = [];
const result = tokens.some(token => {
const {match, tokens} = expectToken(token);
if(!match) {
if (!match) {
assert(tokens === undefined);
}
resultTokens = tokens;
return match;
});
assert(result);
assert.equal(resultTokens.length, 1);
assert.equal(resultTokens.length, 2);
});
});
});

0 comments on commit 81e9be5

Please sign in to comment.