-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetParallelId.ts
119 lines (102 loc) · 3.07 KB
/
getParallelId.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
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
import { Database } from "https://deno.land/x/[email protected]/mod.ts";
const alignmentDb = new Database("alignment.sqlite");
const versification_schema_hierarchy = [
"kjv",
"bhs",
"gnt",
"lxx",
];
const alignments: Alignments = {};
const parallelIdMap: {
[parallelId: number]: {
[versificationSchema: string]: number;
};
} = {};
const alignmentsFromDb = alignmentDb.prepare(
`SELECT kjv, bhs, lxx, gnt FROM alignment`,
).all();
alignmentsFromDb.forEach((row, i) => {
const schemas = Object.keys(row).filter((vs) => row[vs]);
if (schemas.length === 1) {
// There's only one alignment, so we don't need to do anything
// console.log("Single alignment!", row);
return;
}
Object.keys(row).forEach((vs) => {
if (!row[vs]) return;
if (alignments?.[vs]?.[row[vs]]) {
console.log("Duplicate alignment!", row);
console.log(vs, row[vs]);
console.log(alignments[vs][row[vs]], i + 1);
return;
}
if (!(vs in alignments)) {
alignments[vs] = {};
}
alignments[vs][row[vs]] = i + 1;
if (!(i + 1 in parallelIdMap)) {
parallelIdMap[i + 1] = {};
}
parallelIdMap[i + 1][vs] = row[vs];
});
});
let nextParallelId = alignmentsFromDb.length;
const getParallelId = (
rid: number,
versification_schema: VersificationSchema,
) => {
if (
versification_schema in alignments &&
rid in alignments[versification_schema]
) {
return alignments[versification_schema][rid];
}
alignments[versification_schema][rid] = nextParallelId;
if (!(nextParallelId in parallelIdMap)) {
parallelIdMap[nextParallelId] = {};
}
parallelIdMap[nextParallelId][versification_schema] = rid;
nextParallelId++;
return alignments[versification_schema][rid];
};
const getComparableRidFromParallelId = (
parallelId: number,
schema: VersificationSchema,
) => {
if (schema in parallelIdMap[parallelId]) {
return parallelIdMap[parallelId][schema];
}
return versification_schema_hierarchy.reduce((acc, vs) => {
if (vs in parallelIdMap[parallelId] && !acc) {
return parallelIdMap[parallelId][vs];
}
return acc;
}, parallelIdMap[parallelId]?.[schema] ?? 0);
};
import sortBookIdsBySchema from "./sortBookIdsBySchema.ts";
const sortParallelIdsBySchema =
(schema: VersificationSchema) => (pidA: number, pidB: number) => {
if (!(schema in alignments)) {
throw new Error(`Invalid versification schema: ${schema}`);
}
const ridA = getComparableRidFromParallelId(pidA, schema);
const ridB = getComparableRidFromParallelId(pidB, schema);
// Use integer division to get the book id (1_000_000)
const bookA = Math.floor(ridA / 1_000_000);
const bookB = Math.floor(ridB / 1_000_000);
if (bookA !== bookB) {
return sortBookIdsBySchema(bookA, bookB, schema);
}
return ridA - ridB;
};
const getRidFromParallelIdAndSchema = (parallelId: number, schema: string) => {
if (!(schema in parallelIdMap[parallelId])) {
return null;
}
return parallelIdMap[parallelId][schema];
};
export {
getParallelId,
getRidFromParallelIdAndSchema,
sortParallelIdsBySchema,
};