Skip to content

Commit 21e9053

Browse files
feat(*): add pipeConfig() in nlp to allow inquiry of active annotators
1 parent 04f2879 commit 21e9053

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

src/doc-v2.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,9 @@ var doc = function ( docData, addons ) {
456456

457457
methods.printTokens = () => printTokens( tokens, cache );
458458

459+
// Enusre that we make a deep copy of config before returning to avoid corruption!
460+
methods.pipeConfig = () => JSON.parse( JSON.stringify( docData.currPipe ) );
461+
459462
return methods;
460463
};
461464

src/wink-nlp.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,9 @@ var nlp = function ( theModel, pipe ) {
109109
// Used to innstantiate the compiler.
110110
var cerMetaModel;
111111

112-
// Annotation stuff.
112+
// Contains a list of valid annotations built from `theModel`.
113113
var validAnnotations = Object.create( null );
114-
validAnnotations.sbd = true;
115-
validAnnotations.negation = true;
116-
validAnnotations.sentiment = true;
117-
validAnnotations.pos = true;
118-
validAnnotations.ner = true;
119-
validAnnotations.cer = true;
114+
120115
// Current pipe.
121116
var currPipe = Object.create( null );
122117
var onlyTokenization = true;
@@ -231,6 +226,9 @@ var nlp = function ( theModel, pipe ) {
231226
// Markings are 4-tuples of `start`, `end` **token indexes**, and `begin & end markers`.
232227
// The begin & end markers are used to markup the tokens specified.
233228
rdd.markings = [];
229+
// Publish the current annotation pipeline so that code can inquire about
230+
// active annotations!
231+
rdd.currPipe = currPipe;
234232

235233
var wrappedDocData = DocDataWrapper( rdd ); // eslint-disable-line new-cap
236234

@@ -395,6 +393,15 @@ var nlp = function ( theModel, pipe ) {
395393
throw Error( 'wink-nlp: invalid model used.' );
396394
}
397395

396+
// Build a list of valid annotations from `theModel`. This will ensure that
397+
// only **available** annotations from the model can be used in the pipe.
398+
validAnnotations.sbd = typeof theModel.sbd === 'function';
399+
validAnnotations.negation = typeof theModel.negation === 'function';
400+
validAnnotations.sentiment = typeof theModel.sa === 'function';
401+
validAnnotations.pos = typeof theModel.pos === 'function';
402+
validAnnotations.ner = typeof theModel.ner === 'function';
403+
validAnnotations.cer = typeof theModel.metaCER === 'function';
404+
398405
const tempPipe = ( pipe === undefined ) ? Object.keys( validAnnotations ) : pipe;
399406
if ( helper.isArray( tempPipe ) ) {
400407
tempPipe.forEach( ( at ) => {

test/wink-nlp-specs.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ describe( 'wink-nlp test-coverage and basic behavior', function () {
155155
it( 'should throw error if readDoc is given non-text', function () {
156156
expect( nlp.readDoc.bind( 1 ) ).to.throw( /^wink-nlp: expecting a valid Javascript string/ );
157157
} );
158+
159+
it( 'pipeConfig should return sbd, negation, sa, pos, ner and cer as true', function () {
160+
expect( nlp.readDoc('Test').pipeConfig() ).to.deep.equal( { sbd: true, negation: true, pos: true, sentiment: true, ner: true, cer: true } );
161+
} );
158162
} );
159163

160164
describe( 'OOV handling', function () {
@@ -227,6 +231,7 @@ describe( 'Annotation pipe validity checks', function () {
227231

228232
describe( 'Empty Annotation pipe should not detect any of the existing annotations', function () {
229233
const nlpNoAnno = winkNLP( model, [] );
234+
230235
it( 'should still learn from patterns', function () {
231236
const patterns = [
232237
{ name: 'adjectiveNounPair', patterns: [ 'ADJ' ] }
@@ -235,6 +240,11 @@ describe( 'Empty Annotation pipe should not detect any of the existing annotatio
235240
} );
236241

237242
const doc = nlpNoAnno.readDoc( 'Hello World! Not happy! :-)' );
243+
244+
it( 'pipeConfig should return empty config', function () {
245+
expect( doc.pipeConfig() ).to.deep.equal( Object.create( null ) );
246+
} );
247+
238248
it( 'should return single sentence', function () {
239249
expect( doc.sentences().length() ).to.equal( 1 );
240250
expect( doc.sentences().out()[ 0 ] ).to.equal( doc.out() );

0 commit comments

Comments
 (0)