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

Optimize Hypher setup and hyphenation #1

Open
wants to merge 3 commits 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
51 changes: 26 additions & 25 deletions lib/hypher.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ Hypher.TrieNode;
Hypher.prototype.createTrie = function (patternObject) {
var size = 0,
i = 0,
c = 0,
p = 0,
chars = null,
points = null,
codePoint = null,
t = null,
tree = {
_points: []
Expand All @@ -63,27 +58,35 @@ Hypher.prototype.createTrie = function (patternObject) {

for (size in patternObject) {
if (patternObject.hasOwnProperty(size)) {
patterns = patternObject[size].match(new RegExp('.{1,' + (+size) + '}', 'g'));

for (i = 0; i < patterns.length; i += 1) {
chars = patterns[i].replace(/[0-9]/g, '').split('');
points = patterns[i].split(/\D/);
size = +size;
for (i = 0; i < patternObject[size].length; i += size) {
t = tree;

for (c = 0; c < chars.length; c += 1) {
codePoint = chars[c].charCodeAt(0);
const points = [];
let prev = 0;

if (!t[codePoint]) {
t[codePoint] = {};
for (let j = 0; j < size; j++) {
const char = patternObject[size][i + j];
if (!char) {
break;
}
const codePoint = char.charCodeAt(0);
if (codePoint >= 48 && codePoint <= 57) {
points.push(+char);
} else {
if (prev < 48 || prev > 57) {
points.push(0);
}

if (!t[codePoint]) {
t[codePoint] = {};
}
t = t[codePoint];
}
t = t[codePoint];
prev = codePoint;
}

t._points = [];

for (p = 0; p < points.length; p += 1) {
t._points[p] = points[p] || 0;
}
t._points = points;
}
}
}
Expand Down Expand Up @@ -129,7 +132,6 @@ Hypher.prototype.hyphenateText = function (str, minLength) {
Hypher.prototype.hyphenate = function (word) {
var characters,
characterPoints = [],
originalCharacters,
i,
j,
k,
Expand All @@ -153,8 +155,7 @@ Hypher.prototype.hyphenate = function (word) {

word = '_' + word + '_';

characters = word.toLowerCase().split('');
originalCharacters = word.split('');
characters = word.toLowerCase();
wordLength = characters.length;

for (i = 0; i < wordLength; i += 1) {
Expand Down Expand Up @@ -182,9 +183,9 @@ Hypher.prototype.hyphenate = function (word) {

for (i = 1; i < wordLength - 1; i += 1) {
if (i > this.leftMin && i < (wordLength - this.rightMin) && points[i] % 2) {
result.push(originalCharacters[i]);
result.push(word[i]);
} else {
result[result.length - 1] += originalCharacters[i];
result[result.length - 1] += word[i];
}
}

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"author": "Bram Stein <[email protected]> (http://www.bramstein.com)",
"devDependencies": {
"benchmark": "=0.1.347",
"microtime": "^2.1.3",
"vows": ">=0.5.6"
},
"directories": {
Expand Down
2 changes: 2 additions & 0 deletions test/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ suite.add('Hypher', hypherDictionary, {
setup: hypherSetup
});

suite.add('Hypher (createTrie)', hypherSetup);

suite.add('Hyphenator', hyphenatorDictionary, {
setup: hyphenatorSetup
});
Expand Down
4 changes: 2 additions & 2 deletions test/hypher-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ vows.describe('Hypher').addBatch({
97: {
_points: [0, 1],
98: {
_points: [0, 2, 0]
_points: [0, 2]
}
},
98: {
_points: [0, 2],
99: {
_points: [0, 3, 0]
_points: [0, 3]
}
},
_points: []
Expand Down