forked from hola/challenge_word_classifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
1 lines (1 loc) · 1.24 KB
/
solution.js
1
"use strict";function Bloom(t){var e;if(typeof t!=="number"){e=t;t=e.length*8}var r=Math.ceil(t/8);this.m=r*8;var n=this.buckets=new Int8Array(r);if(e){for(var i=0;i<r;++i){n[i]=e[i]}}}Bloom.prototype.add=function(t){var e=this.index(t);var r=this.buckets;r[Math.floor(e/8)]|=1<<e%8};Bloom.prototype.test=function(t){var e=this.index(t);var r=this.buckets;if(!(r[Math.floor(e/8)]&1<<e%8)){return false}return true};Bloom.prototype.index=function(t){var e=hash(t)%this.m;return e<0?e+this.m:e};function hash(t){var e=123;for(var r=0,n=t.length;r<n;++r){e=(e<<5)+e+t.charCodeAt(r)}return e}var MAX_WORD_SIZE=15;var MAX_WORD_SIZE_BLOOM=8;var bloom;function init(t){bloom=new Bloom(t)}function test(t){return heuristic(t)&&bloom.test(stem(t))}function heuristic(t){var e=t.match(/[jqxz]/g);return!(t.length>MAX_WORD_SIZE||t.match(/'(?!s$)/)||t.match(/[bcdfghjklmnpqrstvwxz]{5,}/)||t.match(/[bcdfgjpqvwxz]{3,}/)||t.length>3&&!t.match(/[aeiouy]/)||e&&e.length>2)}function stem(t){return t.replace(/(s?')?s$/,"").replace(/^non/,"").replace(/^(un|de)(.+)(able|ability|ing|ingly|ingness|ed)$/,"$1").replace(/(able|ability|ing|ingly|ed|er|ment)$/,"^").replace(/(ion|ive|ively|ivity)$/,"%").replace(/ful(ly)?$/,"").substr(0,MAX_WORD_SIZE_BLOOM)}exports.init=init;exports.test=test;