-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract_words_static_html.html
430 lines (393 loc) · 25.6 KB
/
extract_words_static_html.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bokeh Plot</title>
<link rel="stylesheet" href="https://cdn.pydata.org/bokeh/release/bokeh-1.2.0.min.css" type="text/css" />
<link rel="stylesheet" href="https://cdn.pydata.org/bokeh/release/bokeh-widgets-1.2.0.min.css" type="text/css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/wordcloud2.js/1.0.6/wordcloud2.min.js"></script>
<script type="text/javascript" src="https://raw.githubusercontent.com/timdream/wordcloud2.js/gh-pages/src/wordcloud2.js"></script>
<script type="text/javascript" src="https://cdn.pydata.org/bokeh/release/bokeh-1.2.0.min.js"></script>
<script type="text/javascript" src="https://cdn.pydata.org/bokeh/release/bokeh-widgets-1.2.0.min.js"></script>
<script type="text/javascript">
Bokeh.set_log_level("info");
</script>
<script type="text/javascript">
(function(root, factory) {
// if(typeof exports === 'object' && typeof module === 'object')
// factory(require("Bokeh"));
// else if(typeof define === 'function' && define.amd)
// define(["Bokeh"], factory);
// else if(typeof exports === 'object')
// factory(require("Bokeh"));
// else
factory(root["Bokeh"]);
})(this, function(Bokeh) {
var define;
return (function outer(modules, entry) {
if (Bokeh != null) {
return Bokeh.register_plugin(modules, {}, entry);
} else {
throw new Error("Cannot find Bokeh. You have to load it prior to loading plugins.");
}
})
({
"custom/main": function(require, module, exports) {
var models = {
"WordCloud2": require("custom/bokeh_wordcloud2.bokeh_wordcloud2.word_cloud2").WordCloud2
};
require("base").register_models(models);
module.exports = models;
},
"custom/bokeh_wordcloud2.bokeh_wordcloud2.word_cloud2": function(require, module, exports) {
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
// import {DataProvider} from "models/widgets/tables/data_table";
var widget_1 = require("models/widgets/widget");
var p = require("core/properties");
var cds_view_1 = require("models/sources/cds_view");
var widgets_1 = require("models/widgets");
var column_data_source_1 = require("models/sources/column_data_source");
var object_1 = require("core/util/object");
var array_1 = require("core/util/array");
var bokeh_events_1 = require("core/bokeh_events");
exports.DTINDEX_NAME = "__bkdt_internal_index__";
function event(event_name) {
return function (cls) {
cls.prototype.event_name = event_name;
};
}
var WordClickEvent = /** @class */ (function (_super) {
__extends(WordClickEvent, _super);
function WordClickEvent(word, weight) {
var _this = _super.call(this) || this;
_this.word = word;
_this.weight = weight;
return _this;
}
WordClickEvent.prototype._to_json = function () {
var _a = this, word = _a.word, weight = _a.weight;
return __assign({}, _super.prototype._to_json.call(this), { weight: weight, word: word });
};
WordClickEvent = __decorate([
event("word_click_event")
], WordClickEvent);
return WordClickEvent;
}(bokeh_events_1.BokehEvent));
exports.WordClickEvent = WordClickEvent;
// export type myCB<T> = CallbackLike<T, [string,number,number,number,number], Ret>
var DataProvider = /** @class */ (function () {
function DataProvider(source, view) {
this.source = source;
this.view = view;
if (exports.DTINDEX_NAME in this.source.data)
throw new Error("special name " + exports.DTINDEX_NAME + " cannot be used as a data table column");
this.index = this.view.indices;
}
DataProvider.prototype.getLength = function () {
return this.index.length;
};
DataProvider.prototype.getItem = function (offset) {
var item = {};
for (var _i = 0, _a = object_1.keys(this.source.data); _i < _a.length; _i++) {
var field = _a[_i];
item[field] = this.source.data[field][this.index[offset]];
}
item[exports.DTINDEX_NAME] = this.index[offset];
return item;
};
DataProvider.prototype.getRecords = function () {
var _this = this;
return array_1.range(0, this.getLength()).map(function (i) { return _this.getItem(i); });
};
return DataProvider;
}());
function Counter(objects) {
var d = {};
for (var _i = 0, objects_1 = objects; _i < objects_1.length; _i++) {
var itm = objects_1[_i];
d[itm] = d[itm] ? d[itm] + 1 : 1;
}
return d;
}
function sortedCounter(objects) {
var d = Counter(objects);
var arr = Object.keys(d).map(function (k) { return [k, d[k]]; });
return arr.sort(function (a, b) { return b[1] - a[1]; });
}
function choose(choices) {
var index = Math.floor(Math.random() * choices.length);
return choices[index];
}
function choose_str(original_list) {
return choose(original_list);
}
var WordCloud2View = /** @class */ (function (_super) {
__extends(WordCloud2View, _super);
function WordCloud2View() {
return _super !== null && _super.apply(this, arguments) || this;
}
WordCloud2View.prototype.initialize = function () {
_super.prototype.initialize.call(this);
this.prepare();
};
WordCloud2View.prototype.prepare = function () {
var _this = this;
if (this.model.fontWeightFun && typeof this.model.fontWeightFun.execute === "function") {
this.model.fontWeight = function (word, weight, font_size) {
var data = { word: word, weight: weight, font_size: font_size };
return _this.model.fontWeightFun ? _this.model.fontWeightFun.execute(_this.model, data) : "normal";
};
}
if (this.model.classesFun && typeof this.model.classesFun.execute === "function") {
this.model.classes = function (word, weight, font_size) {
var data = { word: word, weight: weight, font_size: font_size };
return _this.model.classesFun ? _this.model.classesFun.execute(_this.model, data) : "normal";
};
}
if (this.model.colorsFun && typeof this.model.colorsFun.execute === "function") {
// its a callback
this.model.colors = function (word, weight, font_size, distance, theta) {
var data = { word: word, weight: weight, font_size: font_size, distance: distance, theta: theta };
return _this.model.colorsFun ? _this.model.colorsFun.execute(_this.model, data) : "pink";
};
}
else if (typeof this.model.colors === "object" && typeof this.model.colors.slice === "function") {
// its a list??
var original_list_1 = this.model.colors;
this.model.colors = function () { return choose_str(original_list_1); };
}
else if (typeof this.model.colors === "string") {
var col = this.model.source.get_column(this.model.colors);
if (col && col.length > 0) {
var data_1 = {};
var colors = this.model.source.get_column(this.model.colors);
var keys_1 = this.model.source.get_column(this.model.wordCol);
if (keys_1 && keys_1.length && colors && colors.length) {
for (var i = 0; i < keys_1.length; i++) {
if (keys_1[i] && colors[i]) {
data_1[keys_1[i]] = colors[i];
}
}
}
this.model.colors = function (word) { return data_1[word]; };
}
}
};
// private grid: SlickGrid
// update_data() {
// this.model.view.compute_indices()
// this.data.constructor(this.model.source, this.model.view)
// this.render()
// }
WordCloud2View.prototype.connect_signals = function () {
var _this = this;
_super.prototype.connect_signals.call(this);
// this.connect(this.model.change, () => this.render())
//
// this.connect(this.model.source.streaming, () => this.updateGrid())
// this.connect(this.model.source.patching, () => this.updateGrid())
this.connect(this.model.source.change, function () {
_this.prepare();
_this.render();
});
// this.connect(this.model.source.properties.data.change, () => this.updateGrid())
//
// this.connect(this.model.source.selected.change, () => this.updateSelection())
// this.connect(this.model.source.selected.properties.indices.change, () => this.updateSelection())
};
WordCloud2View.prototype.css_classes = function () {
return _super.prototype.css_classes.call(this).concat("bk-data-table");
};
WordCloud2View.prototype.get_sizes1 = function () {
var _this = this;
if (!this.model.sizeCol) {
var words_1 = [];
this.data.getRecords().map(function (record) {
var results = record[_this.model.wordCol].toUpperCase().match(/([A-Z]+)/);
words_1.push.apply(words_1, results);
});
return sortedCounter(words_1).slice(0, 50);
// console.log(sortedCounter(this.model.source.get_column(this.model.wordCol) as unknown as string[]));
}
var s = [];
this.data.getRecords().map(function (item) {
s.push([item[_this.model.wordCol], item[_this.model.sizeCol]]);
});
return s;
};
WordCloud2View.prototype.render = function () {
var _this = this;
_super.prototype.render.call(this);
this.data = new DataProvider(this.model.source, this.model.view);
var sizes = this.get_sizes1();
console.log("SIZES:", sizes);
var canvas = document.createElement("canvas");
canvas.width = this.model.width;
canvas.height = this.model.height;
// const colors: string[] = this.model.colors ? this.model.colors : [this.model.color,];
var opts = {
list: sizes,
fontFamily: 'Times, serif',
gridSize: Math.round(16 * this.model.width / 1024),
weightFactor: function (size) {
return Math.pow(size, 2.3) * _this.model.height / 1024;
},
color: this.model.colors,
rotateRatio: this.model.rotateRatio,
minRotation: this.model.minRotation,
maxRotation: this.model.maxRotation,
rotationSteps: this.model.rotationSteps,
shuffle: false,
backgroundColor: this.model.background,
drawOutOfBound: false,
classes: this.model.classes,
fontWeight: this.model.fontWeight,
shape: this.model.shape,
click: (!this.model.click) ? null : function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var source = new column_data_source_1.ColumnDataSource({ data: { word: [args[0][0]], weight: [args[0][1]] } });
source.selected.indices = [0]; // update our "selected" indices ...
var data = { word: args[0][0], weight: args[0][1], dimensions: args[1], event: args[2], source: source };
// trigger event to python
_this.model.trigger_event(new WordClickEvent(data.word, data.weight));
// trigger user js click handler
if (_this.model.click)
_this.model.click.execute(_this.model, data);
},
hover: (!this.model.hover) ? null : function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
if (_this.model.hover) {
var data = { word: args[0][0], weight: args[0][1], dimensions: args[1], event: args[2] };
_this.model.hover.execute(_this.model, data);
}
},
};
this.el.appendChild(canvas);
WordCloud(canvas, opts);
};
return WordCloud2View;
}(widget_1.WidgetView));
exports.WordCloud2View = WordCloud2View;
var WordCloud2 = /** @class */ (function (_super) {
__extends(WordCloud2, _super);
function WordCloud2(attrs) {
return _super.call(this, attrs) || this;
}
WordCloud2.initClass = function () {
this.prototype.type = 'WordCloud2';
this.prototype.default_view = WordCloud2View;
this.define({
// every property here should be defined in the namespace above
// and in the pyfile with exactly the same name
source: [p.Instance],
view: [p.Instance, function () { return new cds_view_1.CDSView(); }],
wordCol: [p.String],
sizeCol: [p.String],
colors: [p.Any, "blue"],
colorsFun: [p.Instance, null],
fontWeight: [p.String, "normal"],
fontWeightFun: [p.Instance, null],
classes: [p.String, ""],
classesFun: [p.Instance, null],
hover: [p.Instance, null],
click: [p.Instance, null],
rotateRatio: [p.Number, 1],
minRotation: [p.Number, 0],
maxRotation: [p.Number, Math.PI / 2],
rotationSteps: [p.Number, 32],
shape: [p.String, "square"]
});
this.override({
width: 600,
height: 400,
background: "#FFFFCC",
});
};
return WordCloud2;
}(widgets_1.Widget));
exports.WordCloud2 = WordCloud2;
WordCloud2.initClass();
}
}, "custom/main");
;
});
</script>
</head>
<body>
<div class="bk-root" id="3af7ef8e-7232-4a6c-a44a-e108e05e16a1" data-root-id="1005"></div>
<script type="application/json" id="1022">
{"32683524-3ebb-4d1e-a973-ba259babd0e5":{"roots":{"references":[{"attributes":{"callback":null,"data":{"titles":["lorem ipsum dolor sit amet","consectetur adipiscing elit","cras iaculis semper odio","eu posuere urna vulputate sed","quisque hendrerit","felis ac condimentum laoreet","nunc dolor bibendum lorem","a egestas velit eros ut dui","sed facilisis urna ultrices gravida maximus","morbi sed nisl fermentum","laoreet dui viverra","dignissim metus quisque lobortis eros placerat","imperdiet lobortis","nisi mi porttitor odio quis ultrices diam metus ac dolor","pellentesque tristique cursus nisl at pretium","sed lorem leo vulputate nec aliquet eu","tempor eu purus","ut nec ex pretium","pellentesque mi quis","ornare dolor","donec pulvinar cursus diam quis accumsan","fusce porta finibus nisl sed aliquet","nam aliquet","dolor id consectetur consectetur","mi sapien iaculis velit","sed gravida justo metus sed lorem","pellentesque varius libero eu orci tempor consectetur","suspendisse lacinia fringilla quam at hendrerit","cras eu laoreet enim","sed luctus","ligula dignissim dapibus maximus","ligula orci dictum elit","eget dictum dui lorem sed augue","proin orci purus","fermentum a feugiat non","pretium id eros","fusce mauris dui","elementum vel justo at","iaculis luctus ligula","sed eget velit cursus","posuere magna quis","tristique lectus","ut bibendum","tortor at porta sodales","mauris libero congue dui","eu consectetur urna augue a nunc","duis a odio ac quam imperdiet fermentum vitae nec elit","sed suscipit erat sed imperdiet posuere","ut imperdiet dolor non turpis porttitor efficitur","vestibulum porttitor at arcu sit amet auctor","nam congue erat quam","sed vulputate mauris bibendum in","pellentesque est quam","tempor at est eget","ullamcorper consequat dolor","duis facilisis ante lorem","id vehicula nisi venenatis sit amet","nunc iaculis a mauris eu molestie","aenean auctor eros nec dolor viverra","vitae placerat enim euismod","lorem ipsum dolor sit amet","consectetur adipiscing elit","sed neque nisi","vulputate non sollicitudin at","finibus tristique augue","ut neque purus","accumsan vestibulum urna sit amet","egestas imperdiet est","donec ut suscipit lorem","in hendrerit turpis","mauris et tincidunt elit","et molestie est","sed ut posuere sem","et molestie lacus","nullam metus lorem","fermentum quis lorem nec","lobortis dignissim velit","phasellus et nulla vitae dolor facilisis vulputate","phasellus accumsan est vel ex posuere","vitae tristique libero venenatis","pellentesque maximus dui eros","consequat pretium velit lacinia eu","mauris efficitur ac leo varius egestas","quisque tincidunt ornare arcu","in gravida lacus rhoncus sit amet","pellentesque elementum fringilla diam","etiam vulputate","est id porttitor sodales","ligula diam faucibus neque","vitae lobortis elit massa at odio","posuere ac urna","nam consectetur turpis quis metus malesuada pellentesque eget et urna","curabitur vitae enim quis ante luctus finibus","fusce accumsan nibh a lectus tincidunt","quis tristique sem laoreet","duis elit erat","congue eget scelerisque et","viverra ut est","sed vulputate mi id nisl mattis imperdiet","donec pharetra ultricies egestas","proin varius varius pretium","proin lacinia euismod mi","quis vestibulum orci finibus ut","donec ac nisl ut arcu pulvinar condimentum non eu velit","etiam id egestas arcu","donec ullamcorper at lacus ut porta","nulla nulla urna","aliquet vel iaculis sed","cras ut diam gravida","semper velit eget","convallis erat","nullam rhoncus nec mauris non dignissim","aliquam sed risus non justo luctus iaculis","fusce sed fringilla turpis","a sagittis metus","pellentesque blandit in augue ultricies fermentum","integer finibus congue sollicitudin","nunc consequat nisi est","non tempus massa tempus nec","nullam cursus orci a iaculis bibendum","praesent auctor lacus eget semper auctor","praesent nulla neque","scelerisque facilisis dignissim sed","porttitor sed purus","suspendisse placerat felis felis","at pellentesque turpis accumsan vitae","pellentesque ac blandit sapien","pellentesque volutpat finibus sem","vitae mollis ligula malesuada ut"]},"selected":{"id":"1007","type":"Selection"},"selection_policy":{"id":"1006","type":"UnionRenderers"}},"id":"1001","type":"ColumnDataSource"},{"attributes":{},"id":"1006","type":"UnionRenderers"},{"attributes":{},"id":"1007","type":"Selection"},{"attributes":{"classesFun":null,"click":null,"colors":["pink","blue","green"],"colorsFun":null,"fontWeightFun":null,"hover":null,"source":{"id":"1001","type":"ColumnDataSource"},"view":{"id":"1003","type":"CDSView"},"wordCol":"titles"},"id":"1002","type":"WordCloud2"},{"attributes":{"text":"\n<pre>\ntitles = ['lorem ipsum dolor sit amet',\n 'consectetur adipiscing elit',\n ... (ALOT MORE) ...\n 'cras iaculis semper odio',\n 'eu posuere urna vulputate sed']\ntest1 = ColumnDataSource({'titles':titles}) \nWordCloud2(source=test1,wordCol=\"titles\",colors=['pink','blue','green']) \n </pre>\n "},"id":"1004","type":"Div"},{"attributes":{"children":[{"id":"1002","type":"WordCloud2"},{"id":"1004","type":"Div"}]},"id":"1005","type":"Column"},{"attributes":{"source":{"id":"1001","type":"ColumnDataSource"}},"id":"1003","type":"CDSView"}],"root_ids":["1005"]},"title":"Bokeh Application","version":"1.2.0"}}
</script>
<script type="text/javascript">
(function() {
var fn = function() {
Bokeh.safely(function() {
(function(root) {
function embed_document(root) {
var docs_json = document.getElementById('1022').textContent;
var render_items = [{"docid":"32683524-3ebb-4d1e-a973-ba259babd0e5","roots":{"1005":"3af7ef8e-7232-4a6c-a44a-e108e05e16a1"}}];
root.Bokeh.embed.embed_items(docs_json, render_items);
}
if (root.Bokeh !== undefined) {
embed_document(root);
} else {
var attempts = 0;
var timer = setInterval(function(root) {
if (root.Bokeh !== undefined) {
embed_document(root);
clearInterval(timer);
}
attempts++;
if (attempts > 100) {
console.log("Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing");
clearInterval(timer);
}
}, 10, root)
}
})(window);
});
};
if (document.readyState != "loading") fn();
else document.addEventListener("DOMContentLoaded", fn);
})();
</script>
</body>
</html>