-
Notifications
You must be signed in to change notification settings - Fork 1
/
WordCloud.html
616 lines (555 loc) · 20.1 KB
/
WordCloud.html
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
<!DOCTYPE html>
<html>
<head>
<title>Word Cloud</title>
<link href="stylesheet/style.css" rel="stylesheet">
<script type="text/javascript" src="jquery/jquery-3.1.1.min.js"></script>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="dom-to-image.js"></script>
<script type="text/javascript" src="tableExport/tableExport.js"></script>
<script type="text/javascript" src="tableExport/jquery.base64.js"></script>
<script type="text/javascript" src="tableExport/jspdf/libs/sprintf.js"></script>
<script type="text/javascript" src="tableExport/jspdf/jspdf.js"></script>
<script type="text/javascript" src="tableExport/jspdf/libs/base64.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.debug.js"></script>
</head>
<body>
<div id = "outercontainer">
<div id = "titleDiv">Word Cloud</div>
<div id = "wordcloudDiv">
<svg></svg>
<div id = "progressContainer"><div id = "progressBar"></div></div>
</div>
<div id = "searchDiv">
<input id = "search_input" style="width: 200px;" placeholder="Author Name">
<input id = "x_input" placeholder="# of Papers">
<br>
<button id = "searchbutton" onclick="searchArticle();">Search</button>
<div class="dropdown">
<button class="dropbtn">Recent Search History</button>
<div class="dropdown-content" style="left:0;">
<!-- <a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a> -->
</div>
</div>
<br>
<button id="button_downloadimage" onclick="downloadimage()" disabled=true>Download the Cloud</button>
</div>
<div id= "papersDiv"></div>
<div id="exportDiv">
<button id="subSearch" onclick="subsetSearch()">SubSearch</button>
<button id="exportTablePDF">Export Table to PDF</button>
<button id="exportTableTXT">Export Table to txt</button>
</div>
<div id= "abstractDiv">
<div style="font-size: 20px;">Abstract</div>
<div id= "abstractContent"></div>
<button id = "downloadabstract" onclick="exportabstract();">export abstract</button>
<button id = "backbutton" onclick="backToPapers();">Back</button>
</div>
<script src="pdf.js"></script>
<script src="https://www.jasondavies.com/d3.min.js"></script>
<script src="https://www.jasondavies.com/wordcloud/cloud.min.js"></script>
<script src="d3.layout.cloud.js"></script>
<script>
var time;
var progress;
var parseabstract = false;
var papers = [];
var PDF_URL = '';
var plainText_list;
var frequency_list = [];
var word_title_list = [];
var title_freq_list = [];
var textnum = 0;
var processednum = 0;
PDFJS.workerSrc = 'pdf.worker.js';
init();
var fill = d3.scale.category20();
//generate the cloud to the website
function showCloud() {
d3.layout.cloud().size([800, 500])
.words(frequency_list)
.padding(1)
.rotate(0)
.font("Impact")
.text(function(d) { return d.text; })
.fontSize(function(d) { return d.size; })
.on("end", draw)
.start();
document.getElementById("button_downloadimage").disabled = false;
}
//draw the word cloud using jasondavies reference
function draw(words) {
d3.select(document.getElementById('wordcloudDiv')).append("svg")
.attr("width", 800)
.attr("height", 500)
.append("g")
.attr("transform", "translate(400,250)")
.selectAll("text")
.data(words)
.enter()
.append("text")
.style("font-size", function(d) { return d.size + "px"; })
.style("font-family", "Impact")
.style("fill", function(d, i) { return fill(i); })
.attr("text-anchor", "middle")
.attr("transform", function(d) {
return "translate(" + [d.x, d.y] + ")rotate(" + 0 + ")";
})
.text(function(d) { return d.text; })
.transition()
.each(function () {
d3.select(this).on("click", function (d,i) {
displayPapers(d.text);
});
});
d3.select("svg").remove();
}
function init(){
document.querySelector("#papersDiv").style.visibility = "hidden";
document.querySelector("#x_input").value = 3;
document.querySelector("#abstractDiv").style.display = "none";
document.querySelector("#exportDiv").style.visibility = "hidden";
}
function searchArticle(){
var searchinput = document.querySelector("#search_input").value;
var xinput = document.querySelector("#x_input").value;
document.querySelector(".dropdown-content").innerHTML += '<a onclick="newSearch(this)">' + searchinput + '</a>';
document.querySelector("#wordcloudDiv").style.display = "none";
document.querySelector("#progressContainer").style.display = "block";
document.querySelector("#wordcloudDiv").style.display = "block";
$.ajax({
url: 'php/articlesearch.php',
type: 'POST',
data: {searchinput:searchinput, xinput:xinput},
success : function(data) {
if(data=="error"){alert("Erro occurred"); console.log("error");}
else{
try {
papers = eval(data);
} catch (e) {
if (e instanceof SyntaxError) {
alert("network error occured during API call");
return;
}
}
//papers = eval(data);
displayWordCloud();
console.log(papers);
}
},
error : function() {
alert('Please type in valid artist name.');
}
});
}
function downloadURI(uri) {
var link = document.createElement("a");
link.download = "download";
link.href = uri.innerHTML;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
delete link;
}
function downloadURL(uri) {
var link = document.createElement("a");
link.download = "download";
link.href = uri;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
delete link;
}
function displayWordCloud(){
frequency_list = [];
time = Date.now();
progress = 1;
if(parseabstract){
textnum = papers.length;
}else{
textnum = 0;
}
processednum = 0;
for(var i=0; i<papers.length; i++){
if(parseabstract){
var delay = 4000*i;
setTimeout(function(){moveprogress();}, delay);
parseText(papers[i].abstract,papers[i].title);
}
else{
// if(papers[i].downloadlink.includes("pdf")){
downloadURL(papers[i].downloadlink2);
// }
}
}
if(!parseabstract){setTimeout(function(){converttexts();},10000);}
document.querySelector("#wordcloudDiv").style.display = "block";
}
function converttexts(){
for(var i=0; i<papers.length; i++){
// if(papers[i].downloadlink.includes("pdf")){
var templink = papers[i].downloadlink2;
var tempname = templink.substring(templink.lastIndexOf("/")+1,templink.lastIndexOf(".pdf"));
convertToText("downloads/"+tempname+".pdf", papers[i].title);
textnum++;
// }
}
}
function displayAbstract(tablecell,selectedword){
var clicktitle = tablecell.innerHTML;
for(var i=0; i<papers.length; i++){
if(papers[i].title == clicktitle){
document.querySelector("#papersDiv").style.display = "none";
var paper_abstract = papers[i].abstract; // highlighting the selected word
paper_abstract = paper_abstract.replace(new RegExp(" "+selectedword+" ", 'gi'),
'<span class="highlight">'+" "+selectedword+" "+'</span>');
document.querySelector("#abstractContent").innerHTML = paper_abstract;
document.querySelector("#abstractDiv").style.display = "block";
return;
}
}
}
function backToPapers(){
document.querySelector("#abstractDiv").style.display = "none";
document.querySelector("#papersDiv").style.display = "block";
}
function isLetter(s){
return s.match("^[a-zA-Z\(\)]+$");
}
function parseText(text_list, title) {
var text_str = '';
/**/
if(!parseabstract){
for(var i = 0; i < text_list.length; i++) {
text_str = text_str.concat(text_list[i]);
}
}
/**/
text_str = text_str.concat(text_list);
var text_arr = text_str.split(" ");
var parsed_arr = [];
for(var i = 0; i < text_arr.length; i++) {
var ele = text_arr[i];
if(ele.slice(-1) == "." || ele.slice(-1) == "," || ele.slice(-1) == "?" || ele.slice(-1) == ")") {
ele = ele.substring(0, ele.length - 1);
}
ele = ele.toLowerCase().trim();
if(isLetter(ele) && ele != "" && ele != " " && ele != "a" && ele != "b" && ele != "c" && ele != "d" && ele != "e" &&
ele != "f" && ele != "g" && ele != "h" && ele != "i" && ele != "j" && ele != "k" && ele != "l" &&
ele != "m" && ele != "n" && ele != "o" && ele != "p" && ele != "q" && ele != "r" && ele != "s" &&
ele != "t" && ele != "u" && ele != "v" && ele != "w" && ele != "x" && ele != "y" && ele != "z" &&
ele != "is" && ele != "the" && ele != "of" && ele != "that" && ele != "are" && ele != "be" && ele != "was" &&
ele != "and" && ele != "in" && ele != "it" && ele != "on" && ele != "to" && ele != "not" && ele != "for" &&
ele != "that" && ele != "we" && ele != "with" && ele != "which" && ele != "he" && ele != "she" && ele != "or" &&
ele != "at" && ele != "this" && ele != "so" && ele != "if" && ele != "do" && ele != "go" && ele != "any"
&& ele != "from" && ele != "by" && ele != "as" && ele != "1" && ele != "2" && ele != "3" && ele != "4"
&& ele != "more" && ele != "all" && ele != "no" && ele != "will" && ele != "how" && ele != "our" && ele != "no"
&& ele != "would" && ele != "these" && ele != "my") {
parsed_arr.push(ele);
}
}
var freq_list = {};
for(var i = 0; i < parsed_arr.length; i++) {
var num = parsed_arr[i];
freq_list[num] = freq_list[num] ? freq_list[num]+1 : 1;
}
var sorted_list = [];
for (var word in freq_list) {
sorted_list.push([word, freq_list[word]]);
}
sorted_list.sort(function(a, b) {
return b[1] - a[1];
});
var word_title = [];
var top_freq = 0;
if(sorted_list.length != 0) {
top_freq = sorted_list[0][1];
}
//console.log(sorted_list);
var size = sorted_list.length;
if(size > 250) {size = 250;}
for(var i = 0; i < size; i++) {
var obj = {text: sorted_list[i][0], freq: sorted_list[i][1], title: title};
word_title.push(obj);
}
word_title_list.push(word_title);
for(var i = 0; i < size; i++) {
var ratio = sorted_list[i][1] / top_freq;
var elem_size = ratio * 120 + 30;
var obj = {text : sorted_list[i][0], size : elem_size};
//console.log(obj);
frequency_list.push(obj);
}
processednum++;
if(!parseabstract) moveprogress();
if(textnum==processednum){
if(parseabstract){
var delay = time+processednum*3000-Date.now();
setTimeout(function(){
showCloud();
document.querySelector("#progressContainer").style.display = "none";
},delay);
}else{
setTimeout(function(){
showCloud();
document.querySelector("#progressContainer").style.display = "none";
},1000);
}
}
}
function displayPapers(word) {
title_freq_list = [];
for(var i = 0; i < word_title_list.length; i++) {
for(var j = 0; j < word_title_list[i].length; j++) {
if(word_title_list[i][j].text == word) {
title_freq_list.push({title: word_title_list[i][j].title, freq: word_title_list[i][j].freq});
}
}
}
alltext = ' ';
var table = "<table id = 'papertable'>";
var row = "<thead><tr bgcolor='lightblue'>";
row += "<th><div onclick='sortTable(0)'>Title (click to see Abstract)</div></th>";
row += "<th><div onclick='sortTable(1)'>Authors</div></th>"
row += "<th><div onclick='sortTable(2)'>Conference</div></th>"
row += "<th><div onclick='sortTable(3)'>Download</div></th>"
row += "<th>Bibtex</th> ";
row += "<th><div onclick='sortTable(5)'>Frequency</div></th>";
row += "<th><div>SubSearch</div></th></tr></thead>";
table += row;
table +="<tbody>";
for(var i=0; i<papers.length; i++){
for(var j = 0; j < title_freq_list.length; j++) {
console.log(title_freq_list[j].title);
if(papers[i].title == title_freq_list[j].title) {
row = "<tr bgcolor='lightyellow'>"
row += "<td><div "+"id="+word+
" style='color:grey;' onclick='displayAbstract(this,this.id)'>";
//add an id to the div to enable highlighting the word in the abstract
//row += "<td><div style='width: 200px; color:grey;' onclick='displayAbstract(this)'>";
row += papers[i].title + "</div></td>";
row += "<td>";
for(var k=0; k<papers[i].authors.length; k++){
row += "<div onclick='newSearch(this)'>" + papers[i].authors[k] + "</div>";
}
row += "</td>";
row += "<td>"+ papers[i].conference +"</td>";
row += "<td><a href='#' onclick='downloadURI(this)'>" + papers[i].downloadlink + "</td>";
row += "<td><a href='"+papers[i].bibtex+"'>Bibtex</a></td>";
if(parseabstract){
row += "<td>" + parseInt(title_freq_list[j].freq*7) + "</td>";
}else{
row += "<td>" + title_freq_list[j].freq + "</td>";
}
row += "<td><input type='checkbox' id='" + papers[i].title + "'></td>";
row += "</tr>";
table += row;
break;
}
}
}
table+="</tbody>";
table += "</table>";
document.querySelector("#papersDiv").innerHTML = table;
document.querySelector("#papersDiv").style.visibility = "visible";
sortTable(5);
document.querySelector("#exportDiv").style.visibility = "visible";
document.querySelector("#exportTablePDF").onclick = function() {
$('#papertable').tableExport({type:'pdf', pdfLeftMargin:1, pdfFontSize:10, ignoreColumn: [0], escape:'false'})
}
document.querySelector("#exportTableTXT").onclick = function() {
$('#papertable').tableExport({type:'txt', escape:'false'})
}
}
function convertToText(response, paperTitle){
PDFJS.getDocument(response).then(function (pdf) {
var pdfDocument = pdf;
// Create an array that will contain our promises
var pagesPromises = [];
for (var i = 0; i < pdf.pdfInfo.numPages; i++) {
// Required to prevent that i is always the total of pages
(function (pageNumber) {
// Store the promise of getPageText that returns the text of a page
pagesPromises.push(getPageText(pageNumber, pdfDocument));
})(i + 1);
}
// Execute all the promises
Promise.all(pagesPromises).then(function (pagesText) {
// Display text of all the pages in the console
// e.g ["Text content page 1", "Text content page 2", "Text content page 3" ... ]
parseText(pagesText, paperTitle);
});
}, function (reason) {
// PDF loading error
console.error(reason);
});
}
function getPageText(pageNum, PDFDocumentInstance) {
// Return a Promise that is solved once the text of the page is retrieven
return new Promise(function (resolve, reject) {
PDFDocumentInstance.getPage(pageNum).then(function (pdfPage) {
// The main trick to obtain the text of the PDF page, use the getTextContent method
pdfPage.getTextContent().then(function (textContent) {
var textItems = textContent.items;
var finalString = "";
// Concatenate the string of the item to the final string
for (var i = 0; i < textItems.length; i++) {
var item = textItems[i];
finalString += item.str + " ";
}
// Solve promise with the text retrieven from the page
resolve(finalString);
});
});
});
}
function moveprogress(){
if(parseabstract){
console.log("move "+progress);
var bar = document.querySelector("#progressBar");
bar.style.width = progress/textnum*200 +"px";
progress++;
}else{
var bar = document.querySelector("#progressBar");
bar.style.width = processednum/textnum*200 +"px";
}
}
function sortTable(j) {
var table, rows, switching, i, x, y, shouldSwitch;
table = document.getElementById("papertable");
switching = true;
/*Make a loop that will continue until
no switching has been done:*/
while (switching) {
//start by saying: no switching is done:
switching = false;
rows = table.getElementsByTagName("tr");
/*Loop through all table rows (except the
first, which contains table headers):*/
for (i = 1; i < (rows.length - 1); i++) {
//start by saying there should be no switching:
shouldSwitch = false;
/*Get the two elements you want to compare,
one from current row and one from the next:*/
x = rows[i].getElementsByTagName("td")[j];
y = rows[i + 1].getElementsByTagName("td")[j];
//check if the two rows should switch place:
if (j == 5) { // if sorting the frequency
if (parseFloat(x.innerHTML) < parseFloat(y.innerHTML) ) {
//if so, mark as a switch and break the loop:
shouldSwitch= true;
break;
}
}
else {
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
//if so, mark as a switch and break the loop:
shouldSwitch= true;
break; }
}
}
if (shouldSwitch) {
/*If a switch has been marked, make the switch
and mark that a switch has been done:*/
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
}
function newSearch(i){
document.querySelector("#wordcloudDiv").style.display = "none";
document.querySelector("#papersDiv").style.visibility = "hidden";
document.querySelector("#exportDiv").style.visibility = "hidden";
document.querySelector("#wordcloudDiv").style.display = "none";
document.querySelector("#progressContainer").style.display = "block";
document.querySelector("#wordcloudDiv").style.display = "block";
var searchinput = i.innerHTML;
document.querySelector("#search_input").value = searchinput;
var xinput = document.querySelector("#x_input").value;
document.querySelector(".dropdown-content").innerHTML += '<a>' + searchinput + '</a>';
$.ajax({
url: 'php/articlesearch.php',
type: 'POST',
data: {searchinput:searchinput, xinput:xinput},
success : function(data) {
if(data=="error"){alert("Erro occurred"); console.log("error");}
else{
papers = eval(data);
displayWordCloud();
//console.log(data);
}
},
error : function() {
alert('Please type in valid artist name.');
}
});
}
function exportabstract(){
var abstract = document.querySelector("#abstractContent").textContent;
var doc = new jsPDF();
doc.setFontSize(12);
var index1=0;
var index2=abstract.length;
var line = 20;
while(index1<index2){
var substring = abstract.substring(index1, Math.min(index1+70,index2));
doc.text(substring, 20, line);
index1 += 70;
line += 10;
}
doc.save('a4.pdf')
}
function subsetSearch() {
var sorted_list = [];
var new_word_title_list = [];
for(var i = 0; i < title_freq_list.length; i++) {
var title = title_freq_list[i].title;
if(document.getElementById(title).checked == true) {
for(var j = 0; j < word_title_list.length; j++) {
if(word_title_list[j][0].title == title) {
new_word_title_list.push(word_title_list[j]);
for(var x = 0; x < word_title_list[j].length; x++) {
sorted_list.push([word_title_list[j][x].text, word_title_list[j][x].freq]);
}
}
}
}
}
word_title_list = new_word_title_list;
console.log(word_title_list);
sorted_list.sort(function(a, b) {
return b[1] - a[1];
});
var top_freq = 0;
if(sorted_list.length != 0) {
top_freq = sorted_list[0][1];
}
var size = sorted_list.length;
if(size > 250) {size = 250;}
frequency_list = [];
for(var i = 0; i < size; i++) {
var ratio = sorted_list[i][1] / top_freq;
var elem_size = ratio * 120 + 30;
var obj = {text : sorted_list[i][0], size : elem_size};
frequency_list.push(obj);
}
document.querySelector("#wordcloudDiv").style.display = "block"
showCloud();
}
function downloadimage() {
domtoimage.toJpeg(document.getElementById('wordcloudDiv'), { quality: 0.95 })
.then(function (dataUrl) {
var link = document.createElement('a');
link.download = 'my-image-name.jpeg';
link.href = dataUrl;
link.click();
});
}
</script>
</div>
</body>
</html>