-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdynamic_wordclouds.js
1328 lines (1129 loc) · 46 KB
/
dynamic_wordclouds.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
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*TODO
-- deal with default bootstrap.js handling of clicking on menus so we can click-edit values into the sliders
*/
//Data containers
var master_datasets = []; //All datasets encoded
var current_data = []; //Of size 1 for dynamic wordcloud, of size 3 for venncloud
var current_display_data = []; //array of size 1 for singecloud, size 3 for venncloud
var display_index_in_master_data = []; // array of size 1 for singlecloud, size 2 for venncloud [left_index, right_index]
var dataset_selection_list = [];
//jquery element that we will paint to -- parent pane
var main_wordcloud_container;
var selected_datasets = [];
//Convenience variable for better intuitive reading of the code to indicate what kind of viz we are making
var venncloud = false;
var singlecloud = false;
//Slider values and defaults go here, not all have been moved accross yet.
var overall_max_observed = 0;
var overall_min_idf_observed = 1;
var overall_max_word_length_observed=1;
//Store settings as top level object
var s = {
center_threshold: 0.1,
center_threshold_min: 0,
center_threshold_max: 1,
center_step: 0.003,
min_req_tf: 10,
max_req_tf: 40000,
min_req_idf: 0.0001,
max_req_idf: 0.2,
min_req_chars: 1,
max_req_chars: 100,
size_rarity_weight: 0.8,
opacity_rarity_weight: 0.5,
size_frequency_weight: 0.5,
opacity_frequency_weight: 0.5,
base_fontsize: 30,
base_opacity: 3,
opacity_slider_max_value: 10,
sort_type: 'ALPHABETIC',
mean_counts: 1,
mean_idf: 1,
display_words: true,
display_hashtags: true,
display_user_mentions: true,
wordcloud_element: 'wordcloud_location',
onclick_function: function (token) {
default_example_onclick(token);
},
oncontextclick_function: function (token) {
}
};
/**************/
/** Controls **/
/**************/
//Control visibility
var common_cloud_controls_visible = false;
var frequency_filter_visible = false;
var rarity_filter_visible = false;
var sort_buttons_visible = false;
var wordcloud_entities_buttons_visible = false;
var opacity_controls_visible = false;
var size_controls_visible = false;
var wordcloud_description_visible = false;
var do_not_redraw_wordcloud = false;
var highlight_keywords = false;
/*******************/
/**Example Windows**/
/*******************/
//if (document.getElementById('floatdivright')) {
floatingMenu.add('floatdivright',
{
// Represents distance from left or right browser window
// border depending upon property used. Only one should be
// specified.
// targetLeft: 0,
targetRight: 10,
// Represents distance from top or bottom browser window
// border depending upon property used. Only one should be
// specified.
//targetTop: 10,
targetBottom: 10,
// Uncomment one of those if you need centering on
// X- or Y- axis.
// centerX: true,
// centerY: true,
// Remove this one if you don't want snap effect
snap: true
});
//}
//if (document.getElementById('floatdivleft')) {
floatingMenu.add('floatdivleft',
{
targetLeft: 10,
targetBottom: 10,
snap: true
});
//}
function hide_example_windows() {
$('#floatdivright').hide();
$('#floatdivleft').hide();
}
function show_example_windows() {
if (venncloud) {
$('#floatdivright').show();
}
$('#floatdivleft').show();
}
function default_example_onclick(token) {
if (token.length > 50) {
//This happens if you click outside a word, ignore.
return;
}
//The second window only has to be displayed if we are showing a Venncloud
if (venncloud) {
var r_tokens = master_datasets[display_index_in_master_data[1]].tokens;
var r_str_exs = 'N/A';
if (token in r_tokens) {
r_str_exs = r_tokens[token].examples.join('<div style="font-size:5pt"> <BR></div>');
}
$('#examples_right').html(r_str_exs);
}
//Always display the left window
var l_tokens = master_datasets[display_index_in_master_data[0]].tokens;
var l_str_exs = 'N/A';
if (token in l_tokens) {
l_str_exs = l_tokens[token].examples.join('<div style="font-size:5pt"> <BR></div>');
}
$('#examples_left').html(l_str_exs);
show_example_windows();
}
//Wrapper function, in case we have any other windows
function hide_all_windows() {
hide_example_windows();
}
function initialize_wordcloud_controls() {
//What should we do if the dictionary widget isn't active?
//user_dictionaries = get_dictionaries();
$("#radio").buttonset();
$("#radio").click(function () {
draw_wordcloud();
});
$("div#entities_buttons").click(function () {
draw_wordcloud();
});
$("#required_observations_slider").slider({range: true});
$("#required_idf_slider").slider({range: true});
$("#required_characters_slider").slider({range: true});
///////////////////
// USER CONTROLS //
///////////////////
//Common cloud controls
function common_cloud_change(event, ui) {
var orig_value = $("#common_cloud_controls").slider("values", 0);
//Dynamic range seems to want to be between 0 and 0.03
//value = orig_value / (100 / 0.01)
value = orig_value;
//value = orig_value * (100/55) / 5000 //Convert value to something more sensible for a threshold
//DEBUG//document.getElementById('common_cloud_controls_out').innerHTML = value + " " + orig_value;
s.center_threshold = value;
draw_wordcloud();
}
$(function () {
$("#common_cloud_controls").slider({
"max": s.center_threshold_max,
"min": s.center_threshold_min,
"step": s.center_step,
"value": s.center_threshold,
slide: function (event, ui) {
common_cloud_change(event, ui);
},
change: function (event, ui) {
common_cloud_change(event, ui);
}
});
});
//begin rarity slider
function size_rarity_update_change(event, ui) {
s.size_rarity_weight = $('#size_rarity_slider').slider("values", 0);
update_wordcloud();
}
$(function () {
$("#size_rarity_slider").slider({
"min": 0,
"max": 1,
"step": 0.01,
"value": s.size_rarity_weight,
slide: function (event, ui) {
size_rarity_update_change(event, ui);
},
change: function (event, ui) {
size_rarity_update_change(event, ui);
}
});
});
function opacity_rarity_change(event, ui) {
//var orig_value = $( "#opacity_rarity_slider" ).slider( "values", num_words_max );
var value = $("#opacity_rarity_slider").slider("values", 1);
//DEBUG//document.getElementById('opacity_rarity_slider_out').innerHTML = value + " " + orig_value;
s.opacity_rarity_weight = 1 - value;
update_wordcloud();
}
$(function () {
$("#opacity_rarity_slider").slider({
"min": 0,
"max": 1,
"step": 0.01,
"value": 1 - s.opacity_rarity_weight,
slide: function (event, ui) {
opacity_rarity_change(event, ui);
},
change: function (event, ui) {
opacity_rarity_change(event, ui);
}
});
});
//begin count sliders
function size_frequency_change(event, ui) {
//var orig_value = $( "#size_frequency_slider" ).slider( "values", num_words_max );
//DEBUG//document.getElementById('size_frequency_slider_out').innerHTML = value + " " + orig_value;
s.size_frequency_weight = $("#size_frequency_slider").slider("values", 1);
update_wordcloud();
}
$(function () {
$("#size_frequency_slider").slider({
"min": 0,
"max": 1,
"step": 0.01,
"value": s.size_frequency_weight,
slide: function (event, ui) {
size_frequency_change(event, ui);
},
change: function (event, ui) {
size_frequency_change(event, ui);
}
});
});
function opacity_frequency_change(event, ui) {
//var orig_value = $( "#opacity_frequency_slider" ).slider( "values", num_words_max );
//DEBUG//document.getElementById('opacity_frequency_slider_out').innerHTML = value + " " + orig_value;
s.opacity_frequency_weight = $("#opacity_frequency_slider").slider("values", 1);
update_wordcloud();
}
/*
$(function() {
$( "#opacity_frequency_slider" ).slider();
$( "#opacity_frequency_slider" ).slider("option","min",0);
$( "#opacity_frequency_slider" ).slider("option","max",1);
$( "#opacity_frequency_slider" ).slider("option","step",0.01);
$( "#opacity_frequency_slider" ).slider("option","value",s.opacity_frequency_weight);
$( "#opacity_frequency_slider" ).slider({
slide: function( event, ui ) { opacity_frequency_change(event,ui);},
change: function( event, ui ) { opacity_frequency_change(event,ui);}
});
});
*/
$(function () {
$("#opacity_frequency_slider").slider({
"min": 0,
"max": 1,
"step": 0.01,
"value": s.opacity_frequency_weight,
slide: function (event, ui) {
opacity_frequency_change(event, ui);
},
change: function (event, ui) {
opacity_frequency_change(event, ui);
}
});
});
//end count slider
function update_required_tf_filter_display(values) {
//document.getElementById("required_observations_out").innerHTML = '[' + values[0] + ' , ' + values[1] + ']';
// min value
var min = $( "#min_required_observations" );
if(min.attr('data-mode')==='input')
min.find('input').val(values[ 0 ]);
else
min.text(values[ 0 ]);
// max value
var max = $( "#max_required_observations" );
if(max.attr('data-mode')==='input')
max.find('input').val(values[ 1 ]);
else
max.text(values[ 1 ]);
}
//begin Required Observations slider
$(function () {
/*$( "#required_observations_slider" ).slider("option","min",1);
$( "#required_observations_slider" ).slider("option","max",overall_max_observed);
$( "#required_observations_slider" ).slider("option","step",1);
$( "#required_observations_slider" ).slider("option","values",[s.min_req_tf,s.max_req_tf]);
*/
$("#required_observations_slider").slider({
range: true,
min: 1,
max: overall_max_observed,
step: 1,
value: [s.min_req_tf, s.max_req_tf],
change: function (event, ui) {
//var orig_value = $( "#required_observations_slider" ).slider( "values", num_words_max );
sli = $('#required_observations_slider');
var orig_value = sli.slider("values") || [s.min_req_tf, s.max_req_tf];
value = orig_value;
//DEBUG//document.getElementById('required_observations_slider_out').innerHTML = value + " " + orig_value;
s.min_req_tf = value[0];
s.max_req_tf = value[1];
update_required_tf_filter_display([s.min_req_tf, s.max_req_tf]);
//required_observations = value;
//document.getElementById("required_observations_out").innerHTML = required_observations;
draw_wordcloud();
},
slide: function (event, ui) {
var tmp = $("#required_observations_slider").slider("values");
update_required_tf_filter_display(tmp);
}
});
//update_required_tf_filter_display($("required_observations_slider").slider("values"));
update_required_tf_filter_display([s.min_req_tf, s.max_req_tf]); // Maybe we want to display overall_max_observed instead?
// attach click handlers to result display
$( "#min_required_observations" ).on('click', function(event) { sliderValueInputHandler(event); });
$( "#max_required_observations" ).on('click', function(event) { sliderValueInputHandler(event); });
});
function sliderValueInputHandler(event, conversionFunction){
conversionFunction = conversionFunction || function(value) {return value;};
var fieldValue;
var targetElement = $(event.target);
if(targetElement.attr('data-mode') === 'text') {
// get field value
fieldValue = targetElement.text();
// change "mode" attribute
targetElement.attr('data-mode', 'input');
// remove all child nodes
targetElement.empty();
// append an input node
targetElement.append('<input />');
var newInput = targetElement.find('input');
// set value into input
newInput.val(fieldValue);
// attach change handler
newInput.on('change',function(event){
var eSlider = $(targetElement.attr('data-for'));
var type = targetElement.attr('data-range-element');
var currentValues;
switch (type) {
case 'min':
{
currentValues = eSlider.slider('values');
eSlider.slider('option', 'values', [ conversionFunction(newInput.val()), currentValues[1]]);
break;
}
case 'max':
{
currentValues = eSlider.slider('values');
eSlider.slider('option', 'values', [ currentValues[0], conversionFunction(newInput.val())]);
break;
}
default:
{
eSlider.slider('option', 'value', conversionFunction(newInput.val()));
break;
}
}
});
newInput.on('blur', function(event){
// revert back to text
fieldValue = newInput.val();
// change "mode" attribute
targetElement.attr('data-mode', 'text');
// remove handlers
newInput.off();
// remove all child nodes
targetElement.empty();
// set text
targetElement.text(fieldValue);
});
}
}
//begin Required IDF slider
function update_required_idf_filter_display(values) {
//var disp = '[' + Math.floor(values[0]) + ' , ' + Math.floor(values[1]) + ']';
//document.getElementById("required_idf_out").innerHTML = disp;
// min value
var min = $( "#min_required_idf" );
if(min.attr('data-mode')==='input')
min.find('input').val(Math.floor(values[0]));
else
min.text(Math.floor(values[0]));
// max value
var max = $( "#max_required_idf" );
if(max.attr('data-mode')==='input')
max.find('input').val(Math.floor(values[1]));
else
max.text(Math.floor(values[1]));
}
$(function () {
/*
$( "#required_idf_slider" ).slider();
$( "#required_idf_slider" ).slider("option","min",1);
$( "#required_idf_slider" ).slider("option","max",1/overall_min_idf_observed);
$( "#required_idf_slider" ).slider("option","step",1);
$( "#required_idf_slider" ).slider("option","value",(1/overall_min_idf_observed)/4);
*/
$("#required_idf_slider").slider({
min: 1,
max: 1 / overall_min_idf_observed,
step: 0.0001,
values: [1 / s.max_req_idf, 1 / s.min_req_idf],
change: function (event, ui) {
//var orig_value = 1/$( "#required_idf_slider" ).slider( "values", num_words_max );
var orig_value = $("#required_idf_slider").slider("values");
value = orig_value;
s.min_req_idf = 1 / orig_value[1];
s.max_req_idf = 1 / orig_value[0];
update_required_idf_filter_display([1 / s.max_req_idf, 1 / s.min_req_idf]);
draw_wordcloud();
},
slide: function (event, ui) {
var tmp = $("#required_idf_slider").slider("values");
update_required_idf_filter_display(tmp);
}
});
update_required_idf_filter_display([1 / s.max_req_idf, 1 / s.min_req_idf]);
// attach click handlers to result display
$( "#min_required_idf" ).on('click', function(event) { sliderValueInputHandler(event); });
$( "#max_required_idf" ).on('click', function(event) { sliderValueInputHandler(event); });
});
function update_required_characters_filter_display(values) {
// min value
var min = $( "#min_required_characters" );
if(min.attr('data-mode')==='input')
min.find('input').val(values[ 0 ]);
else
min.text(values[ 0 ]);
// max value
var max = $( "#max_required_characters" );
if(max.attr('data-mode')==='input')
max.find('input').val(values[ 1 ]);
else
max.text(values[ 1 ]);
}
//begin Required Word Length
$(function () {
$("#required_characters_slider").slider({
range: true,
min: 1,
max: overall_max_word_length_observed,
step: 1,
value: [s.min_req_chars, s.max_req_chars],
change: function (event, ui) {
sli = $('#required_characters_slider');
var orig_value = sli.slider("values") || [s.min_req_chars, s.max_req_chars];
value = orig_value;
s.min_req_chars = value[0];
s.max_req_chars = value[1];
update_required_characters_filter_display([s.min_req_chars, s.max_req_chars]);
draw_wordcloud();
},
slide: function (event, ui) {
var tmp = $("#required_characters_slider").slider("values");
update_required_characters_filter_display(tmp);
}
});
update_required_characters_filter_display([s.min_req_chars, s.max_req_chars]); // Maybe we want to display overall_max_observed instead?
// attach click handlers to result display
$( "#min_required_characters" ).on('click', function(event) { sliderValueInputHandler(event); });
$( "#max_required_characters" ).on('click', function(event) { sliderValueInputHandler(event); });
});
//end size slider
//begin dynamic range
function base_fontsize_change(event, ui) {
//var orig_value = $( "#base_fontsize_slider" ).slider( "values", num_words_max );
var orig_value = $("#base_fontsize_slider").slider("values", 12);
value = orig_value;
//DEBUG//document.getElementById('base_fontsize_slider_out').innerHTML = value + " " + orig_value;
s.base_fontsize = value;
update_wordcloud();
}
$(function () {
handle = $('#base_fontsize_slider');
handle.slider();
handle.slider("option", "min", 1);
handle.slider("option", "max", 100);
handle.slider("option", "step", 1);
handle.slider("option", "value", s.base_fontsize);
handle.slider({
slide: function (event, ui) {
base_fontsize_change(event, ui);
},
change: function (event, ui) {
base_fontsize_change(event, ui);
}
});
});
function base_opacity_change(event, ui) {
var sli = $("#base_opacity_slider");
//var orig_value = $( "#base_opacity_slider" ).slider( "values", num_words_max );
var orig_value = sli.slider("values", 0.5);
value = orig_value;
//DEBUG//document.getElementById('base_opacity_slider_out').innerHTML = value + " " + orig_value;
//base_opacity = opacity_slider_max_value - value;
s.base_opacity = value;
update_wordcloud();
}
$(function () {
$("#base_opacity_slider").slider({
"min": 0,
"value": s.base_opacity,
"max": s.opacity_slider_max_value,
"step": 0.01,
slide: function (event, ui) {
base_opacity_change(event, ui);
},
change: function (event, ui) {
base_opacity_change(event, ui);
}
});
});
//end dynamic range sliders
//hide_all_controls();
} //End of initialize_wordcloud_controls
//What year is this that sum() is not defined by default!? --GAC
function sum(l) {
return l.reduce(function (a, b) {
return a + b;
});
}
/*
creates a clone. if passed a decorator function will apply it when cloning an object
Note: for objects, clone does NOT follow prototype chain, however, it will clone objects set as properties
*/
function clone(original, decoratorFunction) {
// start by assuming that original is a value, and can simply be copied via assignment
var copy = original;
// check to see if original is an object, since objects are assigned by reference
if (typeof original === "object") {
// check if array since both arrays and objects will be identified as "object" by 'typeof'
if (toString.call(original) === "[object Array]") {
copy = [];
for (var i = 0, len = original.length; i < len; i += 1) {
copy.push(clone(original[i]));
}
} else {
// it is really an object
copy = {};
for (var prop in original) {
if (original.hasOwnProperty(prop)) {
copy[prop] = clone(original[prop]);
}
}
// if got a decorator function, apply it
if (typeof decoratorFunction === "function") {
copy = decoratorFunction(copy);
}
}
}
return copy;
}
//Passthrough for the moment, can adapt if the data is not in the expected form (e.g. arbreviz?)
proc_query_data = function (query) {
return query;
};
function filter_for_idf(to_filter_dict) {
// If 'required_idf_slider' does not exist, then s.min_req_idf and s.max_req_idf
// will not be set properly by the callback functions attached to the slider.
// Without this guard condition, all words are filtered out when 'required_idf_slider'
// does not exist.
if (document.getElementById('required_idf_slider')) {
$.each(Object.keys(to_filter_dict), function (index, key) {
this_idf = to_filter_dict[key].idf;
if (this_idf < s.min_req_idf || this_idf > s.max_req_idf) {
delete to_filter_dict[key];
}
});
}
}
function filter_for_characters(to_filter_dict) {
$.each(Object.keys(to_filter_dict), function (index, key) {
this_word_length = key.length;
if (this_word_length < s.min_req_chars || this_word_length > s.max_req_chars) {
delete to_filter_dict[key];
}
});
}
//TODO: Make a function to take an arbitrary number of dicts to filter, and filter on the sum of their tfs
function filter_for_required_tf(to_filter_dicts) {
//If we are passed {} or [{}] but not [{},{},...], this is simple and fast
if (to_filter_dicts.constructor != Array || to_filter_dicts.length == 1) {
if (to_filter_dicts.constructor == Array) {
to_filter_dict = to_filter_dicts[0]; // Passed [{}]
}
else {
to_filter_dict = to_filter_dicts; // passed {}
}
$.each(Object.keys(to_filter_dict), function (index, key) {
this_tf = to_filter_dict[key].tf;
//if (this_tf < s.min_req_tf || this_tf > s.max_req_tf ) {
if (key != 'orioles' && (this_tf < s.min_req_tf || this_tf > s.max_req_tf )) {///HARDCODE WHY!?
delete to_filter_dict[key];
}
});
return;
}
//If we're passed [{},{},...] sum the occurences of each token across all objects before applying filter
var all_tokens = {}; //Keyed on token, value is summed tf
var index, len;
for (index = 0, len = to_filter_dicts.length; index < len; index++) {
to_filter_dict = to_filter_dicts[index];
$.each(Object.keys(to_filter_dict), function (index, key) {
this_tf = to_filter_dict[key].tf;
if (key in all_tokens) {
all_tokens[key] += this_tf;
}
else {
all_tokens[key] = this_tf;
}
});
}
//Now go through and filter out any tokens for which we don't have enough occurences
$.each(Object.keys(all_tokens), function (index, key) {
combined_tf = all_tokens[key];
if (combined_tf < s.min_req_tf || combined_tf > s.max_req_tf) {
for (index = 0, len = to_filter_dicts.length; index < len; index++) {
to_filter_dict = to_filter_dicts[index];
//if (key in to_filter_dict){
if (key in to_filter_dict && key != 'orioles') {
delete to_filter_dict[key];
}
}
}
});
}
function filter_for_required_observations_and_idf(to_filter_dict) {
//TODO: classifier score
filter_for_idf(to_filter_dict);
$.each(Object.keys(to_filter_dict), function (index, key) {
this_tf = to_filter_dict[key].tf;
if (this_tf < s.min_req_tf || this_tf > s.max_req_tf) {
delete to_filter_dict[key];
}
});
}
//This function might not actually be needed, given how we constructed compute master data
function filter_multiple_for_required_tf_and_idf(to_filter_dicts) {
//Filter each list individually for idf
for (var index = 0, len = to_filter_dicts.length; index < len; index++) {
filter_for_idf(to_filter_dicts[index]);
}
//Filter them jointly for tf
filter_for_required_tf(to_filter_dicts);
}
//TODO: Add displays necessary to make this function
function filter_for_display_entities_types(to_filter) {
if (document.getElementById('display_plain_words')) {
s.display_words = $('#display_plain_words').is(':checked');
}
else {
// Wordcloud should be displayed even if DOM is missing 'display_plain_words' checkbox
s.display_words = true;
}
s.display_hashtags = $('#display_hashtags').is(':checked');
s.display_user_mentions = $('#display_user_mentions').is(':checked');
if (s.display_words && s.display_user_mentions && s.display_hashtags) {
return to_filter; //All types selected, this is just a passthrough
}
//filtered = [];
//console.log(to_filter);
$.each(Object.keys(to_filter), function (index, i) {
key = to_filter[i].text;
if (key[0] == '@') {
if (!s.display_user_mentions) {
delete to_filter[i];
}
}
else if (key[0] == '#') {
if (!s.display_hashtags) {
delete to_filter[i];
}
}
else {
if (!s.display_words) {
delete to_filter[i];
}
}
});
//return filtered;
return to_filter;
}
//Sorting
function sorter(to_sort, my_sort_type) {
to_sort.sort(function (a, b) {
return b.text < a.text;
}); // The default is alphabetic
if (my_sort_type === 'IDF') {
to_sort.sort(function (a, b) {
return b.idf - a.idf;
});
} // Reverse, so rarer words are on top
if (my_sort_type === 'COUNT') {
to_sort.sort(function (a, b) {
return b.tf - a.tf;
});
}
return to_sort;
}
function preference_sorter(to_sort) {
//Sort by what the user has specified
s.sort_type = $("#radio :radio:checked").attr('id');
sorted = sorter(to_sort, s.sort_type);
return sorted;
}
//Size and Opacity calculations
get_size = function (count, idf) {
var weighted_by_count = count * (10 / overall_max_observed); //HARDCODE??
// When IDF=1, weighted_by_rarity_size=Infinity, which causes weighted_size
// to be set to Infinity - which makes all fonts have the same (maximum) size
//var weighted_by_rarity_size = 1 / Math.log(1 / idf); //HARDCODE turned off
var weighted_by_rarity_size = 1;
var weighted_size = s.base_fontsize; // A base size
//console.log(weighted_size, weighted_by_rarity_size, weighted_by_count, count, idf);
weighted_size *= (1 - s.size_frequency_weight) + s.size_frequency_weight * weighted_by_count;
weighted_size *= (1 - s.size_rarity_weight) + (s.size_rarity_weight * weighted_by_rarity_size);
//DEBUG.innerHTML = "~!" + (1-rarity_weight) * unweighted_by_rarity_size + "~~" + rarity_weight * idf + "!@!" + max_observed_count;
if (weighted_size < 10) {
return 10;
}
if (weighted_size > 60) {
return 60;
}
return(weighted_size);
};
get_opacity = function (count, idf) {
weighted_opacity = s.base_opacity;
weighted_by_count = count / (mean_counts * 2);
weighted_by_rarity = 1 / Math.log(1 / idf);
weighted_opacity *= (1 - s.opacity_frequency_weight) + (s.opacity_frequency_weight * weighted_by_count);
weighted_opacity *= (s.opacity_rarity_weight) + ((1 - s.opacity_rarity_weight) * weighted_by_rarity);
if (weighted_opacity < 0.1) {
weighted_opacity = 0.1;
}
if (weighted_opacity > 1) {
weighted_opacity = 1;
}
return(weighted_opacity);
};
//NB: Each word knows where it is located, so we don't explicitly target the WC location
function update_wordcloud() {
//Updates all the WC visual aspects that have changed since last time.
for (var cloud_index = 0, numclouds = current_display_data.length; cloud_index < numclouds; cloud_index++) {
$.each(Object.keys(current_display_data[cloud_index]), function (index) {
token = current_display_data[cloud_index][index];
var token_element = token.handle;
token_element.style.fontSize = get_size(token.tf, token.idf) + 'pt';
token_element.style.opacity = get_opacity(token.tf, token.idf);
});
}
add_description_to_display();
}
function prepare_wordcloud_data(selected_datasets) {
//selected datasets is an array of length 1 or 2 indicating the index of the dataset(s) to be used
//TODO:Move to where we switch modes
//Determine whether we are working on one or two corpora
singlecloud = false;
venncloud = false;
if (selected_datasets.length == 1) {
singlecloud = true;
}
else if (selected_datasets.length == 2) {
venncloud = true;
}
else {
alert('Oh shucks, something broke.');
}
//Store the dataset(s) selected in current_data
current_data = []; //Of size 1 for dynamic wordcloud, of size 3 for venncloud
for (var j = 0, lenj = selected_datasets.length; j < lenj; j++) {
var dat = clone(master_datasets[selected_datasets[j]].tokens);
//Filter for idf,tf,entity types
dat = filter_for_display_entities_types(dat);
filter_for_idf(dat);
filter_for_characters(dat);
current_data.push(dat);
}
filter_for_required_tf(current_data);
//Split into lists (if applicable) and convert to arrays
if (singlecloud) {
current_display_data = [];
for (var k in current_data[0]) {
current_display_data.push(current_data[0][k]);
}
current_display_data = [current_display_data];
}
else if (venncloud) {
var common_list = [];
var left_list = [];
var right_list = [];
current_display_data = [left_list, common_list, right_list];
var L = current_data[0];
var R = current_data[1];
var all_tokens = {};
for (var k1 in L) {
all_tokens[k1] = true;
}
for (var k2 in R) {
all_tokens[k2] = true;
}
for (var token in all_tokens) {
//TODO: classifier scores
var l_prop = L[token] && L[token].prop_tokens || 0;
var l_tf = L[token] && L[token].tf || 0;
var r_prop = R[token] && R[token].prop_tokens || 0;
var r_tf = R[token] && R[token].tf || 0;
if (r_tf > 0 && l_tf > 0 &&
Math.abs((l_prop - r_prop) / (l_prop + r_prop)) <= s.center_threshold) {
common_token = {};
common_token.text = token;
common_token.tf = (l_tf + r_tf); // Shoudl we average instead of add?
common_token.idf = L[token].idf;
//Add classifier scores?
common_list.push(common_token);
}
else {
if (r_prop < l_prop) {
left_list.push(L[token]);
}
else {
right_list.push(R[token]);
}
}
}
}
//Sort
$.map(current_display_data, preference_sorter);
/*
//Do set-level changes -- i.e. those that result in a complete redraw of the wc
//Figure out what we are displayign and in what order
current_display_data = [];
for (var i=0, len=current_data.length; i<len; i++){
var tmp =filter_for_required_observations_and_idf(current_data[i]);
tmp = filter_for_display_entities_types(tmp);
tmp = sorter(tmp, 'COUNT'); //HARDCODE -- number of words is not getting set correctly
current_display_data.push(tmp);
};
*/
}
function tf_size_weight_str() {
return "TF:" + Math.round(s.size_frequency_weight * 100) / 100;
}
function idf_size_weight_str() {
return "IDF:" + Math.round(s.size_rarity_weight * 100) / 100;
}
function tf_opacity_weight_str() {
return "TF:" + Math.round(s.opacity_frequency_weight * 100) / 100;
}
function idf_opacity_weight_str() {
return "IDF:" + Math.round(s.opacity_rarity_weight * 100) / 100;
}
function add_description_to_display() {
var d = "";