wordsninja 1.0.0 → 1.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.
package/README.md CHANGED
@@ -1,5 +1,7 @@
1
- # WordsNinja 🐱‍👤
2
- Split a English sentence without any spaces nor accents, into words.
1
+ # WordsNinja
2
+ Split an English sentence that lacks spaces and accents into separate words.
3
+
4
+ [![https://nodei.co/npm/wordsninja.png?downloads=true&downloadRank=true&stars=true](https://nodei.co/npm/wordsninja.png?downloads=true&downloadRank=true&stars=true)](https://www.npmjs.com/package/wordsninja)
3
5
 
4
6
  ## Install
5
7
  ```bash
@@ -27,7 +29,7 @@ WordsNinja.addWords('new word');
27
29
  ```
28
30
  Parameters
29
31
 
30
- - `word`: The word (string|array)
32
+ - `word`: The word(s) (string|array)
31
33
 
32
34
 
33
35
 
@@ -39,10 +41,10 @@ let words = WordsNinja.splitSentence(string, {camelCaseSplitter, capitalizeFirst
39
41
 
40
42
  Parameters
41
43
  - `string`: The string for split
42
- - `options`
43
- - `camelCaseSplitter`: Split by Camel Case, Default is `false` (optional)
44
- - `capitalizeFirstLetter`: Capitalize First Letter, Default is `false` (optional)
45
- - `joinWords`: Return join words as sentence, Default is `false` (optional)
44
+ - `options`
45
+ - `camelCaseSplitter`: Split by Camel Case, Default is `false` (optional)
46
+ - `capitalizeFirstLetter`: Capitalize First Letter, Default is `false` (optional)
47
+ - `joinWords`: Return join words as sentence, Default is `false` (optional)
46
48
 
47
49
  Example
48
50
 
@@ -69,9 +71,9 @@ Result
69
71
  let string = 'youneedtolearnfromyourmistakes';
70
72
  let words = WordsNinja.splitSentence(string,
71
73
  {
72
- camelCaseSplitter: true, // Frist camel case spliting
74
+ camelCaseSplitter: true, // Camel case splitting
73
75
  capitalizeFirstLetter: true, // Capitalize first letter of result
74
- joinWords: true // Join words
76
+ joinWords: true // Join words with spaces
75
77
  }
76
78
  );
77
79
  console.log(words);
@@ -101,7 +103,7 @@ WordsNinja.addWords('Parsa');
101
103
  let words = WordsNinja.splitSentence(string,
102
104
  {
103
105
  capitalizeFirstLetter: true, // Capitalize first letter of result
104
- joinWords: true // Join words
106
+ joinWords: true // Join words with spaces
105
107
  }
106
108
  );
107
109
  console.log(words);
package/example.js ADDED
@@ -0,0 +1,15 @@
1
+ const WordsNinjaPack = require("./index.js");
2
+ const WordsNinja = new WordsNinjaPack();
3
+
4
+ (async () => {
5
+ await WordsNinja.loadDictionary();
6
+
7
+ let string = 'youneedtolearnfromyourmistakes';
8
+ let words = WordsNinja.splitSentence(string,
9
+ {
10
+ camelCaseSplitter: false, // Camel case splitting
11
+ capitalizeFirstLetter: false, // Capitalize first letter of result
12
+ joinWords: false // Join words with space
13
+ });
14
+ console.log(words);
15
+ })();
package/index.js CHANGED
@@ -1,9 +1,7 @@
1
1
  'use strict'
2
2
 
3
- const fs = require("fs");
4
- const path = require('path');
3
+ const jsonWords = require('./words-en.json');
5
4
  const splitRegex = new RegExp("[^a-zA-Z0-9']+", "g");
6
- const FILE_WORDS = path.join(__dirname, 'words-en.txt');
7
5
  let maxWordLen = 0;
8
6
  let wordCost = {};
9
7
  let maxCost = 9e999;
@@ -19,21 +17,16 @@ class WordsNinja {
19
17
  */
20
18
  loadDictionary() {
21
19
  return new Promise((resolve) => {
22
- fs.readFile(FILE_WORDS, 'utf8', function (err, data) {
23
- if (err)
24
- throw err;
25
-
26
- let words = data.split('\n');
27
-
28
- words.forEach(function (word, index) {
29
- wordCost[word] = Math.log((index + 1) * Math.log(words.length));
30
- if (word.length > maxWordLen)
31
- maxWordLen = word.length;
32
- if (wordCost[word] < maxCost)
33
- maxCost = wordCost[word];
34
- });
35
- resolve(wordCost);
20
+ let words = jsonWords;
21
+
22
+ words.forEach(function (word, index) {
23
+ wordCost[word] = Math.log((index + 1) * Math.log(words.length));
24
+ if (word.length > maxWordLen)
25
+ maxWordLen = word.length;
26
+ if (wordCost[word] < maxCost)
27
+ maxCost = wordCost[word];
36
28
  });
29
+ resolve(wordCost);
37
30
  });
38
31
  };
39
32
 
@@ -45,7 +38,7 @@ class WordsNinja {
45
38
  * @param {boolean} options.joinWords - Return join words as sentence, Default false (optional)
46
39
  * @returns {Array|string} result - Split String
47
40
  */
48
- splitSentence(string, { camelCaseSplitter, capitalizeFirstLetter, joinWords } = {}) {
41
+ splitSentence(string, {camelCaseSplitter, capitalizeFirstLetter, joinWords} = {}) {
49
42
  let list = [];
50
43
  let that = this;
51
44
  camelCaseSplitter = camelCaseSplitter || false;
@@ -60,10 +53,8 @@ class WordsNinja {
60
53
  list.push(word);
61
54
  })
62
55
  })
63
- if (joinWords)
64
- return list.join(' ');
65
- else
66
- return list;
56
+
57
+ return joinWords ? list.join(' ') : list;
67
58
  }
68
59
 
69
60
  /**
@@ -96,14 +87,14 @@ class WordsNinja {
96
87
  let candidates = cost.slice(Math.max(0, i - maxWordLen), i).reverse();
97
88
  let minPair = [Number.MAX_SAFE_INTEGER, 0];
98
89
  candidates.forEach(function (c, k) {
99
- let ccost;
90
+ let cCost;
100
91
  if (wordCost[s.substring(i - k - 1, i).toLowerCase()]) {
101
- ccost = c + wordCost[s.substring(i - k - 1, i).toLowerCase()];
92
+ cCost = c + wordCost[s.substring(i - k - 1, i).toLowerCase()];
102
93
  } else {
103
- ccost = Number.MAX_SAFE_INTEGER;
94
+ cCost = Number.MAX_SAFE_INTEGER;
104
95
  }
105
- if (ccost < minPair[0]) {
106
- minPair = [ccost, k + 1];
96
+ if (cCost < minPair[0]) {
97
+ minPair = [cCost, k + 1];
107
98
  }
108
99
  })
109
100
  return minPair;
@@ -118,13 +109,11 @@ class WordsNinja {
118
109
  while (i > 0) {
119
110
  let c = best_match(i)[0];
120
111
  let k = best_match(i)[1];
121
- //if (c == cost[i])
122
- // console.log("Alert: " + c);
123
112
 
124
113
  let newToken = true;
125
- if (s.slice(i - k, i) != "'") {
114
+ if (s.slice(i - k, i) !== "'") {
126
115
  if (out.length > 0) {
127
- if (out[-1] == "'s" || (Number.isInteger(s[i - 1]) && Number.isInteger(out[-1][0]))) {
116
+ if (out[-1] === "'s" || (Number.isInteger(s[i - 1]) && Number.isInteger(out[-1][0]))) {
128
117
  out[-1] = s.slice(i - k, i) + out[-1];
129
118
  newToken = false;
130
119
  }
@@ -145,7 +134,7 @@ class WordsNinja {
145
134
  * Camel Case Splitter
146
135
  * Based on 'split-camelcase-to-words' package, https://www.npmjs.com/package/split-camelcase-to-words
147
136
  * @private
148
- * @param {string} inputString
137
+ * @param {string} inputString
149
138
  * @return {string} String
150
139
  */
151
140
  camelCaseSplitter(inputString) {
@@ -155,7 +144,7 @@ class WordsNinja {
155
144
 
156
145
  let splitStringsArray = [];
157
146
  arrayOfStrings.forEach(tempString => {
158
- if (tempString != '') {
147
+ if (tempString !== '') {
159
148
  let splitWords = tempString.split(/(?=[A-Z])/).join(" ");
160
149
  splitStringsArray.push(splitWords);
161
150
  }
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "wordsninja",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Split a English sentence without any spaces nor accents, into words.",
5
5
  "main": "index.js",
6
- "author": "Parsa Kafi <parselearn@gmail.com> (http://parsa.ws)",
6
+ "author": "Parsa Kafi <parselearn@gmail.com> (https://parsa.ws)",
7
7
  "license": "MIT",
8
8
  "scripts": {},
9
9
  "keywords": [
@@ -20,10 +20,10 @@
20
20
  ],
21
21
  "repository": {
22
22
  "type": "git",
23
- "url": "https://github.com/parsakafi/wordsninja.git"
23
+ "url": "git+https://github.com/parsakafi/wordsninja.git"
24
24
  },
25
25
  "bugs": {
26
26
  "url": "https://github.com/parsakafi/wordsninja/issues"
27
27
  },
28
- "homepage": "https://github.com/parsakafi/wordsninja"
28
+ "homepage": "https://parsa.ws/projects/wordsninja-js-package"
29
29
  }