-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtext-processing.js
44 lines (32 loc) · 1.03 KB
/
text-processing.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
import { removeStopWords, toLoweCase, removePunctuations, split } from "text-ai";
function process (input, processors, callback) {
if (!input || typeof input !== "string") {
return setTimeout(() => callback(new Error("Invalid input argument")));
}
if (!processors || !Array.isArray(processors) || processors.length === 0) {
return setTimeout(() => callback(new Error("Invalid processors argument")));
}
function iterate (index) {
if (index === processors.length) {
return callback(null, input);
}
const processor = processors[index];
processor(input, (error, output) => {
if (error) {
return callback(error);
}
input = output;
iterate(index + 1);
});
}
iterate(0);
}
const processors = [removeStopWords, removePunctuations, toLoweCase, split];
process("The world of coding is amazing!", processors, (error, result) => {
if (error) {
return console.error(error);
}
console.log(result);
});
// Async output:
// ["world", "coding", "is", "amazing"]