-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
52 lines (46 loc) · 1.13 KB
/
utils.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
import fs from 'fs';
import path from 'path';
function getAllFiles(directoryPath, filetype) {
const files = fs.readdirSync(directoryPath).filter(file => file.endsWith(filetype));
return files.map(file => path.join(directoryPath, file));
}
function getRandomSample(array, n) {
let result = [];
while (result.length < n) {
const index = Math.floor(Math.random() * array.length);
if (!result.includes(array[index])) {
result.push(array[index]);
}
}
return result;
}
function parseChat(chatText) {
const messages = [];
const lines = chatText.split("\n");
for (let line of lines) {
line = line.trim();
if (line.includes(":")) {
const [speaker, message] = line.split(":", 2).map((s) => s.trim());
messages.push({ speaker, message });
}
}
return messages;
}
function parseLines(text) {
const textLines = text.trim().split("\n");
let lines = [];
for (let line of textLines) {
line = line.trim();
line = line.replace(/^\d+\.\s*/, '');
if (line.length > 0) {
lines.push(line);
}
}
return lines;
}
export {
getAllFiles,
getRandomSample,
parseChat,
parseLines
}