natural 8.0.0 → 8.0.1
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
|
-
|
|
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,12 @@ function escapeRegExp (string) {
|
|
|
39
41
|
}
|
|
40
42
|
|
|
41
43
|
class SentenceTokenizer extends Tokenizer {
|
|
42
|
-
constructor (abbreviations
|
|
44
|
+
constructor (abbreviations) {
|
|
43
45
|
super()
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
this.sentenceDemarkers = sentenceDemarkers
|
|
46
|
+
if (abbreviations) {
|
|
47
|
+
this.abbreviations = abbreviations
|
|
47
48
|
} else {
|
|
48
|
-
this.
|
|
49
|
+
this.abbreviations = []
|
|
49
50
|
}
|
|
50
51
|
this.replacementMap = null
|
|
51
52
|
this.replacementCounter = 0
|
|
@@ -64,7 +65,10 @@ class SentenceTokenizer extends Tokenizer {
|
|
|
64
65
|
}
|
|
65
66
|
|
|
66
67
|
replaceAbbreviations (text) {
|
|
67
|
-
|
|
68
|
+
if (this.abbreviations.length === 0) {
|
|
69
|
+
return text
|
|
70
|
+
}
|
|
71
|
+
const pattern = new RegExp(`(${this.abbreviations.map(abbrev => escapeRegExp(abbrev)).join('|')})`, 'gi')
|
|
68
72
|
const replacedText = text.replace(pattern, match => {
|
|
69
73
|
const code = generateUniqueCode(ABBREV, this.replacementCounter++)
|
|
70
74
|
this.replacementMap.set(code, match)
|
|
@@ -77,12 +81,11 @@ class SentenceTokenizer extends Tokenizer {
|
|
|
77
81
|
replaceDelimitersWithPlaceholders (text) {
|
|
78
82
|
// Regular expression for sentence delimiters optionally followed by a bracket or quote
|
|
79
83
|
// Multiple delimiters with spaces in between are allowed
|
|
80
|
-
// The
|
|
81
|
-
const delimiterPattern = /(
|
|
82
|
-
|
|
83
|
-
const modifiedText = text.replace(delimiterPattern, (match, p1, p2) => {
|
|
84
|
+
// The expression makes sure that the sentence delimiter group ends with a sentence delimiter
|
|
85
|
+
const delimiterPattern = /([.?!… ]*)([.?!…])(["'”’)}\]]?)/g
|
|
86
|
+
const modifiedText = text.replace(delimiterPattern, (match, p1, p2, p3) => {
|
|
84
87
|
const placeholder = generateUniqueCode(DELIM, this.replacementCounter++)
|
|
85
|
-
this.delimiterMap.set(placeholder, p1 + p2)
|
|
88
|
+
this.delimiterMap.set(placeholder, p1 + p2 + p3)
|
|
86
89
|
return placeholder
|
|
87
90
|
})
|
|
88
91
|
|
|
@@ -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.
|
|
4
|
+
"version": "8.0.1",
|
|
5
5
|
"homepage": "https://github.com/NaturalNode/natural",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|