-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsript.js
171 lines (150 loc) · 3.54 KB
/
sript.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
const conjunctions = [
" and ",
" but ",
" or ",
" yet ",
" so ",
" for ",
" nor ",
" after ",
" although ",
" as ",
" because ",
" before ",
" if ",
" since ",
" though ",
" unless ",
" until ",
" when ",
" whereas ",
" while ",
" to ",
" will ",
" be ",
" the ",
" you ",
" he ",
" some ",
" other ",
" she ",
" it ",
" they ",
" we ",
" I ",
" me ",
" him ",
" her ",
" them ",
" us ",
" his ",
" her ",
" their ",
" our ",
" my ",
" your ",
" a ",
" an ",
" this ",
" that ",
" these ",
" those ",
" there ",
" is ",
" are ",
" was ",
" were ",
" have ",
" has ",
" had ",
" in ",
" you ",
" can ",
" might ",
" could ",
" also ",
" i ",
" do ",
" & ",
" i ",
" from ",
" of ",
];
function check() {
var totalElem;
table = document.getElementById("freq_table");
var sentence = document.getElementById("input-area").value;
totalElem = sentence.trim().split(/\W+/).length;
sentence = sentence.toLowerCase();
for (let index = 0; index < conjunctions.length; index++) {
sentence = sentence.replace(new RegExp(conjunctions[index], "g"), " ");
sentence = sentence.replace(" ", " ");
sentence = sentence.replace(".", "");
sentence = sentence.replace("\n", " ");
sentence = sentence.replace("\n\n", " ");
sentence = sentence.replace("?", " ");
}
sortedFrequency = frequency(sentence);
var percent = calcPercent(sortedFrequency, totalElem);
drawTable(percent, sortedFrequency);
}
function calcPercent(sortedFrequency, totalElem) {
var percent = {};
for (let index = 0; index < sortedFrequency.length; index++) {
percent[index] = ((sortedFrequency[index][1] / totalElem) * 100).toFixed(2);
}
console.log(percent, sortedFrequency);
return percent;
}
function drawTable(percent, sortedFrequency) {
table.innerHTML = " ";
row = document.createElement("tr");
table.appendChild(row);
column1 = document.createElement("th");
var KeywordText = document.createTextNode("Keyword");
column1.appendChild(KeywordText);
row.appendChild(column1);
column2 = document.createElement("th");
var CountText = document.createTextNode("Count");
column2.appendChild(CountText);
row.appendChild(column2);
column3 = document.createElement("th");
var PercText = document.createTextNode("Percentage");
column3.appendChild(PercText);
row.appendChild(column3);
min = 20;
if (sortedFrequency.length < min) {
min = sortedFrequency.length;
}
console.log(min);
for (let index = 0; index < min; index++) {
row = document.createElement("tr");
table.appendChild(row);
column1 = document.createElement("td");
var KeywordText = document.createTextNode(sortedFrequency[index][0]);
column1.appendChild(KeywordText);
row.appendChild(column1);
column2 = document.createElement("td");
var CountText = document.createTextNode(sortedFrequency[index][1]);
column2.appendChild(CountText);
row.appendChild(column2);
column3 = document.createElement("td");
var PercText = document.createTextNode(percent[index] + "%");
column3.appendChild(PercText);
row.appendChild(column3);
}
}
function frequency(sentence) {
const words = sentence.split(" ");
const frequency = {};
for (let i = 0; i < words.length; i++) {
const word = words[i];
if (frequency[word]) {
frequency[word]++;
} else {
frequency[word] = 1;
}
}
const sortedFrequency = Object.entries(frequency).sort((a, b) => b[1] - a[1]);
return sortedFrequency;
}