Skip to content

Commit 869e270

Browse files
feat: add .parentCustomEntity() API to .tokens() API
1 parent c92d45b commit 869e270

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/doc-v2.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ var doc = function ( docData, wordVectors ) {
154154
api.parentDocument = () => methods;
155155
// Access the parent entity, **if any.**
156156
api.parentEntity = () => getParentItem( index, entities, itemEntity );
157+
// Access the parent cuustom entity, **if any.**
158+
api.parentCustomEntity = () => getParentItem( index, customEntities, itemCustomEntity );
157159
// Markup this token.
158160
api.markup = ( beginMarker, endMarker ) => markings.push( [ index, index, beginMarker, endMarker ] );
159161
// Output this token or its properties using mapper function — `f`.

test/custom-entities-specs.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,21 @@ describe( 'Detect from learned examples', function () {
205205
var d1 = nlp0.readDoc( 'I am a literal!' );
206206
expect( d1.customEntities().out( its.detail ) ).to.deep.equal( [] );
207207
} );
208+
209+
it( '.parentCustomEntity() must work correctly', function () {
210+
const config = { matchValue: false, usePOS: false, useEntity: true };
211+
const expectedEntities = [
212+
{ value: '[email protected]:-)', type: 'email-emoticon' },
213+
{ value: '^', type: 'caret' },
214+
{ value: 'thin', type: 'crust-type' },
215+
{ value: 'ADJ Parrot', type: 'escaped-adj-parrot' },
216+
{ value: '$5', type: 'entity-money' },
217+
{ value: '^ carrot', type: 'caret-carrot' }
218+
];
219+
nlp.learnCustomEntities( examples, config );
220+
var d1 = nlp.readDoc( s1 );
221+
expect( d1.tokens().itemAt( 0 ).parentCustomEntity().out( its.detail ) ).to.deep.equal( expectedEntities[ 0 ] );
222+
expect( d1.tokens().itemAt( 2 ).parentCustomEntity() ).to.deep.equal( undefined );
223+
expect( d1.tokens().itemAt( 22 ).parentCustomEntity().out( its.detail ) ).to.deep.equal( expectedEntities[ 5 ] );
224+
} );
208225
} );

0 commit comments

Comments
 (0)