Skip to content

Commit

Permalink
Repaired sentence tokenizer for braces and quotes (issue #591) (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo-ter-Doest authored Mar 24, 2021
1 parent 2a8cac7 commit 007f455
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/natural/tokenizers/sentence_tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ util.inherits(SentenceTokenizer, Tokenizer)

SentenceTokenizer.prototype.tokenize = function (text) {
// break string up in to sentences based on punctation and quotation marks
let tokens = text.match(/(?<=\s+|^)["'‘“'"[({⟨](.*?[.?!])(\s[.?!])*["'’”'"\])}⟩](?=\s+|$)|(?<=\s+|^)\S(.*?[.?!])(\s[.?!])*(?=\s+|$)/g)
let tokens = text.match(/(?<=\s+|^)["'‘“'"[({⟨]?(.*?[.?!])(\s[.?!])*["'’”'"\])}⟩]?(?=\s+|$)|(?<=\s+|^)\S(.*?[.?!])(\s[.?!])*(?=\s+|$)/g)

DEBUG && console.log('SentenceTokenizer.tokenize: ' + tokens)

Expand Down
27 changes: 27 additions & 0 deletions spec/sentence_tokenizer_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,31 @@ describe('sentence_tokenizer', function () {
'Same Bat-Channel!'
])
})

it('should handle braces and quotes (issue #591)', function () {
expect(
tokenizer.tokenize('Teste. Test test. Test test: “Test.”')
).toEqual([
'Teste.',
'Test test.',
'Test test: “Test.”'
])
expect(
tokenizer.tokenize('Test Test. Test test, test test (test test) test: “Test.”')
).toEqual([
'Test Test.',
'Test test, test test (test test) test: “Test.”'
])
expect(
tokenizer.tokenize('Test Test. Test test, test (test) test (test test) test: “Test.”')
).toEqual([
'Test Test.',
'Test test, test (test) test (test test) test: “Test.”'
])
expect(
tokenizer.tokenize('Test: Test (test) test “Test.”')
).toEqual([
'Test: Test (test) test “Test.”'
])
})
})

0 comments on commit 007f455

Please sign in to comment.