-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhypher.js
185 lines (160 loc) · 4.99 KB
/
hypher.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
179
180
181
182
183
184
185
/**
* @constructor
* @param {!{patterns: !Object, leftmin: !number, rightmin: !number}} language The language pattern file. Compatible with Hyphenator.js.
* @param {?Object=} options Options to alter Hypher's hyphenation behaviour.
*/
function Hypher(language) {
/**
* @type {!Hypher.TrieNode}
*/
this.trie = this.createTrie(language['patterns']);
/**
* @type {!number}
* @const
*/
this.leftMin = language['leftmin'];
/**
* @type {!number}
* @const
*/
this.rightMin = language['rightmin'];
/**
* @type {!Object.<string, !Array.<string>>}
*/
this.exceptions = {};
if (language['exceptions']) {
language['exceptions'].split(/,\s?/g).forEach(function (exception) {
var hyphenationMarker = new RegExp(exception.indexOf('=') !== -1 ? '=' : '-', 'g');
this.exceptions[exception.replace(hyphenationMarker, '')] = exception.split(hyphenationMarker);
}, this);
}
}
/**
* @typedef {{_points: !Array.<number>}}
*/
Hypher.TrieNode;
/**
* Creates a trie from a language pattern.
* @private
* @param {!Object} patternObject An object with language patterns.
* @return {!Hypher.TrieNode} An object trie.
*/
Hypher.prototype.createTrie = function (patternObject) {
var size = 0,
tree = {
_points: []
},
patterns;
for (size in patternObject) {
if (patternObject.hasOwnProperty(size)) {
patterns = patternObject[size].match(new RegExp('.{1,' + (+size) + '}', 'g'));
patterns.forEach(function (pattern) {
var chars = pattern.replace(/[0-9]/g, '').split(''),
points = pattern.split(/\D/),
t = tree;
chars.forEach(function (c) {
var codePoint = c.charCodeAt(0);
if (!t[codePoint]) {
t[codePoint] = {};
}
t = t[codePoint];
});
t._points = points.map(function (p) {
return p || 0;
});
});
}
}
return tree;
};
/**
* Hyphenates a text.
*
* @param {!string} str The text to hyphenate.
* @return {!string} The same text with soft hyphens inserted in the right positions.
*/
Hypher.prototype.hyphenateText = function (str, minLength) {
minLength = minLength || 4;
// Regexp("\b", "g") splits on word boundaries,
// compound separators and ZWNJ so we don't need
// any special cases for those characters.
var words = str.split(/\b/g);
return words.map(function (word, i) {
if (word.indexOf('/') !== -1) {
// Don't insert a zero width space if the slash is at the beginning or end
// of the text, or right after or before a space.
if (i === 0 || i === words.length -1 || /\s+\/|\/\s+/.test(word)) {
return word;
} else {
return word + '\u200B';
}
} else if (word.length <= minLength) {
return word;
} else {
return this.hyphenate(word).join('\u00AD');
}
}, this).join('');
};
/**
* Hyphenates a word.
*
* @param {!string} word The word to hyphenate
* @return {!Array.<!string>} An array of word fragments indicating valid hyphenation points.
*/
Hypher.prototype.hyphenate = function (word) {
var characters,
characterPoints = [],
originalCharacters,
i,
j,
k,
node,
points = [],
wordLength,
nodePoints,
nodePointsLength,
m = Math.max,
trie = this.trie,
result = [''];
if (this.exceptions.hasOwnProperty(word)) {
return this.exceptions[word];
}
if (word.indexOf('\u00AD') !== -1) {
return [word];
}
word = '_' + word + '_';
characters = word.toLowerCase().split('');
originalCharacters = word.split('');
wordLength = characters.length;
for (i = 0; i < wordLength; i += 1) {
points[i] = 0;
characterPoints[i] = characters[i].charCodeAt(0);
}
for (i = 0; i < wordLength; i += 1) {
node = trie;
for (j = i; j < wordLength; j += 1) {
node = node[characterPoints[j]];
if (node) {
nodePoints = node._points;
if (nodePoints) {
for (k = 0, nodePointsLength = nodePoints.length; k < nodePointsLength; k += 1) {
points[i + k] = m(points[i + k], nodePoints[k]);
}
}
} else {
break;
}
}
}
for (i = 1; i < wordLength - 1; i += 1) {
if (i > this.leftMin && i < (wordLength - this.rightMin) && points[i] % 2) {
result.push(originalCharacters[i]);
} else {
result[result.length - 1] += originalCharacters[i];
}
}
return result;
};
if(typeof(module)!=='undefined' && module.exports){
module.exports = Hypher;
}