Skip to content

Commit f7cb905

Browse files
feat(*): integrate revised mark approach ensuring backwards compatibility
also add test cases.
1 parent 65cdf3c commit f7cb905

File tree

3 files changed

+92
-8
lines changed

3 files changed

+92
-8
lines changed

src/automaton.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,17 +259,10 @@ var simpleFSM = function ( cache, token2Ignore ) {
259259
if ( terminalStates[ m0 ] === '0' ) return;
260260
// Not to be ignored — process it.
261261
var mark = markedStates[ m0 ];
262-
263262
var customProperty = customPropertyAtStates[ m0 ];
264263
if ( mark ) {
265-
// `match[ 1 ]` will now point to the index of the token where
266-
// the entity should end because `match[ 0 ]` is pointing to the index
267-
// of the token of detected entity's start and by adding `mark[ 1 ]`
268-
// i.e. the `lastIndex` to it, we get the required value.
269-
match[ 1 ] = match[ 0 ] + mark[ 1 ];
270-
// For `match[ 0 ]`, simply adding `mark[ 0 ]` i.e. `firstIndex` yields
271-
// the desried value.
272264
match[ 0 ] += mark[ 0 ];
265+
match[ 1 ] -= mark[ 1 ];
273266
}
274267

275268
// Removed `customProperty !== undefined &&` check while coding pos experiment

src/identify-marked-area.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ const identifyMarkedArea = function ( mark, length ) {
1111
lastIndex = Math.min( lastIndex, lm1 );
1212
if ( lastIndex < firstIndex ) lastIndex = lm1;
1313

14+
// The `lastIndex` manoeuvre is required to keep identical approach
15+
// being followed in `learnSinglePattern()` of automaton.js, where
16+
// the `firstIndex` **was** being added and the `lastIndex` **was** being
17+
// subtracted from the span of entity.
18+
lastIndex = length - lastIndex - 1;
1419
return [ firstIndex, lastIndex ];
1520
}; // identifyMarkedArea()
1621

test/identify-marked-area-specs.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// wink-nlp
2+
//
3+
// Copyright (C) GRAYPE Systems Private Limited
4+
//
5+
// This file is part of “wink-nlp”.
6+
//
7+
// Permission is hereby granted, free of charge, to any
8+
// person obtaining a copy of this software and
9+
// associated documentation files (the "Software"), to
10+
// deal in the Software without restriction, including
11+
// without limitation the rights to use, copy, modify,
12+
// merge, publish, distribute, sublicense, and/or sell
13+
// copies of the Software, and to permit persons to
14+
// whom the Software is furnished to do so, subject to
15+
// the following conditions:
16+
//
17+
// The above copyright notice and this permission notice
18+
// shall be included in all copies or substantial
19+
// portions of the Software.
20+
//
21+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
22+
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
23+
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
24+
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25+
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
26+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
27+
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28+
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29+
// DEALINGS IN THE SOFTWARE.
30+
31+
//
32+
33+
var chai = require( 'chai' );
34+
var mocha = require( 'mocha' );
35+
var ima = require( '../src/identify-marked-area.js' );
36+
37+
var expect = chai.expect;
38+
var describe = mocha.describe;
39+
var it = mocha.it;
40+
41+
describe( 'Identify marked area', function () {
42+
// Assume the entity length is 6 tokens and the area has
43+
// to be decided by different values of `mark`. Note `mark`'s
44+
// first & last indexes are 0 based when counting forward;
45+
// and when counting backwards, `-1` refers to last and `-2`
46+
// to last but one and so on.
47+
const length = 6;
48+
49+
it( 'Left edge to right edge should return the entire area', function () {
50+
// Counting forward.
51+
// Note: the values in expect's comments are without the `lastIndex` manoeuvre to
52+
// bring clarity in the test cases. Refer to the source file under test for details.
53+
expect( ima( [ 0, 5 ], length ) ).deep.equals( [ 0, 0 ] ); // [ 0, 5 ]
54+
// Counting backwards corresponding to the above.
55+
expect( ima( [ -6, -1 ], length ) ).deep.equals( [ 0, 0 ] ); // [ 0, 5 ]
56+
// Mix of backwards & forward.
57+
expect( ima( [ -6, 5 ], length ) ).deep.equals( [ 0, 0 ] ); // [ 0, 5 ]
58+
expect( ima( [ 0, -1 ], length ) ).deep.equals( [ 0, 0 ] ); // [ 0, 5 ]
59+
} );
60+
61+
it( 'Single values at left edge, middle(2nd) & right edge should yield [0, 0], [2, 2], & [5, 5]', function () {
62+
// Counting forward.
63+
expect( ima( [ 0, 0 ], length ) ).deep.equals( [ 0, 5 ] ); // [ 0, 0 ]
64+
expect( ima( [ 2, 2 ], length ) ).deep.equals( [ 2, 3 ] ); // [ 2, 2 ]
65+
expect( ima( [ 5, 5 ], length ) ).deep.equals( [ 5, 0 ] ); // [ 5, 5 ]
66+
// Counting backwards corresponding to the above.
67+
expect( ima( [ -6, -6 ], length ) ).deep.equals( [ 0, 5 ] ); // [ 0, 0 ]
68+
expect( ima( [ -4, -4 ], length ) ).deep.equals( [ 2, 3 ] ); // [ 2, 2 ]
69+
expect( ima( [ -1, -1 ], length ) ).deep.equals( [ 5, 0 ] ); // [ 5, 5 ]
70+
// Mix of backwards & forward.
71+
expect( ima( [ -6, 0 ], length ) ).deep.equals( [ 0, 5 ] ); // [ 0, 0 ]
72+
expect( ima( [ -4, 2 ], length ) ).deep.equals( [ 2, 3 ] ); // [ 2, 2 ]
73+
expect( ima( [ 2, -4 ], length ) ).deep.equals( [ 2, 3 ] ); // [ 2, 2 ]
74+
expect( ima( [ 5, -1 ], length ) ).deep.equals( [ 5, 0 ] ); // [ 5, 5 ]
75+
expect( ima( [ -1, 5 ], length ) ).deep.equals( [ 5, 0 ] ); // [ 5, 5 ]
76+
} );
77+
78+
it( 'Out of bound values should yield the entire area i.e. [0, 5]', function () {
79+
// Counting forward beyond limits
80+
expect( ima( [ 20, 500 ], length ) ).deep.equals( [ 0, 0 ] ); // [ 0, 5 ]
81+
// Going backwards beyond limits
82+
expect( ima( [ -10, 0 ], length ) ).deep.equals( [ 0, 5 ] ); // [ 0, 0 ]
83+
expect( ima( [ -10, -1 ], length ) ).deep.equals( [ 0, 0 ] ); // [ 0, 5 ]
84+
expect( ima( [ -10, -7 ], length ) ).deep.equals( [ 0, 0 ] ); // [ 0, 5 ]
85+
} );
86+
} );

0 commit comments

Comments
 (0)