-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
178 lines (158 loc) · 5.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
'use strict'
const fs = require("fs");
const path = require('path');
const splitRegex = new RegExp("[^a-zA-Z0-9']+", "g");
const FILE_WORDS = path.join(__dirname, 'words-en.txt');
let maxWordLen = 0;
let wordCost = {};
let maxCost = 9e999;
/**
* WordsNinja, Split your string text without space to english words
* @class
*/
class WordsNinja {
/**
* Load Dictionary
* @return {Object} Objects of dictionary
*/
loadDictionary() {
return new Promise((resolve) => {
fs.readFile(FILE_WORDS, 'utf8', function (err, data) {
if (err)
throw err;
let words = data.split('\n');
words.forEach(function (word, index) {
wordCost[word] = Math.log((index + 1) * Math.log(words.length));
if (word.length > maxWordLen)
maxWordLen = word.length;
if (wordCost[word] < maxCost)
maxCost = wordCost[word];
});
resolve(wordCost);
});
});
};
/**
* @param {string} string - The string for split
* @param {object} options - The Options
* @param {boolean} options.camelCaseSplitter - Split by Camel Case, Default false (optional)
* @param {boolean} options.capitalizeFirstLetter - Capitalize First Letter, Default false (optional)
* @param {boolean} options.joinWords - Return join words as sentence, Default false (optional)
* @returns {Array|string} result - Split String
*/
splitSentence(string, { camelCaseSplitter, capitalizeFirstLetter, joinWords } = {}) {
let list = [];
let that = this;
camelCaseSplitter = camelCaseSplitter || false;
capitalizeFirstLetter = capitalizeFirstLetter || false;
joinWords = joinWords || false;
if (camelCaseSplitter)
string = this.camelCaseSplitter(string);
string.split(splitRegex).forEach(function (sub) {
that.splitWords(sub).forEach(function (word) {
word = capitalizeFirstLetter ? that.capitalizeFirstLetter(word) : word;
list.push(word);
})
})
if (joinWords)
return list.join(' ');
else
return list;
}
/**
* Add words to dictionary
* @param {Array|string} words Word(s) to add dictionary
* @return {void}
*/
addWords(words) {
if (Array.isArray(words)) {
for (let value of words)
this.addWords(value);
} else {
let word = words.toLocaleLowerCase();
wordCost[word] = maxCost;
if (word.length > maxWordLen)
maxWordLen = word.length;
}
}
/**
* Split Words
* @private
* @param {string} s Input String
* @return {Array} Splited Words
*/
splitWords(s) {
let cost = [0];
function best_match(i) {
let candidates = cost.slice(Math.max(0, i - maxWordLen), i).reverse();
let minPair = [Number.MAX_SAFE_INTEGER, 0];
candidates.forEach(function (c, k) {
let ccost;
if (wordCost[s.substring(i - k - 1, i).toLowerCase()]) {
ccost = c + wordCost[s.substring(i - k - 1, i).toLowerCase()];
} else {
ccost = Number.MAX_SAFE_INTEGER;
}
if (ccost < minPair[0]) {
minPair = [ccost, k + 1];
}
})
return minPair;
}
for (let i = 1; i < s.length + 1; i++) {
cost.push(best_match(i)[0]);
}
let out = [];
let i = s.length;
while (i > 0) {
let c = best_match(i)[0];
let k = best_match(i)[1];
//if (c == cost[i])
// console.log("Alert: " + c);
let newToken = true;
if (s.slice(i - k, i) != "'") {
if (out.length > 0) {
if (out[-1] == "'s" || (Number.isInteger(s[i - 1]) && Number.isInteger(out[-1][0]))) {
out[-1] = s.slice(i - k, i) + out[-1];
newToken = false;
}
}
}
if (newToken) {
out.push(s.slice(i - k, i))
}
i -= k
}
return out.reverse();
}
/**
* Camel Case Splitter
* Based on 'split-camelcase-to-words' package, https://www.npmjs.com/package/split-camelcase-to-words
* @private
* @param {string} inputString
* @return {string} String
*/
camelCaseSplitter(inputString) {
let notNullString = inputString || '';
let trimmedString = notNullString.trim();
let arrayOfStrings = trimmedString.split(' ');
let splitStringsArray = [];
arrayOfStrings.forEach(tempString => {
if (tempString != '') {
let splitWords = tempString.split(/(?=[A-Z])/).join(" ");
splitStringsArray.push(splitWords);
}
});
return splitStringsArray.join(" ");
}
/**
* Capitalize First Letter
* @private
* @param {string} string - String to Capitalize First Letter
* @return {string} result
*/
capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
}
module.exports = WordsNinja;