Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace txt with json #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
'use strict'

const fs = require("fs");
const path = require('path');
const WORDS_JSON = require('./words-en.json');
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
Expand All @@ -19,21 +18,16 @@ class WordsNinja {
*/
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);
});
let words = WORDS_JSON;

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);
});
};

Expand Down Expand Up @@ -145,7 +139,7 @@ class WordsNinja {
* Camel Case Splitter
* Based on 'split-camelcase-to-words' package, https://www.npmjs.com/package/split-camelcase-to-words
* @private
* @param {string} inputString
* @param {string} inputString
* @return {string} String
*/
camelCaseSplitter(inputString) {
Expand Down
Loading