-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
101 lines (96 loc) · 2.97 KB
/
util.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
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var words = ["mention", "is", "either", "before", "or", "accept", "like", "answers", "[", "]", "on", "until", "it", "mentioned", "synonyms", "the", "do", "any", "kind", "of", "mention", "a"];
var chars = [".", "[", "]", ",", "(", ")", ";", '"'];
var subjects = ["Literature", "History", "Science", "Fine Arts", "Religion", "Mythology", "Philosophy", "Social Science", "Geography", "Current Events", "Trash"]
function similar(s1, s2) {
var longer = s1;
var shorter = s2;
if (s1.length < s2.length) {
longer = s2;
shorter = s1;
}
var longerLength = longer.length;
if (longerLength == 0) {
return 1.0;
}
return (longerLength - editDistance(longer, shorter)) / parseFloat(longerLength);
}
function percent(num, total){
return num*100/total;
}
function editDistance(s1, s2) {
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
var costs = new Array();
for (var i = 0; i <= s1.length; i++) {
var lastValue = i;
for (var j = 0; j <= s2.length; j++) {
if (i == 0)
costs[j] = j;
else {
if (j > 0) {
var newValue = costs[j - 1];
if (s1.charAt(i - 1) != s2.charAt(j - 1))
newValue = Math.min(Math.min(newValue, lastValue),
costs[j]) + 1;
costs[j - 1] = lastValue;
lastValue = newValue;
}
}
}
if (i > 0)
costs[s2.length] = lastValue;
}
return costs[s2.length];
}
function stripWords(answer) {
for (var i = chars.length - 1; i >= 0; i--) {
newanswer = answer;
do {
answer = newanswer;
newanswer = answer.replace(chars[i], " ");
}
while (newanswer != answer);
}
for (var i = words.length - 1; i >= 0; i--) {
answer = answer.split(" " + words[i] + " ").join(" ");
}
arr = answer.split(" ");
for (var i = arr.length - 1; i >= 0; i--) {
if (arr[i] == "") {
arr.splice(i, 1)
}
}
arr = arr.filter(function(value, index, array) {
return array.indexOf(value) == index;
});
return arr;
}
function check(input, answers) {
arr = stripWords(input.trim());
var temp;
for (var i = arr.length - 1; i >= 0; i--) {
for (var j = answers.length - 1; j >= 0; j--) {
if (similar(arr[i], answers[j]) > .70) {
temp = true;
}
}
}
if (temp) {
return true;
} else {
return false;
}
}
function sortByFrequency(array) {
var frequency = {};
array.forEach(function(value) { frequency[value] = 0; });
var uniques = array.filter(function(value) {
return ++frequency[value] == 1;
});
return uniques.sort(function(a, b) {
return frequency[b] - frequency[a];
});
}