natural 8.0.0 → 8.1.0

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.
@@ -43,6 +43,7 @@ export class TfIdf {
43
43
  constructor (deserialized?: Record<string, unknown>)
44
44
  idf (term: string, force?: boolean): number
45
45
  addDocument (document: string | string[] | Record<string, string>, key?: Record<string, any> | any, restoreCache?: boolean): void
46
+ removeDocument (key: any): boolean
46
47
  addFileSync (path: string, encoding?: string, key?: string, restoreCache?: boolean): void
47
48
  tfidf (terms: string | string[], d: number): number
48
49
  tfidfs (terms: string | string[], callback?: TfIdfCallback): number[]
@@ -31,7 +31,9 @@ const ABBREV = 'ABBREV'
31
31
  const DEBUG = false
32
32
 
33
33
  function generateUniqueCode (base, index) {
34
- return `${base}_${index}`
34
+ // Surround the placeholder with {{}} to prevent shorter numbers to be recognized
35
+ // in larger numbers
36
+ return `{{${base}_${index}}}`
35
37
  }
36
38
 
37
39
  function escapeRegExp (string) {
@@ -39,13 +41,17 @@ function escapeRegExp (string) {
39
41
  }
40
42
 
41
43
  class SentenceTokenizer extends Tokenizer {
42
- constructor (abbreviations, sentenceDemarkers) {
44
+ constructor (abbreviations, trimSentences) {
43
45
  super()
44
- this.abbreviations = abbreviations
45
- if (sentenceDemarkers) {
46
- this.sentenceDemarkers = sentenceDemarkers
46
+ if (abbreviations) {
47
+ this.abbreviations = abbreviations
47
48
  } else {
48
- this.sentenceDemarkers = ['.', '!', '?']
49
+ this.abbreviations = []
50
+ }
51
+ if (trimSentences === undefined) {
52
+ this.trimSentences = true
53
+ } else {
54
+ this.trimSentences = trimSentences
49
55
  }
50
56
  this.replacementMap = null
51
57
  this.replacementCounter = 0
@@ -64,7 +70,10 @@ class SentenceTokenizer extends Tokenizer {
64
70
  }
65
71
 
66
72
  replaceAbbreviations (text) {
67
- const pattern = new RegExp(`(${this.abbreviations.map(abbrev => escapeRegExp(abbrev)).join('|')})`, 'g')
73
+ if (this.abbreviations.length === 0) {
74
+ return text
75
+ }
76
+ const pattern = new RegExp(`(${this.abbreviations.map(abbrev => escapeRegExp(abbrev)).join('|')})`, 'gi')
68
77
  const replacedText = text.replace(pattern, match => {
69
78
  const code = generateUniqueCode(ABBREV, this.replacementCounter++)
70
79
  this.replacementMap.set(code, match)
@@ -77,12 +86,11 @@ class SentenceTokenizer extends Tokenizer {
77
86
  replaceDelimitersWithPlaceholders (text) {
78
87
  // Regular expression for sentence delimiters optionally followed by a bracket or quote
79
88
  // Multiple delimiters with spaces in between are allowed
80
- // The look ahead makes sure that there is punctuation symbol as next symbol
81
- const delimiterPattern = /(?=[.?!…])([.?!… ]+)(["'”’)}\]]?)/g
82
-
83
- const modifiedText = text.replace(delimiterPattern, (match, p1, p2) => {
89
+ // The expression makes sure that the sentence delimiter group ends with a sentence delimiter
90
+ const delimiterPattern = /([.?!… ]*)([.?!…])(["'”’)}\]]?)/g
91
+ const modifiedText = text.replace(delimiterPattern, (match, p1, p2, p3) => {
84
92
  const placeholder = generateUniqueCode(DELIM, this.replacementCounter++)
85
- this.delimiterMap.set(placeholder, p1 + p2)
93
+ this.delimiterMap.set(placeholder, p1 + p2 + p3)
86
94
  return placeholder
87
95
  })
88
96
 
@@ -178,7 +186,7 @@ class SentenceTokenizer extends Tokenizer {
178
186
  const trimmedSentences = this.trim(newSentences)
179
187
  DEBUG && console.log('Phase 7: trimming array of empty sentences: ' + JSON.stringify(trimmedSentences))
180
188
 
181
- const trimmedSentences2 = trimmedSentences.map(sent => sent.trim())
189
+ const trimmedSentences2 = trimmedSentences.map(sent => this.trimSentences ? sent.trim() : sent)
182
190
  DEBUG && console.log('Phase 8: trimming sentences from surrounding whitespace: ' + JSON.stringify(trimmedSentences2))
183
191
  DEBUG && console.log('---End of sentence tokenization--------------------------')
184
192
  DEBUG && console.log('---Replacement map---------------------------------------')
@@ -7,6 +7,8 @@ const knownAbbreviations = [
7
7
  'c/o',
8
8
  'dept.',
9
9
  'D.I.Y.',
10
+ 'Dr.',
11
+ 'e.g.',
10
12
  'est.',
11
13
  'E.T.A.',
12
14
  'Inc.',
@@ -23,6 +23,7 @@ THE SOFTWARE.
23
23
  'use strict'
24
24
 
25
25
  exports.stopwords = require('./stopwords').words
26
+ exports.abbreviations = require('./abbreviations_en').knownAbbreviations
26
27
  exports.ShortestPathTree = require('./shortest_path_tree')
27
28
  exports.LongestPathTree = require('./longest_path_tree')
28
29
  exports.DirectedEdge = require('./directed_edge')
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": "8.0.0",
4
+ "version": "8.1.0",
5
5
  "homepage": "https://github.com/NaturalNode/natural",
6
6
  "repository": {
7
7
  "type": "git",