Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/natural/tokenizers/regexp_tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class RegexpTokenizer extends Tokenizer {
super(opts)
const options = opts || {}
this._pattern = options.pattern || this._pattern
this.discardEmpty = options.discardEmpty || true
// Empty tokens are discarded unless the caller turns that off
this.discardEmpty = options.discardEmpty === undefined ? true : options.discardEmpty

// Match and split on GAPS not the actual WORDS
this._gaps = options.gaps
Expand Down
22 changes: 22 additions & 0 deletions spec/regexp_tokenizer_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

import { RegexpTokenizer } from 'lib/natural'

describe('RegexpTokenizer', function () {
describe('discardEmpty', function () {
it('should discard empty tokens by default', function () {
const tokenizer = new RegexpTokenizer({ pattern: /,/ })
expect(tokenizer.tokenize('a,,b,')).toEqual(['a', 'b'])
})

it('should discard empty tokens when discardEmpty is true', function () {
const tokenizer = new RegexpTokenizer({ pattern: /,/, discardEmpty: true })
expect(tokenizer.tokenize('a,,b,')).toEqual(['a', 'b'])
})

it('should keep empty tokens when discardEmpty is false', function () {
const tokenizer = new RegexpTokenizer({ pattern: /,/, discardEmpty: false })
expect(tokenizer.tokenize('a,,b,')).toEqual(['a', '', 'b', ''])
})
})
})
Loading