Skip to content

Commit f2742b0

Browse files
shirayuazu
authored andcommitted
fix: Added information whether a token is skipped or not (#3)
1 parent 52a5529 commit f2742b0

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Install with [npm](https://www.npmjs.com/):
2020

2121
### createTokenMatcher(expectedTokens): function
2222

23-
`createTokenMatcher()` return `function(token): { match: boolean, tokens?: Array }`.
23+
`createTokenMatcher()` return `function(token): { match: boolean, tokens?: Array, skipped? Array }`.
2424

2525
We want to check "名詞かもしれない" contain "かも" token.
2626
Write following:
@@ -107,7 +107,7 @@ If want to get matched token, write following:
107107
```js
108108
let resultTokens = [];
109109
const result = tokens.some(token => {
110-
const {match, tokens} = expectToken(token);
110+
const {match, tokens, skipped} = expectToken(token);
111111
resultTokens = tokens;
112112
return match;
113113
});

src/morpheme-match.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,38 @@ module.exports = function createTokenMatcher(matchedTokens) {
2525
let currentTokenPosition = 0;
2626
const tokenCount = matchedTokens.length;
2727
const matchTokens = [];
28-
return (token) => {
28+
const matchSkipped = [];
29+
return (token, index) => {
2930
while (currentTokenPosition < tokenCount) {
3031
const expectedToken = matchedTokens[currentTokenPosition];
3132
if (matchToken(token, expectedToken)) {
3233
matchTokens.push(token);
34+
matchSkipped.push(false);
3335
currentTokenPosition += 1;
3436
break;
3537
} else if (expectedToken["_skippable"]) {
3638
currentTokenPosition += 1;
39+
matchSkipped.push(true);
3740
} else {
3841
// reset position
3942
matchTokens.length = 0;
43+
matchSkipped.length = 0;
4044
currentTokenPosition = 0;
4145
break;
4246
}
4347
}
4448
// match all tokens
4549
if (currentTokenPosition === tokenCount) {
4650
const tokens = matchTokens.slice();
51+
const skipped = matchSkipped.slice();
4752
// match -> reset
4853
currentTokenPosition = 0;
4954
matchTokens.length = 0;
55+
matchSkipped.length = 0;
5056
return {
5157
match: true,
52-
tokens: tokens
58+
tokens: tokens,
59+
skipped: skipped,
5360
};
5461
}
5562
return {

test/morpheme-match-test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import expectTokenStream from "../src/morpheme-match";
44
const assert = require("power-assert");
55
describe("expectTokenStream", function() {
6-
it("should return {match, tokens}", function() {
6+
it("should return {match, tokens, skipped}", function() {
77
// http://localhost:8080/#名詞(かも)しれない
88
const expectToken = expectTokenStream([
99
{
@@ -28,6 +28,7 @@ describe("expectTokenStream", function() {
2828
},
2929
{
3030
"surface_form": "しれ",
31+
"_skippable": true,
3132
},
3233
{
3334
"surface_form": "、",
@@ -90,15 +91,18 @@ describe("expectTokenStream", function() {
9091
}
9192
];
9293
let resultTokens = [];
94+
let resultSkipped = [];
9395
const result = tokens.some(token => {
94-
const {match, tokens} = expectToken(token);
96+
const {match, tokens, skipped} = expectToken(token);
9597
if (!match) {
9698
assert(tokens === undefined);
9799
}
98100
resultTokens = tokens;
101+
resultSkipped = skipped;
99102
return match;
100103
});
101104
assert(result);
102105
assert.equal(resultTokens.length, 2);
106+
assert.deepEqual(resultSkipped, [false, true, true, false, true, true]);
103107
});
104108
});

0 commit comments

Comments
 (0)