natural 6.9.2 → 6.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,7 +22,7 @@
22
22
 
23
23
  'use strict'
24
24
 
25
- const DEBUG = true
25
+ const DEBUG = false
26
26
 
27
27
  // Afinn
28
28
  const englishAfinnVoca = require('afinn-165')
@@ -23,22 +23,18 @@ THE SOFTWARE.
23
23
  'use strict'
24
24
 
25
25
  const Tokenizer = require('./tokenizer')
26
- const util = require('util')
27
26
 
28
- const AggressiveTokenizer = function () {
29
- Tokenizer.call(this)
27
+ class AggressiveTokenizer extends Tokenizer {
28
+ tokenize (text) {
29
+ // break a string up into an array of tokens by anything non-word
30
+ // underscore is considered to be non word character
31
+ // Old splitter:
32
+ // return this.trim(text.split(/[\W|_]+/))
33
+ // New splitter:
34
+ // Explicitly mentions which characters make up words.
35
+ // So words may contain hyphen -, single quote ' and slash /
36
+ return this.trim(text.split(/[^a-zA-Z0-9'\-/]+/))
37
+ }
30
38
  }
31
- util.inherits(AggressiveTokenizer, Tokenizer)
32
39
 
33
40
  module.exports = AggressiveTokenizer
34
-
35
- AggressiveTokenizer.prototype.tokenize = function (text) {
36
- // break a string up into an array of tokens by anything non-word
37
- // underscore is considered to be non word character
38
- // Old splitter:
39
- // return this.trim(text.split(/[\W|_]+/))
40
- // New splitter:
41
- // Explicitly mentions which characters make up words.
42
- // So words may contain hyphen -, single quote ' and slash /
43
- return this.trim(text.split(/[^a-zA-Z0-9'\-/]+/))
44
- }
@@ -23,16 +23,12 @@ THE SOFTWARE.
23
23
  'use strict'
24
24
 
25
25
  const Tokenizer = require('./tokenizer')
26
- const util = require('util')
27
26
 
28
- const AggressiveTokenizer = function () {
29
- Tokenizer.call(this)
27
+ class AggressiveTokenizer extends Tokenizer {
28
+ tokenize (text) {
29
+ // break a string up into an array of tokens by anything non-word
30
+ return this.trim(text.split(/[^a-zA-Z0-9ßäöü_'-]+/))
31
+ }
30
32
  }
31
- util.inherits(AggressiveTokenizer, Tokenizer)
32
33
 
33
34
  module.exports = AggressiveTokenizer
34
-
35
- AggressiveTokenizer.prototype.tokenize = function (text) {
36
- // break a string up into an array of tokens by anything non-word
37
- return this.trim(text.split(/[^a-zA-Z0-9_'-]+/))
38
- }
@@ -23,16 +23,12 @@ THE SOFTWARE.
23
23
  'use strict'
24
24
 
25
25
  const Tokenizer = require('./tokenizer')
26
- const util = require('util')
27
26
 
28
- const AggressiveTokenizer = function () {
29
- Tokenizer.call(this)
27
+ class AggressiveTokenizer extends Tokenizer {
28
+ tokenize (text) {
29
+ // break a string up into an array of tokens by anything non-word
30
+ return this.trim(text.split(/[^a-zA-Zá-úÁ-ÚñÑüÜ]+/))
31
+ }
30
32
  }
31
- util.inherits(AggressiveTokenizer, Tokenizer)
32
33
 
33
34
  module.exports = AggressiveTokenizer
34
-
35
- AggressiveTokenizer.prototype.tokenize = function (text) {
36
- // break a string up into an array of tokens by anything non-word
37
- return this.trim(text.split(/[^a-zA-Zá-úÁ-ÚñÑüÜ]+/))
38
- }
@@ -24,27 +24,23 @@ THE SOFTWARE.
24
24
  'use strict'
25
25
 
26
26
  const Tokenizer = require('./tokenizer')
27
- const util = require('util')
28
27
 
29
- const AggressiveTokenizer = function () {
30
- Tokenizer.call(this)
28
+ class AggressiveTokenizer extends Tokenizer {
29
+ clearEmptyString (array) {
30
+ return array.filter(function (a) {
31
+ return a !== ''
32
+ })
33
+ }
34
+
35
+ clearText (text) {
36
+ return text.replace(/.:\+-=\(\)"'!\?،,؛;/g, ' ')
37
+ }
38
+
39
+ tokenize (text) {
40
+ // break a string up into an array of tokens by anything non-word
41
+ text = this.clearText(text)
42
+ return this.clearEmptyString(text.split(/\s+/))
43
+ }
31
44
  }
32
- util.inherits(AggressiveTokenizer, Tokenizer)
33
45
 
34
46
  module.exports = AggressiveTokenizer
35
-
36
- AggressiveTokenizer.prototype.clearEmptyString = function (array) {
37
- return array.filter(function (a) {
38
- return a !== ''
39
- })
40
- }
41
-
42
- AggressiveTokenizer.prototype.clearText = function (text) {
43
- return text.replace(/.:\+-=\(\)"'!\?،,؛;/g, ' ')
44
- }
45
-
46
- AggressiveTokenizer.prototype.tokenize = function (text) {
47
- // break a string up into an array of tokens by anything non-word
48
- text = this.clearText(text)
49
- return this.clearEmptyString(text.split(/\s+/))
50
- }
@@ -23,16 +23,12 @@ THE SOFTWARE.
23
23
  'use strict'
24
24
 
25
25
  const Tokenizer = require('./tokenizer')
26
- const util = require('util')
27
26
 
28
- const AggressiveTokenizer = function () {
29
- Tokenizer.call(this)
27
+ class AggressiveTokenizer extends Tokenizer {
28
+ tokenize (text) {
29
+ // break a string up into an array of tokens by anything non-word
30
+ return this.trim(text.split(/[^a-z0-9äâàéèëêïîöôùüûœç-]+/i))
31
+ }
30
32
  }
31
- util.inherits(AggressiveTokenizer, Tokenizer)
32
33
 
33
34
  module.exports = AggressiveTokenizer
34
-
35
- AggressiveTokenizer.prototype.tokenize = function (text) {
36
- // break a string up into an array of tokens by anything non-word
37
- return this.trim(text.split(/[^a-z0-9äâàéèëêïîöôùüûœç-]+/i))
38
- }
@@ -28,13 +28,6 @@ DEALINGS IN THE SOFTWARE.
28
28
  'use strict'
29
29
 
30
30
  const Tokenizer = require('./tokenizer')
31
- const util = require('util')
32
-
33
- const AggressiveTokenizer = function () {
34
- Tokenizer.call(this)
35
- }
36
-
37
- util.inherits(AggressiveTokenizer, Tokenizer)
38
31
 
39
32
  /*
40
33
  To know more on hindi
@@ -44,9 +37,11 @@ Important links:
44
37
  3.https://www.unicode.org/charts/PDF/U0900.pdf
45
38
  */
46
39
 
47
- AggressiveTokenizer.prototype.tokenize = function (text) {
48
- const response = this.trim(text.replace(/[\u0964\u0965...?,]/g, '').split(/\s+|(?![\u0900-\u097F\u0020-\u007F])./u)).filter(Boolean)
49
- return response
40
+ class AggressiveTokenizer extends Tokenizer {
41
+ tokenize (text) {
42
+ const response = this.trim(text.replace(/[\u0964\u0965...?,]/g, '').split(/\s+|(?![\u0900-\u097F\u0020-\u007F])./u)).filter(Boolean)
43
+ return response
44
+ }
50
45
  }
51
46
 
52
47
  module.exports = AggressiveTokenizer
@@ -23,24 +23,20 @@ THE SOFTWARE.
23
23
  'use strict'
24
24
 
25
25
  const Tokenizer = require('./tokenizer')
26
- const util = require('util')
27
26
 
28
- const AggressiveTokenizer = function () {
29
- Tokenizer.call(this)
27
+ class AggressiveTokenizer extends Tokenizer {
28
+ // Remove all non alphanumeric characters except '-'
29
+ // Replace more than one space character to ' '
30
+ normalizeText (text) {
31
+ const result = text.replace(/[^a-z0-9 -]/g, ' ').replace(/( +)/g, ' ')
32
+ return result
33
+ }
34
+
35
+ tokenize (text) {
36
+ // break a string up into an array of tokens by space
37
+ text = this.normalizeText(text)
38
+ return this.trim(text.split(' '))
39
+ }
30
40
  }
31
- util.inherits(AggressiveTokenizer, Tokenizer)
32
41
 
33
42
  module.exports = AggressiveTokenizer
34
-
35
- // Remove all non alphanumeric characters except '-'
36
- // Replace more than one space character to ' '
37
- function normalizeText (text) {
38
- const result = text.replace(/[^a-z0-9 -]/g, ' ').replace(/( +)/g, ' ')
39
- return result
40
- }
41
-
42
- AggressiveTokenizer.prototype.tokenize = function (text) {
43
- // break a string up into an array of tokens by space
44
- text = normalizeText(text)
45
- return this.trim(text.split(' '))
46
- }
@@ -23,16 +23,12 @@ THE SOFTWARE.
23
23
  'use strict'
24
24
 
25
25
  const Tokenizer = require('./tokenizer')
26
- const util = require('util')
27
26
 
28
- const AggressiveTokenizer = function () {
29
- Tokenizer.call(this)
27
+ class AggressiveTokenizer extends Tokenizer {
28
+ tokenize (text) {
29
+ // break a string up into an array of tokens by anything non-word
30
+ return this.trim(text.split(/\W+/))
31
+ }
30
32
  }
31
- util.inherits(AggressiveTokenizer, Tokenizer)
32
33
 
33
34
  module.exports = AggressiveTokenizer
34
-
35
- AggressiveTokenizer.prototype.tokenize = function (text) {
36
- // break a string up into an array of tokens by anything non-word
37
- return this.trim(text.split(/\W+/))
38
- }
@@ -23,16 +23,12 @@ THE SOFTWARE.
23
23
  'use strict'
24
24
 
25
25
  const Tokenizer = require('./tokenizer')
26
- const util = require('util')
27
26
 
28
- const AggressiveTokenizer = function () {
29
- Tokenizer.call(this)
27
+ class AggressiveTokenizer extends Tokenizer {
28
+ tokenize (text) {
29
+ // break a string up into an array of tokens by anything non-word
30
+ return this.trim(text.split(/[^a-zA-Z0-9_'-]+/))
31
+ }
30
32
  }
31
- util.inherits(AggressiveTokenizer, Tokenizer)
32
33
 
33
34
  module.exports = AggressiveTokenizer
34
-
35
- AggressiveTokenizer.prototype.tokenize = function (text) {
36
- // break a string up into an array of tokens by anything non-word
37
- return this.trim(text.split(/[^a-zA-Z0-9_'-]+/))
38
- }
@@ -24,18 +24,14 @@ THE SOFTWARE.
24
24
 
25
25
  const Tokenizer = require('./tokenizer')
26
26
  const normalizer = require('../normalizers/normalizer_no')
27
- const util = require('util')
28
27
 
29
- const AggressiveTokenizer = function () {
30
- Tokenizer.call(this)
28
+ class AggressiveTokenizer extends Tokenizer {
29
+ tokenize (text) {
30
+ text = normalizer.removeDiacritics(text)
31
+
32
+ // break a string up into an array of tokens by anything non-word
33
+ return this.trim(text.split(/[^A-Za-z0-9_æøåÆØÅäÄöÖüÜ]+/))
34
+ }
31
35
  }
32
- util.inherits(AggressiveTokenizer, Tokenizer)
33
36
 
34
37
  module.exports = AggressiveTokenizer
35
-
36
- AggressiveTokenizer.prototype.tokenize = function (text) {
37
- text = normalizer.removeDiacritics(text)
38
-
39
- // break a string up into an array of tokens by anything non-word
40
- return this.trim(text.split(/[^A-Za-z0-9_æøåÆØÅäÄöÖüÜ]+/))
41
- }
@@ -23,25 +23,20 @@ THE SOFTWARE.
23
23
  'use strict'
24
24
 
25
25
  const Tokenizer = require('./tokenizer')
26
- const util = require('util')
27
26
 
28
- const AggressiveTokenizer = function () {
29
- Tokenizer.call(this)
30
- }
31
-
32
- util.inherits(AggressiveTokenizer, Tokenizer)
27
+ class AggressiveTokenizer extends Tokenizer {
28
+ withoutEmpty (array) {
29
+ return array.filter(function (a) { return a })
30
+ }
33
31
 
34
- module.exports = AggressiveTokenizer
32
+ clearText (text) {
33
+ return text.replace(/[^a-zążśźęćńół0-9]/gi, ' ').replace(/[\s\n]+/g, ' ').trim()
34
+ }
35
35
 
36
- AggressiveTokenizer.prototype.withoutEmpty = function (array) {
37
- return array.filter(function (a) { return a })
36
+ tokenize (text) {
37
+ // break a string up into an array of tokens by anything non-word
38
+ return this.withoutEmpty(this.clearText(text).split(' '))
39
+ }
38
40
  }
39
41
 
40
- AggressiveTokenizer.prototype.clearText = function (text) {
41
- return text.replace(/[^a-zążśźęćńół0-9]/gi, ' ').replace(/[\s\n]+/g, ' ').trim()
42
- }
43
-
44
- AggressiveTokenizer.prototype.tokenize = function (text) {
45
- // break a string up into an array of tokens by anything non-word
46
- return this.withoutEmpty(this.clearText(text).split(' '))
47
- }
42
+ module.exports = AggressiveTokenizer
@@ -23,20 +23,16 @@ THE SOFTWARE.
23
23
  'use strict'
24
24
 
25
25
  const Tokenizer = require('./tokenizer')
26
- const util = require('util')
27
26
 
28
- const AggressiveTokenizer = function () {
29
- Tokenizer.call(this)
30
- }
31
- util.inherits(AggressiveTokenizer, Tokenizer)
32
-
33
- module.exports = AggressiveTokenizer
27
+ class AggressiveTokenizer extends Tokenizer {
28
+ withoutEmpty (array) {
29
+ return array.filter(function (a) { return a })
30
+ }
34
31
 
35
- AggressiveTokenizer.prototype.withoutEmpty = function (array) {
36
- return array.filter(function (a) { return a })
32
+ tokenize (text) {
33
+ // break a string up into an array of tokens by anything non-word
34
+ return this.withoutEmpty(this.trim(text.split(/[^a-zA-Zà-úÀ-Ú]/)))
35
+ }
37
36
  }
38
37
 
39
- AggressiveTokenizer.prototype.tokenize = function (text) {
40
- // break a string up into an array of tokens by anything non-word
41
- return this.withoutEmpty(this.trim(text.split(/[^a-zA-Zà-úÀ-Ú]/)))
42
- }
38
+ module.exports = AggressiveTokenizer
@@ -23,25 +23,20 @@ THE SOFTWARE.
23
23
  'use strict'
24
24
 
25
25
  const Tokenizer = require('./tokenizer')
26
- const util = require('util')
27
26
 
28
- const AggressiveTokenizer = function () {
29
- Tokenizer.call(this)
30
- }
31
-
32
- util.inherits(AggressiveTokenizer, Tokenizer)
27
+ class AggressiveTokenizer extends Tokenizer {
28
+ withoutEmpty (array) {
29
+ return array.filter(function (a) { return a })
30
+ }
33
31
 
34
- module.exports = AggressiveTokenizer
32
+ clearText (text) {
33
+ return text.replace(/[^a-zа-яё0-9]/gi, ' ').replace(/[\s\n]+/g, ' ').trim()
34
+ }
35
35
 
36
- AggressiveTokenizer.prototype.withoutEmpty = function (array) {
37
- return array.filter(function (a) { return a })
36
+ tokenize (text) {
37
+ // break a string up into an array of tokens by anything non-word
38
+ return this.withoutEmpty(this.clearText(text).split(' '))
39
+ }
38
40
  }
39
41
 
40
- AggressiveTokenizer.prototype.clearText = function (text) {
41
- return text.replace(/[^a-zа-яё0-9]/gi, ' ').replace(/[\s\n]+/g, ' ').trim()
42
- }
43
-
44
- AggressiveTokenizer.prototype.tokenize = function (text) {
45
- // break a string up into an array of tokens by anything non-word
46
- return this.withoutEmpty(this.clearText(text).split(' '))
47
- }
42
+ module.exports = AggressiveTokenizer
@@ -24,19 +24,15 @@ THE SOFTWARE.
24
24
 
25
25
  const Tokenizer = require('./tokenizer')
26
26
  const normalizer = require('../normalizers/normalizer_sv')
27
- const util = require('util')
28
27
 
29
- const AggressiveTokenizer = function () {
30
- Tokenizer.call(this)
28
+ class AggressiveTokenizer extends Tokenizer {
29
+ tokenize (text) {
30
+ text = normalizer.removeDiacritics(text)
31
+
32
+ // break a string up into an array of tokens by anything non-word
33
+ // Ü is not part of swedish alphabet but there are words using it like müsli and München
34
+ return this.trim(text.split(/[^A-Za-z0-9_åÅäÄöÖüÜ-]+/))
35
+ }
31
36
  }
32
- util.inherits(AggressiveTokenizer, Tokenizer)
33
37
 
34
38
  module.exports = AggressiveTokenizer
35
-
36
- AggressiveTokenizer.prototype.tokenize = function (text) {
37
- text = normalizer.removeDiacritics(text)
38
-
39
- // break a string up into an array of tokens by anything non-word
40
- // Ü is not part of swedish alphabet but there are words using it like müsli and München
41
- return this.trim(text.split(/[^A-Za-z0-9_åÅäÄöÖüÜ-]+/))
42
- }
@@ -1,25 +1,43 @@
1
- 'use strict'
1
+ /*
2
+ Copyright (c) 2023, Pluto Rotegott
3
+ Tokenizer for Ukrainian
2
4
 
3
- const Tokenizer = require('./tokenizer')
4
- const util = require('util')
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
5
11
 
6
- const AggressiveTokenizer = function () {
7
- Tokenizer.call(this)
8
- }
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
9
14
 
10
- util.inherits(AggressiveTokenizer, Tokenizer)
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+ */
11
23
 
12
- module.exports = AggressiveTokenizer
24
+ 'use strict'
13
25
 
14
- AggressiveTokenizer.prototype.withoutEmpty = function (array) {
15
- return array.filter(function (a) { return a })
16
- }
26
+ const Tokenizer = require('./tokenizer')
17
27
 
18
- AggressiveTokenizer.prototype.clearText = function (text) {
19
- return text.replace(/[^a-zа-яґєії0-9]/gi, ' ').replace(/[\s\n]+/g, ' ').trim()
20
- }
28
+ class AggressiveTokenizer extends Tokenizer {
29
+ withoutEmpty (array) {
30
+ return array.filter(function (a) { return a })
31
+ }
32
+
33
+ clearText (text) {
34
+ return text.replace(/[^a-zа-яґєії0-9]/gi, ' ').replace(/[\s\n]+/g, ' ').trim()
35
+ }
21
36
 
22
- AggressiveTokenizer.prototype.tokenize = function (text) {
23
- // break a string up into an array of tokens by anything non-word
24
- return this.withoutEmpty(this.clearText(text).split(' '))
37
+ tokenize (text) {
38
+ // break a string up into an array of tokens by anything non-word
39
+ return this.withoutEmpty(this.clearText(text).split(' '))
40
+ }
25
41
  }
42
+
43
+ module.exports = AggressiveTokenizer
@@ -23,17 +23,12 @@ THE SOFTWARE.
23
23
  'use strict'
24
24
 
25
25
  const Tokenizer = require('./tokenizer')
26
- const util = require('util')
27
26
 
28
- const AggressiveTokenizer = function () {
29
- Tokenizer.call(this)
30
- }
31
-
32
- util.inherits(AggressiveTokenizer, Tokenizer)
33
-
34
- // break a string up into an array of tokens by anything non-word
35
- AggressiveTokenizer.prototype.tokenize = function (text) {
36
- return this.trim(text.split(/[^a-z0-9àáảãạăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵđ]+/i))
27
+ class AggressiveTokenizer extends Tokenizer {
28
+ // break a string up into an array of tokens by anything non-word
29
+ tokenize (text) {
30
+ return this.trim(text.split(/[^a-z0-9àáảãạăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵđ]+/i))
31
+ }
37
32
  }
38
33
 
39
34
  module.exports = AggressiveTokenizer
@@ -22,34 +22,35 @@ THE SOFTWARE.
22
22
 
23
23
  'use strict'
24
24
 
25
+ const DEBUG = false
26
+
25
27
  const Tokenizer = require('./tokenizer')
26
- const util = require('util')
27
28
  const _ = require('underscore')
28
29
 
29
- // Base Class for RegExp Matching
30
- const RegexpTokenizer = function (opts) {
31
- const options = opts || {}
32
- this._pattern = options.pattern || this._pattern
33
- this.discardEmpty = options.discardEmpty || true
30
+ class RegexpTokenizer extends Tokenizer {
31
+ constructor (opts) {
32
+ super(opts)
33
+ const options = opts || {}
34
+ this._pattern = options.pattern || this._pattern
35
+ this.discardEmpty = options.discardEmpty || true
34
36
 
35
- // Match and split on GAPS not the actual WORDS
36
- this._gaps = options.gaps
37
+ // Match and split on GAPS not the actual WORDS
38
+ this._gaps = options.gaps
37
39
 
38
- if (this._gaps === undefined) {
39
- this._gaps = true
40
+ if (this._gaps === undefined) {
41
+ this._gaps = true
42
+ }
40
43
  }
41
- }
42
-
43
- util.inherits(RegexpTokenizer, Tokenizer)
44
44
 
45
- RegexpTokenizer.prototype.tokenize = function (s) {
46
- let results
45
+ tokenize (s) {
46
+ let results
47
47
 
48
- if (this._gaps) {
49
- results = s.split(this._pattern)
50
- return (this.discardEmpty) ? _.without(results, '', ' ') : results
51
- } else {
52
- return s.match(this._pattern)
48
+ if (this._gaps) {
49
+ results = s.split(this._pattern)
50
+ return (this.discardEmpty) ? _.without(results, '', ' ') : results
51
+ } else {
52
+ return s.match(this._pattern)
53
+ }
53
54
  }
54
55
  }
55
56
 
@@ -57,21 +58,24 @@ exports.RegexpTokenizer = RegexpTokenizer
57
58
 
58
59
  const orthographyMatchers = require('./orthography_matchers')
59
60
 
60
- /***
61
- * A tokenizer that accepts an alphabet definition.
62
- * @param {string} options.language ISO 639-1 for the language, e.g. 'en'
63
- */
64
- const OrthographyTokenizer = function (options) {
65
- const pattern = orthographyMatchers[options.language]
66
- if (!pattern) {
67
- WordTokenizer.call(this, options)
68
- } else {
69
- this._pattern = pattern
70
- RegexpTokenizer.call(this, options)
61
+ class OrthographyTokenizer {
62
+ constructor (options) {
63
+ const pattern = orthographyMatchers[options.language]
64
+ DEBUG && console.log(pattern)
65
+
66
+ if (!pattern) {
67
+ this.tokenizer = new WordTokenizer()
68
+ } else {
69
+ this.tokenizer = new RegexpTokenizer(options)
70
+ this.tokenizer._pattern = pattern
71
+ DEBUG && console.log(this.tokenizer)
72
+ }
71
73
  }
72
- }
73
74
 
74
- util.inherits(OrthographyTokenizer, RegexpTokenizer)
75
+ tokenize (text) {
76
+ return this.tokenizer.tokenize(text)
77
+ }
78
+ }
75
79
 
76
80
  exports.OrthographyTokenizer = OrthographyTokenizer
77
81
 
@@ -83,12 +87,13 @@ exports.OrthographyTokenizer = OrthographyTokenizer
83
87
  * ['She', 'said', 'hello']
84
88
  *
85
89
  */
86
- const WordTokenizer = function (options) {
87
- this._pattern = /[^A-Za-zА-Яа-я0-9_]+/
88
- RegexpTokenizer.call(this, options)
90
+ class WordTokenizer extends RegexpTokenizer {
91
+ constructor (options) {
92
+ super(options)
93
+ this._pattern = /[^A-Za-zА-Яа-я0-9_]+/
94
+ }
89
95
  }
90
96
 
91
- util.inherits(WordTokenizer, RegexpTokenizer)
92
97
  exports.WordTokenizer = WordTokenizer
93
98
 
94
99
  /***
@@ -99,10 +104,14 @@ exports.WordTokenizer = WordTokenizer
99
104
  * ["She","said","'","hello","'","."]
100
105
  *
101
106
  */
102
- const WordPunctTokenizer = function (options) {
103
- this._pattern = /([A-Za-zÀ-ÿ-]+|[0-9._]+|.|!|\?|'|"|:|;|,|-)/i
104
- RegexpTokenizer.call(this, options)
107
+ class WordPunctTokenizer extends RegexpTokenizer {
108
+ constructor (options) {
109
+ if (!options) {
110
+ options = {}
111
+ }
112
+ options.pattern = /([A-Za-zÀ-ÿ-]+|[0-9._]+|.|!|\?|'|"|:|;|,|-)/i
113
+ super(options)
114
+ }
105
115
  }
106
116
 
107
- util.inherits(WordPunctTokenizer, RegexpTokenizer)
108
117
  exports.WordPunctTokenizer = WordPunctTokenizer
@@ -23,32 +23,29 @@ THE SOFTWARE.
23
23
  'use strict'
24
24
 
25
25
  const Tokenizer = require('./tokenizer')
26
- const util = require('util')
27
26
 
28
27
  const DEBUG = false
29
28
 
30
- const SentenceTokenizer = function () {
31
- Tokenizer.call(this)
32
- }
33
- util.inherits(SentenceTokenizer, Tokenizer)
34
-
35
- SentenceTokenizer.prototype.tokenize = function (text) {
36
- // break string up in to sentences based on punctation and quotation marks
37
- // let tokens = text.match(/(?<=\s+|^)["'‘“'"[({⟨]?.*?[.?!…](\s[.?!…])*["'’”'"\])}⟩]?(?=\s+|$)/g)
38
- let tokens = text.match(/(?<=\s+|^)["'‘“'"[({⟨]?(.*?[.?!…]|.+)(\s[.?!…])*["'’”'"\])}⟩]?(?=\s+|$)/g)
29
+ class SentenceTokenizer extends Tokenizer {
30
+ tokenize (text) {
31
+ // Break string up in to sentences based on punctation and quotation marks
32
+ // See https://gist.github.com/Hugo-ter-Doest/4ed21fb7eb5077814d998fa61a726566
33
+ // for a breakdown of the regular expression
34
+ let tokens = text.match(/(?<=\s+|^)["'‘“'"[({⟨]?(.*?[.?!…]|.+)(\s[.?!…])*["'’”'"\])}⟩]?(?=\s+|$)/g)
39
35
 
40
- DEBUG && console.log('SentenceTokenizer.tokenize: ' + tokens)
36
+ DEBUG && console.log('SentenceTokenizer.tokenize: ' + tokens)
41
37
 
42
- if (!tokens) {
43
- return [text]
44
- }
38
+ if (!tokens) {
39
+ return [text]
40
+ }
45
41
 
46
- // remove unecessary white space
47
- tokens = tokens.map(Function.prototype.call, String.prototype.trim)
42
+ // remove unecessary white space
43
+ tokens = tokens.map(Function.prototype.call, String.prototype.trim)
48
44
 
49
- DEBUG && console.log('SentenceTokenizer.tokenize: tokens after removing whitespace ' + tokens)
45
+ DEBUG && console.log('SentenceTokenizer.tokenize: tokens after removing whitespace ' + tokens)
50
46
 
51
- return this.trim(tokens)
47
+ return this.trim(tokens)
48
+ }
52
49
  }
53
50
 
54
51
  module.exports = SentenceTokenizer
@@ -22,16 +22,11 @@ THE SOFTWARE.
22
22
  const parser = require('./parser_sentence_tokenizer')
23
23
 
24
24
  const Tokenizer = require('./tokenizer')
25
- const util = require('util')
26
25
 
27
- const SentenceTokenizer = function () {
28
- Tokenizer.call(this)
29
- }
30
-
31
- util.inherits(SentenceTokenizer, Tokenizer)
32
-
33
- SentenceTokenizer.prototype.tokenize = function (text) {
34
- return (parser.parse(text))
26
+ class SentenceTokenizer extends Tokenizer {
27
+ tokenize (text) {
28
+ return (parser.parse(text))
29
+ }
35
30
  }
36
31
 
37
32
  module.exports = SentenceTokenizer
@@ -20,35 +20,16 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
20
  THE SOFTWARE.
21
21
  */
22
22
 
23
- /**
24
- * \@todo Use .bind() in Tokenizer.prototype.attach().
25
- */
26
-
27
23
  'use strict'
28
24
 
29
- const Tokenizer = function () {
30
- }
31
-
32
- Tokenizer.prototype.trim = function (array) {
33
- while (array[array.length - 1] === '') { array.pop() }
25
+ class Tokenizer {
26
+ trim (array) {
27
+ while (array[array.length - 1] === '') { array.pop() }
34
28
 
35
- while (array[0] === '') { array.shift() }
36
-
37
- return array
38
- }
29
+ while (array[0] === '') { array.shift() }
39
30
 
40
- // Expose an attach function that will patch String with new methods.
41
- // Changing the prototype of a native type is bad practice
42
- /*
43
- Tokenizer.prototype.attach = function () {
44
- const self = this
45
-
46
- String.prototype.tokenize = function () {
47
- return self.tokenize(this)
31
+ return array
48
32
  }
49
33
  }
50
- */
51
-
52
- Tokenizer.prototype.tokenize = function () {}
53
34
 
54
35
  module.exports = Tokenizer
@@ -21,41 +21,26 @@
21
21
  */
22
22
 
23
23
  const Tokenizer = require('./tokenizer')
24
- const util = require('util')
25
- const CaseTokenizer = function () {
26
- Tokenizer.call(this)
27
- }
28
-
29
- util.inherits(CaseTokenizer, Tokenizer)
30
-
31
- // Changing the prototype of a native type is bad practice
32
- /*
33
- CaseTokenizer.prototype.attach = function () {
34
- const self = this
35
-
36
- String.prototype.tokenize = function (preserveApostrophe) {
37
- return self.tokenize(this, preserveApostrophe)
38
- }
39
- }
40
- */
41
24
 
42
25
  // Idea from Seagull: http://stackoverflow.com/a/26482650
43
- CaseTokenizer.prototype.tokenize = function (text, preserveApostrophe) {
44
- const whitelist = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
45
- const lower = text.toLowerCase()
46
- const upper = text.toUpperCase()
47
- let result = ''
48
- let i
49
-
50
- for (i = 0; i < lower.length; ++i) {
51
- if (lower[i] !== upper[i] || whitelist.indexOf(lower[i]) > -1 || (text[i] === '\'' && preserveApostrophe)) {
52
- result += text[i]
53
- } else {
54
- result += ' '
26
+ class CaseTokenizer extends Tokenizer {
27
+ tokenize (text, preserveApostrophe) {
28
+ const whitelist = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
29
+ const lower = text.toLowerCase()
30
+ const upper = text.toUpperCase()
31
+ let result = ''
32
+ let i
33
+
34
+ for (i = 0; i < lower.length; ++i) {
35
+ if (lower[i] !== upper[i] || whitelist.indexOf(lower[i]) > -1 || (text[i] === '\'' && preserveApostrophe)) {
36
+ result += text[i]
37
+ } else {
38
+ result += ' '
39
+ }
55
40
  }
56
- }
57
41
 
58
- return this.trim(result.replace(/\s+/g, ' ').split(' '))
42
+ return this.trim(result.replace(/\s+/g, ' ').split(' '))
43
+ }
59
44
  }
60
45
 
61
46
  module.exports = CaseTokenizer
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "natural",
3
3
  "description": "General natural language (tokenizing, stemming (English, Russian, Spanish), part-of-speech tagging, sentiment analysis, classification, inflection, phonetics, tfidf, WordNet, jaro-winkler, Levenshtein distance, Dice's Coefficient) facilities for node.",
4
- "version": "6.9.2",
4
+ "version": "6.10.2",
5
5
  "homepage": "https://github.com/NaturalNode/natural",
6
6
  "repository": {
7
7
  "type": "git",
@@ -33,7 +33,7 @@
33
33
  "proxyquire": "^1.8.0",
34
34
  "rimraf": "^2.6.3",
35
35
  "sinon": "^1.12.2",
36
- "standard": "^16.0.3",
36
+ "standard": "^16.0.4",
37
37
  "ts-standard": "^12.0.2",
38
38
  "typescript": "^4.9.3",
39
39
  "uubench": "^0.0.1",