-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsolution.js
200 lines (146 loc) · 4.94 KB
/
solution.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Finalized version
var REGEXMACHINE = (function () {
'use strict';
console.log = function () {
};
var config = {};
var rX = {};
function _wrap(ar) {
return '([a-z]*)(' + ar.join('|') + ')$';
}
function _buildRegEx(w, ar, t) {
// ((?!(x|z|c)).i)
if ((ar && ar.length)) {
for (var i = 0; i < ar.length; i++) {
if (typeof (ar[i]) !== 'string') {
ar[i] = _buildRegEx(ar[i].s, ar[i].x);
}
}
return (t ? '(?!(' : '(?:(') + ar.join('|') + ')' + (t ? '.' : '') + w + ')';
}
return '';
}
function _buildInitPattern(o) {
var keys = [];
for (var k in o) {
keys.push(k);
}
return _wrap(keys);
}
function _createPatterns(c, id) {
rX[id] = {
suffixes: {}
};
var rex = '';
for (var i = 0; i < c.length; i++) {
rX[id].suffixes[c[i].s] = rX[id].suffixes[c[i].s] || {};
rX[id].suffixes[c[i].s].suffixes = rX[id].suffixes[c[i].s].suffixes || {};
rex += '(';
for (var z = 0; z < c[i].x.length; z++) {
rX[id].suffixes[c[i].s].suffixes[c[i].x[z].s] = '([a-z]*)' + _buildRegEx(c[i].x[z].s, c[i].x[z].x) +
(c[i].x[z].not ? ('|' + _buildRegEx(c[i].x[z].s, c[i].x[z].not, true)) : '') + '$';
}
}
rX[id].rex = _buildInitPattern(rX[id].suffixes);
if (Object.keys(rX[id].suffixes).length) {
for (var key in rX[id].suffixes) {
if (Object.keys(rX[id].suffixes[key].suffixes).length)
rX[id].suffixes[key].rex = _buildInitPattern(rX[id].suffixes[key].suffixes);
}
}
}
var _initialize = function init(data) {
// read config data made by bookworms
config = JSON.parse(data.toString());
for (var key in config.suffixes) {
_createPatterns(config.suffixes[key], key);
}
};
function _isAbrakadabra(str) {
var ratio = 0.5;
var clusters;
var r1 = new RegExp('(?:(?![aeiou])[a-z]){3,}', 'gm');
var r2 = new RegExp('(?:(' + config.clusters.join('|') + '))', 'g');
if (str.match(/^[a-z]\w+/)) {
clusters = str.match(r1);
if (clusters) {
for (var i = 0; i < clusters.length; i++) {
ratio = 0.5 * clusters[i].length;
clusters = clusters[i].match(r2);
if (!clusters) {
return true;
} else {
if (ratio > clusters.join('').length)
return true;
}
}
} else {
return false;
}
} else {
return true;
}
return false;
}
function _isGenetive(str) {
var res = str.split(/^([a-z])*('s)$/);
return (res && res[1]) ? true : false;
}
function _checkRules(str, word) {
var scope = '', match = [];
function _isInRuleset(str, rex) {
match = str.match(rex);
return match ? true : false;
}
str = word.isGenetiv ? str.substr(0, str.length - 2) : str;
for (var type in word.toTest) {
if (word.toTest[type]) {
scope = '';
match = word.toTest[type];
scope = match[2];
str = match[1];
if (_isInRuleset(str, rX[type].suffixes[match[2]].rex)) {
match = match[0].match(rX[type].suffixes[scope].suffixes[match[2]]);
if (match && match[0].length) {
if (!match[1].length) {
console.log('all rules are passed');
}
str = match[0];
scope = match[2];
} else {
console.log('ruleset not passed');
return false
}
} else {
console.log('not in ruleset');
}
}
}
return true;
}
function _soundsLike(s, r) {
return s.match(r);
}
var test = function main(str) {
if (_isAbrakadabra(str)) {
return false;
}
var word = {
isGenetive: _isGenetive(str),
toTest: {
adjective: _soundsLike(str, rX.adjective.rex),
adverb: _soundsLike(str, rX.adverb.rex),
noun: _soundsLike(str, rX.adverb.rex)
}
};
console.log('--------- ' + str);
console.log(word.toTest);
return _checkRules(str, word);
};
return {
initialize: _initialize,
test: test
};
})();
exports.init = REGEXMACHINE.initialize;
exports.test = REGEXMACHINE.test;