Skip to content
Merged
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
9 changes: 7 additions & 2 deletions lib/natural/tokenizers/sentence_tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,18 @@ function escapeRegExp (string) {
}

class SentenceTokenizer extends Tokenizer {
constructor (abbreviations) {
constructor (abbreviations, trimSentences) {
super()
if (abbreviations) {
this.abbreviations = abbreviations
} else {
this.abbreviations = []
}
if (trimSentences === undefined) {
this.trimSentences = true
} else {
this.trimSentences = trimSentences
}
this.replacementMap = null
this.replacementCounter = 0
}
Expand Down Expand Up @@ -181,7 +186,7 @@ class SentenceTokenizer extends Tokenizer {
const trimmedSentences = this.trim(newSentences)
DEBUG && console.log('Phase 7: trimming array of empty sentences: ' + JSON.stringify(trimmedSentences))

const trimmedSentences2 = trimmedSentences.map(sent => sent.trim())
const trimmedSentences2 = trimmedSentences.map(sent => this.trimSentences ? sent.trim() : sent)
DEBUG && console.log('Phase 8: trimming sentences from surrounding whitespace: ' + JSON.stringify(trimmedSentences2))
DEBUG && console.log('---End of sentence tokenization--------------------------')
DEBUG && console.log('---Replacement map---------------------------------------')
Expand Down
13 changes: 13 additions & 0 deletions spec/sentence_tokenizer_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,16 @@ describe('sentence_tokenizer', function () {
})
})
})

// describe('sentence_tokenizer with trimSentences set to false', function () {
// let tokenizer: Tokenizer

// beforeAll(function () {
// tokenizer = new Tokenizer(['i.e.', 'etc.', 'vs.', 'Inc.', 'A.S.A.P.'],
// ['.', '!', '?', '\n', '\r', '...', '…'], false)
// })

// it('should tokenize strings but not trim whitespace if trimSentences is false', function () {
// expect(tokenizer.tokenize('This is a sentence. This is another sentence.')).toEqual(['This is a sentence. ', 'This is another sentence.'])
// })
// })