-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitextmap.js
More file actions
412 lines (316 loc) · 10.4 KB
/
itextmap.js
File metadata and controls
412 lines (316 loc) · 10.4 KB
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
var heatData = [];
var heatLabels = [];
var heatIndices = [];
var labelCount = 0;
var distValue = 0;
var boundaries = [];
var clickLock = false;
var thisLabel = '';
var bundleMask = [];
var bundleCount = 0;
var xData;
var yData;
var xFilename;
var yFilename;
var sectionList;
var selectedSections = {};
var myPlot = document.getElementById('myDiv');
var clickInfo = document.getElementById('clickText');
var popupDiv = document.getElementById('infopane');
function parseLabels(label) {
var labelParts = String(label).split("_");
var thisSection = labelParts[0];
if (!(thisSection in selectedSections)) {
selectedSections[thisSection] = true;
}
if (!selectedSections[thisSection]) {
bundleMask.push(false);
return;
}
bundleMask.push(true);
if (thisLabel == '') {
thisLabel = thisSection;
} else if (thisLabel != thisSection) {
thisLabel = thisSection;
boundaries.push(labelCount);
}
heatLabels.push(label);
heatIndices.push(labelCount);
labelCount++;
}
function parseData(rowdata) {
if (bundleMask[bundleCount]) {
var newData = [];
console.log("looking at bundle " + bundleCount);
for (var i in rowdata) {
if (bundleMask[i]) {
newData.push(rowdata[i]);
}
}
heatData.push(newData);
}
bundleCount++;
}
function sectionClicked(cbox) {
var thisSection = cbox.id;
if (selectedSections[thisSection]) {
selectedSections[thisSection] = false;
} else {
selectedSections[thisSection] = true;
}
}
function showSections() {
sectionList = document.getElementById('sectionList');
var thisHTML = '<fieldset><div><strong>Sections</strong></div>';
for (var thisSection in selectedSections) {
var checkedString = "";
if (selectedSections[thisSection]) {
checkedString = " checked";
}
thisHTML += '<div class="checkbox"><input type="checkbox" id="' + thisSection + '" name="' + thisSection + '" onclick="sectionClicked(this)"' + checkedString + '><label for="' + thisSection + '">' + thisSection + '</label></div>';
}
thisHTML += "</fieldset>";
var reloadButton = document.createElement("BUTTON");
var t = document.createTextNode("Reload");
reloadButton.appendChild(t);
reloadButton.onclick = function(){loadData()};
var invertButton = document.createElement("BUTTON");
var u = document.createTextNode("Invert");
invertButton.appendChild(u);
invertButton.onclick = function(){invertSelection()};
sectionList.innerHTML = thisHTML;
sectionList.appendChild(reloadButton);
sectionList.appendChild(invertButton);
}
function invertSelection() {
var qsa = document.querySelectorAll("input[type=checkbox]"),
l = qsa.length, i;
for( var i=0; i<l; i++) {
var boxID = qsa[i].id;
if (selectedSections[boxID]) {
selectedSections[boxID] = false;
} else {
selectedSections[boxID] = true;
}
qsa[i].checked = !qsa[i].checked;
}
}
function processReuse(error, idata) {
console.log("processing text reuse data");
itextData = idata;
var totalMatches = 0;
for (var m in idata["docs"]) {
var match = idata["docs"][m];
var sourceTitle = match['source_title'];
var targetTitle = match['target_title'];
totalMatches += 1;
if (itextMatches.hasOwnProperty(sourceTitle)) {
if (itextMatches[sourceTitle].hasOwnProperty(targetTitle)) {
itextMatches[sourceTitle][targetTitle].push(match);
} else {
itextMatches[sourceTitle][targetTitle] = [match];
}
} else {
itextMatches[sourceTitle] = {};
itextMatches[sourceTitle][targetTitle] = [match];
}
if (itextMatches.hasOwnProperty(targetTitle)) {
if (itextMatches[targetTitle].hasOwnProperty(sourceTitle)) {
itextMatches[targetTitle][sourceTitle].push(match);
} else {
itextMatches[targetTitle][sourceTitle] = [match];
}
} else {
itextMatches[targetTitle] = {};
itextMatches[targetTitle][sourceTitle] = [match];
}
}
console.log("Registered " + totalMatches + " text reuse matches");
}
function loadData() {
heatData = [];
heatLabels = [];
heatIndices = [];
labelCount = 0;
distValue = 0;
boundaries = [];
clickLock = false;
thisLabel = '';
bundleMask = [];
bundleCount = 0;
clickInfo.innerHTML = "";
popupDiv.innerHTML = "";
myPlot.innerHTML = "<h3>Loading data. Please wait...</h3>";
console.log("Loading labels and data...");
d3.text("bin_labels.txt",
function (lerror, ltext) {
labelsData = d3.dsvFormat("\t").parseRows(ltext, parseLabels);
console.log("Loaded labels data, now loading texts");
showSections();
d3.text("itext_sim.txt",
function (error, text) {
hData = d3.dsvFormat("\t").parseRows(text, parseData);
console.log("Loaded matrix data, drawing map.");
drawHeatmap();
});
});
}
function initialize() {
// Could be set dynamically based on some value loaded from JSON files
//document.title = "Interactive text reuse heatmap";
//document.getElementById('pagetitle').innerHTML = "Interactive text reuse heatmap";
loadData();
}
function drawHeatmap() {
console.log("Drawing heatmap");
console.log("number of labels is " + heatLabels.length);
console.log("first label is " + heatLabels[0]);
console.log("size of data array is " + heatData.length);
var data = [{
x: heatLabels,
y: heatLabels,
z: heatData,
type: 'heatmap',
name: 'Text reuse heatmap',
hoverinfo: "none",
colorscale: 'Viridis',
reversescale: true,
zsmooth: "best",
zmax: .5,
zmin: .1,
showscale: true
}];
var layout = {
showlegend: false,
width: 800,
height: 800,
margin: {
l: 140,
r: 0,
t: 25,
b: 100},
autosize: true,
xaxis: {
type: 'category',
categoryorder: 'array',
categoryarray: heatLabels,
range: heatIndices,
ticks: '',
showspikes: true,
tick0: 0,
tickangle: -45
},
yaxis: {
type: 'category',
categoryorder: 'array',
categoryarray: heatLabels,
range: heatIndices,
ticks: '',
showspikes: true,
tick0: 0
},
shapes: []
};
var maxLine = heatData.length;
for (var b in boundaries) {
var boundary = parseInt(boundaries[b])-1;
/* Use this code to draw lines outlining the main sections of the text
* groupings, as determined by the first part of the filename */
// Horizontal lines
layout['shapes'].push({type: 'line', xref: 'x', yref: 'y', x0: 0, y0: boundary, x1: maxLine, y1: boundary, opacity: 0.9, line: {color: '#ff0000', width: 1, dash: 'dash' }});
// Vertical lines
// layout['shapes'].push({type: 'line', xref: 'x', yref: 'y', x0: boundary, y0: 0, x1: boundary, y1: maxLine, opacity: 0.9, line: {color: '#ff0000', width: 1, dash: 'dash', }});
}
myPlot.innerHTML = "";
Plotly.newPlot(myDiv, data, layout);
function processXtext(error, bdata){
xData = bdata;
queue() // queue function loads all external data files asynchronously
.defer(d3.json, "binsJSON/" + yFilename)
.await(processYtext); // once all files are loaded,
}
function processYtext(error, bdata) {
yData = bdata;
var clickHTML = '<h2>Comparison details</h2>';
hoverHTML = 'X axis (column) text bundle: ' + xData["label"] + '<br>';
hoverHTML += 'Y axis (row) text bundle: ' + yData["label"] + '<br>';
hoverHTML += '</p><p>Similarity between the text bundles (1=identical, 0=nothing in common):<br><b>' + distValue + '</b></p>';
clickHTML += '<h3><font color="#ff0000">Texts in bundle ' + xData["label"] + '</font></h3>';
clickHTML += textInfo(xData);
clickHTML += '<h3><font color="#ff0000">Texts in bundle ' + yData["label"] + '</font></h3>';
clickHTML += textInfo(yData);
hoverHTML += formatMatches(xData["matches"], yData["matches"]);
clickInfo.innerHTML = clickHTML;
popupDiv.innerHTML = hoverHTML;
}
function textInfo(data) {
var HTML = "";
for (var d in data['docsInBin']) {
var dID = data['docsInBin'][d];
var thisDoc = data['docs'][dID];
HTML += '<b>' + dID + ':</b> Title: ' + thisDoc['metadata']['title'] + ', Author: ' + thisDoc['metadata']['author'] + ', Year: ' + thisDoc['metadata']['publication_year'] + ' ';
HTML += '<b><a href="' + thisDoc['metadata']['url'] + '">[link]</a></b><br>';
}
return HTML;
}
function formatMatches(xMatches, yMatches) {
var matchHTML = "";
for (var docXID in xMatches) {
console.log("Checking matches for doc " + docXID);
if (xMatches.hasOwnProperty(docXID)) {
for (var docYID in xMatches[docXID]) {
console.log("Found match doc " + docYID);
if (xMatches[docXID].hasOwnProperty(docYID)) {
if (docYID in yMatches) {
if (matchHTML == "") {
matchHTML = "<h4>Matches found:</h4>";
}
console.log(xMatches[docXID][docYID]);
for (var m in xMatches[docXID][docYID]) {
var match = xMatches[docXID][docYID][m];
console.log("match: " + match);
matchHTML += '<p><b>' + match["source_title"] + ' -> ' + match["target_title"] + ', similarity: ' + match['similarity'] + '</b></p>';
matchHTML += '<p><b>' + match["source_title"] + ':</b> ' + match['source_prematch'] + ' <span style="background-color: #ffff00"><b>' + match['source_match'] + '</b></span> ' + match['source_postmatch'] + '</p>';
matchHTML += '<p><b>' + match["target_title"] + ':</b> ' + match['target_prematch'] + ' <span style="background-color: #ffff00"><b>' + match['target_match'] + '</b></span> ' + match['target_postmatch'] + '</p>';
matchHTML += '<p> </p>';
}
}
}
}
}
}
return matchHTML;
}
function textStats(data) {
var xVal = data.points[0].x;
var yVal = data.points[0].y;
var zVal = data.points[0].z;
distValue = zVal;
//console.log("Row: " + yVal + ", Column: " + xVal + ", Difference: " + zVal);
var xaxis = data.points[0].xaxis;
var yaxis = data.points[0].yaxis;
xFilename = xVal.toString() + '.json';
yFilename = yVal.toString() + '.json';
var distX = xaxis.d2p(data.points[0].x);
var distY = yaxis.d2p(data.points[0].y);
queue() // queue function loads all external data files asynchronously
.defer(d3.json, "binsJSON/" + xFilename)
.await(processXtext); // once all files are loaded,
}
myPlot.on('plotly_hover', function(data){
if (clickLock) {
return;
}
textStats(data);
});
myPlot.on('plotly_click', function(data){
if (!clickLock) {
clickLock = true;
} else {
clickLock = false;
}
textStats(data);
});
myPlot.on('plotly_unhover', function(data){
});
}