-
Notifications
You must be signed in to change notification settings - Fork 6
/
gibberish.js
114 lines (92 loc) · 2.99 KB
/
gibberish.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
(function (exports) {
'use strict';
//This function divides the string in chunks
function divideByChunks(string) {
var charArray = [];
while (charArray.length < (string.length / 35)) {
charArray.push(string.substring(0, 35));
string = string.substring(36);
}
if (charArray.length >= 1 && charArray[charArray.length - 1].length < 10) {
charArray[charArray.length - 2] += charArray[charArray.length - 1];
charArray.pop();
}
return charArray;
};
function getUniqueCharsInChunk(string) {
return getUnique(string).length;
}
function getUniqueCharsInArray(array) {
var result = [];
for (var i = 0; i < array.length; i++) {
result.push(getUniqueCharsInChunk(array[i]));
}
return result * 100;
}
function getAverage(a) {
var sum = 0;
for (var i = 0; i < a.length; i++) {
sum += parseFloat(a[i], 10);
}
return sum / a.length;
}
function getVowelFrequency(string) {
var vowelFreq = 0;
var normalFreq = 0;
for (var i = 0; i < string.length; i++) {
var character = string.charAt(i);
if (!character.match(/^[a-zA-Z]+$/))
continue;
if (character.match(/^(a|e|i|o|u)$/i))
vowelFreq++;
normalFreq++;
}
if (normalFreq !== 0)
return vowelFreq / normalFreq * 100;
else
return 0;
};
function getWordToCharRatio(string) {
var wordArray = string.split(/[\W_]/);
return wordArray.length / string.length * 100;
}
function getDeviationScore(percentage, lowerBound, upperBound) {
if (percentage < lowerBound)
return getBaseLog(lowerBound - percentage, lowerBound) * 100;
else if (percentage > upperBound)
return getBaseLog(percentage - upperBound, 100 - upperBound) * 100;
else
return 0;
}
//Utility Functions
function getUnique(s) {
var chars = {},
rv = '';
for (var i = 0; i < s.length; ++i) {
if (!(s[i] in chars)) {
chars[s[i]] = 1;
rv += s[i];
}
}
return rv;
};
//Thanks to Jano González from SE for this
function verifyEmptyness(str) {
return (str.length === 0 || !str.trim());
};
function getBaseLog(x, y) {
return Math.log(y) / Math.log(x);
};
exports.detect = function (string) {
if (verifyEmptyness(string))
return 0;
var chunks = divideByChunks(string);
var uniqueCharsInArray = getAverage(getUniqueCharsInArray(chunks));
var vowelFrequency = getVowelFrequency(string);
var wordToCharRatio = getWordToCharRatio(string);
var uniqueCharsInArrayDev = Math.max(1, getDeviationScore(uniqueCharsInArray, 45, 50));
var vowelFrequencyDev = Math.max(1, getDeviationScore(vowelFrequency, 35, 45));
var wordToCharRatioDev = Math.max(1, getDeviationScore(wordToCharRatio, 15, 20));
return Math.max(1, (Math.log10(uniqueCharsInArrayDev) + Math.log10(vowelFrequencyDev) + Math.log10(wordToCharRatioDev)) / 6 * 100);
};
})(typeof exports === 'undefined' ? this['gibberish'] = {} : exports);