forked from SecurityEssentials/Teams2NVivo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.html
458 lines (383 loc) · 16.3 KB
/
converter.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
---
layout: default
---
<!DOCTYPE html>
<!-- Single web page implementation of a VTT Transcript Converter -->
<html>
<head>
<title>Teams Transcript Converter for Atlas.ti</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- ****************************************** Stylesheet *********************************************** -->
<style>
footer,
#wordsDeleted {
font-style: italic;
}
#interactions {
line-height: 2.5;
}
#unitTestResult {
color: red;
}
</style>
<!-- ****************************************** Javascript *********************************************** -->
<script type="text/javascript">
function chatTranscriptChosenEvent(event) {
const input = event.target;
const filename = document.getElementById("fileName");
filename.value = input.files[0].name;
const reader = new FileReader();
reader.onload = function () {
const text = reader.result;
const node = document.getElementById("chatInputVTT");
node.value = text;
};
reader.readAsText(input.files[0]);
checkTimeshift();
}
function streamTranscriptChosenEvent(event) {
const input = event.target;
const reader = new FileReader();
reader.onload = function () {
const text = reader.result;
const firstTimestamp = text.match(
/([0-9]*):([0-9]*):([0-9]*).([0-9]*) --> /
);
const hh = firstTimestamp[1];
const mm = firstTimestamp[2];
const ss = firstTimestamp[3];
const ms = firstTimestamp[4];
const timeshift =
parseInt(hh) * 3600000 +
parseInt(mm) * 60000 +
parseInt(ss) * 1000 +
parseInt(ms);
const timeshiftInput = document.getElementById("timeshift");
timeshiftInput.value = timeshift;
};
reader.readAsText(input.files[0]);
checkTimeshift();
}
function shiftedTimestamp(timeshift, hh, mm, ss, ms) {
let timeInMs =
parseInt(hh) * 3600000 +
parseInt(mm) * 60000 +
parseInt(ss) * 1000 +
parseInt(ms);
timeInMs += timeshift;
// Mark negative timestamps for removal.
if (timeInMs < 0) {
return "--:--:--";
}
// ~~ forces integer arithmetic. PadStart zero-pads the string:
return (
(~~(timeInMs / 3600000)).toString().padStart(2, "0") +
":" +
(~~((timeInMs % 3600000) / 60000)).toString().padStart(2, "0") +
":" +
(~~((timeInMs % 60000) / 1000)).toString().padStart(2, "0") +
"." +
(timeInMs % 1000).toString().padStart(3, "0")
);
}
function processInput(input, timeshiftInMs, shouldDeleteShort) {
// Does the conversion, adding timeshift to each timestamp (may be negative)
// If shouldDeleteShort, then removes short phrases under 10 characters.
const AllWordsDeleted = {};
let output = input
// Remove notes (e.g., level of confidence in auto transcription)
.replace(/NOTE .*?\n/g, "")
// Remove cue ids (they're completely optional)
.replace(/[a-z0-9]*-[a-z0-9]*-[a-z0-9]*-[a-z0-9]*-[a-z0-9]*\n/g, "")
// Atlas.ti breaks when timestamps aren't zero-padded, so this both adds padding
// and offsets by specified timeshift
.replace(
/([0-9]*):([0-9]*):([0-9]*).([0-9]*) --> ([0-9]*):([0-9]*):([0-9]*).([0-9]*)/g,
(match, hh1, mm1, ss1, ms1, hh2, mm2, ss2, ms2) => {
const timestamp1 = shiftedTimestamp(
timeshiftInMs,
hh1,
mm1,
ss1,
ms1
);
const timestamp2 = shiftedTimestamp(
timeshiftInMs,
hh2,
mm2,
ss2,
ms2
);
return timestamp1 + " --> " + timestamp2;
}
)
// Remove items with negative timestamps:
.replace(
/.*(?<time>--:--:--).*\n<v (?<speaker>.*?)>(?<text>.*?)<\/v>\n/g,
""
);
if (shouldDeleteShort) {
// Now look for short interjections and remove them:
output = output.replace(
/(?<time>\d\d:\d\d:\d\d.\d\d\d).*\n<v (?<speaker>.*?)>(?<text>[\S ]{0,6}?)<\/v>\n/g,
(line, time, speaker, text) => {
// Remembers the words from line that will be removed.
for (const i of text.split(/\W+/).filter((s) => s)) {
// Split third item ignoring anything that's not alphanumeric.
AllWordsDeleted[i.toLowerCase()] = true;
}
return "";
}
);
}
output = output
// Atlas.ti doesn't support voice tags, so reformat them to be a bit nicer
.replace(/<v (.+)>(.+)<\/v>/g, "$1: $2")
// Collapse consecutive blank lines
.replace(/\r?\n[\r\n]+/g, "\n\n")
// And finally remove whitespace at end of file for consistency
.replace(/\s+$/, "");
document.getElementById("wordsDeleted").innerHTML =
Object.keys(AllWordsDeleted).join(" "); // Safe, since all non-alphanum filtered out in WordsDeleted().
return output;
}
function cleanVTT() {
const inputData = document.getElementById("chatInputVTT").value;
const outputElement = document.getElementById("outputVTT");
const outputFilename = document.getElementById("fileName").value;
const timeshiftInMs = parseInt(
document.getElementById("timeshift").value
);
const shouldDeleteShort =
document.getElementById("deleteShort").checked;
outputElement.value = processInput(
inputData,
timeshiftInMs,
shouldDeleteShort
);
download(outputFilename);
}
function download(filename) {
// Saves the contents of element 'output' to file with provided filename
var input = document.getElementById("outputVTT").value;
var element = document.createElement("a");
element.setAttribute(
"href",
"data:text/vtt;charset=utf-8," + encodeURIComponent(input)
);
element.setAttribute("download", filename);
element.style.display = "none";
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function checkTimeshift() {
// Disables the Convert and Download button if there's no input file or the timeshift is invalid.
document.getElementById("doConvert").disabled = !(
document.getElementById("timeshift").value.match(/^-?\d+$/) &&
document.getElementById("fileName").value.length != 0
);
}
</script>
</head>
<!-- ****************************************** HTML body *********************************************** -->
<body>
<!-- Converter symbol in SVG -->
<img
src="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTUiIGhlaWdodD0iMTUiIHZpZXdCb3g9IjAgMCAxNSAxNSIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICAgIDxwYXRoIGQ9Ik03LjA5Mzc1IDEuNDA2MjVDNy4wOTM3NSAxLjYyOTY5IDcuMjc2NTYgMS44MTI1IDcuNSAxLjgxMjVDOS4wMjM0NCAxLjgxMjUgMTAuNDQ1MyAyLjQwMTU2IDExLjUyMTkgMy40NzgxMkMxMi41OTg0IDQuNTU0NjkgMTMuMTg3NSA1Ljk3NjU2IDEzLjE4NzUgNy41QzEzLjE4NzUgOS4wMDMxMyAxMi42MTg4IDEwLjQwNDcgMTEuNTYyNSAxMS40ODEyVjEwLjM0MzhDMTEuNTYyNSAxMC4xMjAzIDExLjM3OTcgOS45Mzc1IDExLjE1NjIgOS45Mzc1QzEwLjkzMjggOS45Mzc1IDEwLjc1IDEwLjEyMDMgMTAuNzUgMTAuMzQzOFYxMi43ODEySDEzLjE4NzVDMTMu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gNC4yNSAxIDUuODE0MDYgMSA3LjVDMSA5LjIyNjU2IDEuNjcwMzEgMTAuODcxOSAyLjkwOTM3IDEyLjA5MDZaIiBmaWxsPSIjMjgzOTUyIiBzdHJva2U9IiMyODM5NTIiLz4KPC9zdmc+Cg=="
width="8%"
/>
<h3>Transcript Converter</h3>
<div>
<p>
This page will transform a WebVTT transcript file from Microsoft Teams
to be ready for import into Atlas.ti
</p>
<p>
Choose a file from your computer using Choose file. This should be a
text based file with a .vtt extension as downloaded from the Teams chat.
</p>
<p>See <a href="index.html">the main page</a> for full instructions.</p>
<p>
If you want to adjust the transcript timestamps, you may either specify
a time shift value in milliseconds manually (which will then be added to
each timestamp; this value can be negative) or to upload another
transcript file from Microsoft Stream, which will pick up the time shift
value automatically. You can also choose to filter out phrases of 6 or
fewer characters. Then click Convert and Download to process your
transcript(s) and save the transformed output file.
</p>
</div>
<div id="interactions">
<form name="data">
<input
type="file"
id="myFile"
single
size="50"
accept=".vtt"
onchange="chatTranscriptChosenEvent(event)"
/>
<input type="hidden" id="fileName" /><br />
<label for="timeshift">Manual time shift (in ms):</label>
<input
type="text"
id="timeshift"
name="timeshift"
value="0"
onChange="checkTimeshift()"
/>
<span> OR </span>
<label for="autoTimeshift"
>Get time shift from MS Stream VTT file:</label
>
<input
type="file"
id="autoTimeshift"
name="autoTimeshift"
single
size="50"
accept=".vtt"
onchange="streamTranscriptChosenEvent(event)"
/><br />
<label for="deleteShort"
>Filter out short (<= 6 chars) phrases:</label
>
<input
type="checkbox"
id="deleteShort"
name="deleteShort"
value="false"
/><br />
<input
type="button"
id="doConvert"
onclick="cleanVTT()"
disabled
value="Convert and Download"
/>
</form>
</div>
<!-- Dummy textareas to hold the input and output file contents -->
<div id="inputDiv" style="display: none">
<p>Input data</p>
<textarea id="chatInputVTT"></textarea>
</div>
<div id="outputDiv" style="display: none">
<p>Output data</p>
<textarea id="outputVTT"></textarea>
</div>
<p>Words filtered: <span id="wordsDeleted" /></p>
<p id="unitTestResult"></p>
<p>
I only expect this tool to be useful temporarily, so I don't plan to
maintain the project, but if you'd like to improve or extend it, pull
requests are welcome.
</p>
<!-- Footer with copyright etc. -->
<footer id="footer">
<p>
Copyright (c) 2022 Vidminas Mikucionis<br />
Portions Copyright (c) 2021 Charles Weir <br />
Portions Copyright (c) 2020 Tim Ellis
</p>
<p>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
</p>
<p>
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
</p>
<p>
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</p>
</footer>
</body>
<!-- ************************************ Unit Tests ****************************** -->
<script type="text/javascript">
// This script runs unit tests on page load, and displays an error message if they fail.
function findError(actual, expected) {
// Answers an error message with the location of the error and problem.
actuals = actual.split("\n");
expecteds = expected.split("\n");
console.log(actual);
console.log(expected);
for (var i = 0; i < Math.min(actuals.length, expecteds.length); i++) {
if (!(actuals[i] === expecteds[i])) break;
}
return (
"output line " +
(i + 1).toString() +
': expected "' +
expecteds[i] +
'", got "' +
actuals[i] +
'"'
);
}
const testInput = `WEBVTT
NOTE Confidence: 0.648119294
53118acb-c442-4300-a3e9-4f29e5413f84
00:00:02.160 --> 00:00:06.560
Non-speaker captions from Microsoft Stream
NOTE Confidence: 0.648119294
7e46607a-d03d-44be-bbc5-6c0a9c898ec9
00:00:06.560 --> 00:00:07.967
Times don't matter for testing.
NOTE Transcripts with speaker from Microsoft Teams
0:0:0.000 --> 0:0:06.110
<v Joe>Captions from Teams. Before start.</v>
0:0:01.000 --> 0:0:06.110
<v Fred>I'm sorry if cats meows happened throughout this.</v>
0:0:05.000 --> 0:0:06.110
<v Joe>Err.</v>
0:0:06.450 --> 0:0:06.990
<v Joe>Yes.</v>
`;
const expectedOutput = `WEBVTT
00:00:01.160 --> 00:00:05.560
Non-speaker captions from Microsoft Stream
00:00:05.560 --> 00:00:06.967
Times don't matter for testing.
00:00:00.000 --> 00:00:05.110
Fred: I'm sorry if cats meows happened throughout this.`;
expectedNonDeleteShortOutput = `WEBVTT
00:00:02.160 --> 00:00:06.560
Non-speaker captions from Microsoft Stream
00:00:06.560 --> 00:00:07.967
Times don't matter for testing.
00:00:00.000 --> 00:00:06.110
Joe: Captions from Teams. Before start.
00:00:01.000 --> 00:00:06.110
Fred: I'm sorry if cats meows happened throughout this.
00:00:05.000 --> 00:00:06.110
Joe: Err.
00:00:06.450 --> 00:00:06.990
Joe: Yes.`;
deleteShortOutput = processInput(testInput, -1000, true);
nonDeleteShortOutput = processInput(testInput, 0, false);
document.getElementById("unitTestResult").innerHTML =
deleteShortOutput === expectedOutput &&
nonDeleteShortOutput === expectedNonDeleteShortOutput
? ""
: "Self tests failed: " +
(deleteShortOutput === expectedOutput
? "ND " +
findError(nonDeleteShortOutput, expectedNonDeleteShortOutput)
: findError(deleteShortOutput, expectedOutput)) +
"<br> Please notify developers.";
</script>
</html>