-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
89 lines (79 loc) · 2.22 KB
/
index.ts
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
import fs from "node:fs";
import { parse } from "csv-parse";
import FuzzySet from "fuzzyset";
import { z } from "zod";
const __dirname = new URL(".", import.meta.url).pathname;
const Row = z.object({
c1: z.string(),
c2: z.string(),
c3: z.string(),
c4: z.string(),
c5: z.string(),
});
const processFile = async () => {
const records = [];
const parser = fs.createReadStream(`${__dirname}/input.csv`).pipe(parse());
for await (const [c1, c2, c3, c4, c5] of parser) {
const record = Row.parse({
c1,
c2,
c3,
c4,
c5,
});
records.push(record);
}
return records;
};
function toCSV(data: any[]) {
const csvRows = [];
// Get the headers
const headers = Object.keys(data[0]);
csvRows.push(headers.join(","));
// Map the data to CSV format
for (const row of data) {
const values = headers.map((header) => {
const escaped = ("" + row[header]).replace(/"/g, '\\"');
return `"${escaped}"`;
});
csvRows.push(values.join(","));
}
const csv = csvRows.join("\n");
fs.writeFileSync("output.csv", csv);
}
(async () => {
const records = await processFile();
const results = [];
for (const record of records) {
const result = {
...record,
c1_c2: compareStrings(record.c1, record.c2),
c1_c3: compareStrings(record.c1, record.c3),
c1_c4: compareStrings(record.c1, record.c4),
c1_c5: compareStrings(record.c1, record.c5),
c2_c3: compareStrings(record.c2, record.c3),
c2_c4: compareStrings(record.c2, record.c4),
c2_c5: compareStrings(record.c2, record.c5),
c3_c4: compareStrings(record.c3, record.c4),
c3_c5: compareStrings(record.c3, record.c5),
c4_c5: compareStrings(record.c4, record.c5),
};
results.push(result);
}
toCSV(results);
})();
function compareStrings(aString: string, bString: string) {
const a = aString.split(",").map((s) => s.trim());
const b = bString.split(",").map((s) => s.trim());
const aSet = FuzzySet(a);
const results = b
.map((s) => {
const res = aSet.get(s);
return res ? res[0][0] : 0;
})
.sort((a, b) => b - a);
return toPercentage(results[0]);
}
function toPercentage(value: number): string {
return `${(value * 100).toFixed(0)}`;
}