sovana-solver 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/data/public_rules_50.json +1379 -0
- package/data/terminology_vi.json +235 -0
- package/dist/core.d.ts +6 -0
- package/dist/core.d.ts.map +1 -0
- package/dist/core.js +6 -0
- package/dist/core.js.map +1 -0
- package/dist/dag.d.ts +4 -0
- package/dist/dag.d.ts.map +1 -0
- package/dist/dag.js +820 -0
- package/dist/dag.js.map +1 -0
- package/dist/data.browser.d.ts +16 -0
- package/dist/data.browser.d.ts.map +1 -0
- package/dist/data.browser.js +80 -0
- package/dist/data.browser.js.map +1 -0
- package/dist/data.d.ts +16 -0
- package/dist/data.d.ts.map +1 -0
- package/dist/data.js +61 -0
- package/dist/data.js.map +1 -0
- package/dist/hopban.d.ts +4 -0
- package/dist/hopban.d.ts.map +1 -0
- package/dist/hopban.js +435 -0
- package/dist/hopban.js.map +1 -0
- package/dist/index.browser.d.ts +3 -0
- package/dist/index.browser.d.ts.map +1 -0
- package/dist/index.browser.js +3 -0
- package/dist/index.browser.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/rectify.d.ts +21 -0
- package/dist/rectify.d.ts.map +1 -0
- package/dist/rectify.js +1128 -0
- package/dist/rectify.js.map +1 -0
- package/dist/store.d.ts +9 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +17 -0
- package/dist/store.js.map +1 -0
- package/dist/types.d.ts +389 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +77 -3
- package/README.md +0 -3
package/dist/rectify.js
ADDED
|
@@ -0,0 +1,1128 @@
|
|
|
1
|
+
import { BRANCHES_VI, floorMod } from 'sovana-geo-time';
|
|
2
|
+
const MICRO_SCALE = 1_000_000;
|
|
3
|
+
const MICRO_SCALE_BIG = 1000000n;
|
|
4
|
+
const SURVIVING_THRESHOLD_MICRO = 50_000;
|
|
5
|
+
const LOCK_PROB_THRESHOLD_MICRO = 800_000;
|
|
6
|
+
const LOCK_RATIO_MULTIPLIER = 3;
|
|
7
|
+
const ALLOWED_CATEGORIES = new Set([
|
|
8
|
+
'RETRODICTIVE_TIMELINE',
|
|
9
|
+
'SOMATIC_MEDICAL',
|
|
10
|
+
'KINSHIP_TOPOLOGY',
|
|
11
|
+
]);
|
|
12
|
+
function cmpStr(a, b) {
|
|
13
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
14
|
+
}
|
|
15
|
+
function jsRound(x) {
|
|
16
|
+
return Math.floor(x + 0.5);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Exact zero-float Largest Remainder (Hamilton) normalization in pure BigInt arithmetic.
|
|
20
|
+
* Guarantees TS and Python produce 100% identical integer micro-probabilities summing to 1_000_000.
|
|
21
|
+
*/
|
|
22
|
+
export function normalizeMicroProbs(weights) {
|
|
23
|
+
const n = weights.length;
|
|
24
|
+
if (n === 0) {
|
|
25
|
+
throw new Error('Cannot normalize empty probability weight vector');
|
|
26
|
+
}
|
|
27
|
+
const bigWeights = weights.map((w) => {
|
|
28
|
+
const bw = typeof w === 'bigint' ? w : BigInt(Math.max(0, Math.trunc(w)));
|
|
29
|
+
return bw < 0n ? 0n : bw;
|
|
30
|
+
});
|
|
31
|
+
let sum = 0n;
|
|
32
|
+
for (const bw of bigWeights) {
|
|
33
|
+
sum += bw;
|
|
34
|
+
}
|
|
35
|
+
const effectiveWeights = sum === 0n ? bigWeights.map(() => 1n) : bigWeights;
|
|
36
|
+
const effectiveSum = sum === 0n ? BigInt(n) : sum;
|
|
37
|
+
const quotients = new Array(n);
|
|
38
|
+
const remainders = [];
|
|
39
|
+
let qSum = 0;
|
|
40
|
+
for (let i = 0; i < n; i++) {
|
|
41
|
+
const scaled = effectiveWeights[i] * MICRO_SCALE_BIG;
|
|
42
|
+
const q = Number(scaled / effectiveSum);
|
|
43
|
+
const r = scaled % effectiveSum;
|
|
44
|
+
quotients[i] = q;
|
|
45
|
+
qSum += q;
|
|
46
|
+
remainders.push({ idx: i, rem: r });
|
|
47
|
+
}
|
|
48
|
+
const deficit = MICRO_SCALE - qSum;
|
|
49
|
+
remainders.sort((a, b) => (a.rem > b.rem ? -1 : a.rem < b.rem ? 1 : a.idx - b.idx));
|
|
50
|
+
for (let k = 0; k < deficit; k++) {
|
|
51
|
+
const target = remainders[k];
|
|
52
|
+
quotients[target.idx] = quotients[target.idx] + 1;
|
|
53
|
+
}
|
|
54
|
+
return quotients;
|
|
55
|
+
}
|
|
56
|
+
function entropyBitsFloat(probsMicro) {
|
|
57
|
+
let h = 0.0;
|
|
58
|
+
for (const p of probsMicro) {
|
|
59
|
+
if (p > 0) {
|
|
60
|
+
const ratio = p / MICRO_SCALE;
|
|
61
|
+
h -= ratio * Math.log2(ratio);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return h < 0 ? 0 : h;
|
|
65
|
+
}
|
|
66
|
+
export function computeEntropyMillibits(probsMicro) {
|
|
67
|
+
return jsRound(entropyBitsFloat(probsMicro) * 1000.0);
|
|
68
|
+
}
|
|
69
|
+
export const PUBLIC_RECTIFICATION_QUESTIONS = [
|
|
70
|
+
{
|
|
71
|
+
id: 'Q-01',
|
|
72
|
+
category: 'SOMATIC_MEDICAL',
|
|
73
|
+
minAge: 0,
|
|
74
|
+
maxAge: 120,
|
|
75
|
+
ageCohort: null,
|
|
76
|
+
promptVi: 'Vào thời thơ ấu (khi chưa bị ép giờ đi học), nhịp tỉnh táo tự nhiên nhất trong ngày của bạn nghiêng về buổi nào?',
|
|
77
|
+
options: [
|
|
78
|
+
{ id: 'OPT_DAWN_MORN', labelVi: 'Dậy sớm từ rạng sáng và tỉnh táo nhất suốt buổi sáng' },
|
|
79
|
+
{ id: 'OPT_NOON_DUSK', labelVi: 'Tỉnh táo mạnh mẽ nhất từ trưa cho tới chiều muộn' },
|
|
80
|
+
{ id: 'OPT_NIGHT_LATE', labelVi: 'Tỉnh táo rõ nhất vào buổi tối khuya đến nửa đêm' },
|
|
81
|
+
{ id: 'OPT_NA', labelVi: 'Không rõ / Đồng đều mọi buổi trong ngày' },
|
|
82
|
+
],
|
|
83
|
+
likelihoodMatrixMicro: {
|
|
84
|
+
OPT_DAWN_MORN: [60000, 60000, 780000, 780000, 780000, 780000, 60000, 60000, 60000, 60000, 60000, 60000],
|
|
85
|
+
OPT_NOON_DUSK: [60000, 60000, 60000, 60000, 60000, 60000, 780000, 780000, 780000, 780000, 60000, 60000],
|
|
86
|
+
OPT_NIGHT_LATE: [780000, 780000, 60000, 60000, 60000, 60000, 60000, 60000, 60000, 60000, 780000, 780000],
|
|
87
|
+
OPT_NA: [100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000],
|
|
88
|
+
},
|
|
89
|
+
chartPredicates: {
|
|
90
|
+
OPT_DAWN_MORN: { op: 'shichen_in', branches: [2, 3, 4, 5] },
|
|
91
|
+
OPT_NOON_DUSK: { op: 'shichen_in', branches: [6, 7, 8, 9] },
|
|
92
|
+
OPT_NIGHT_LATE: { op: 'shichen_in', branches: [10, 11, 0, 1] },
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
id: 'Q-02',
|
|
97
|
+
category: 'KINSHIP_TOPOLOGY',
|
|
98
|
+
minAge: 0,
|
|
99
|
+
maxAge: 120,
|
|
100
|
+
ageCohort: null,
|
|
101
|
+
promptVi: 'Cấu trúc thực tế giữa bạn và anh chị em ruột trong gia đình khớp với tình huống nào nhất?',
|
|
102
|
+
options: [
|
|
103
|
+
{ id: 'OPT_THUY_CUC', labelVi: 'Là con một, hoặc con trưởng sống tách tự lập sớm xa anh em' },
|
|
104
|
+
{ id: 'OPT_KIM_CUC', labelVi: 'Anh chị em cách tuổi nhau rõ (từ 4 năm trở lên) và ít ở chung dài ngày' },
|
|
105
|
+
{ id: 'OPT_HOA_CUC', labelVi: 'Có anh chị em gần tuổi (cách 1–3 năm) và bạn thường gánh vác việc chung' },
|
|
106
|
+
{ id: 'OPT_MOC_CUC', labelVi: 'Gia đình đông anh chị em (từ 3 người trở lên) gắn bó hỗ trợ nhau' },
|
|
107
|
+
{ id: 'OPT_NA', labelVi: 'Không rõ / Không thuộc các mẫu trên' },
|
|
108
|
+
],
|
|
109
|
+
likelihoodMatrixMicro: {
|
|
110
|
+
OPT_THUY_CUC: [790000, 40000, 40000, 40000, 790000, 40000, 40000, 40000, 790000, 40000, 40000, 40000],
|
|
111
|
+
OPT_KIM_CUC: [40000, 790000, 40000, 40000, 40000, 790000, 40000, 40000, 40000, 790000, 40000, 40000],
|
|
112
|
+
OPT_HOA_CUC: [40000, 40000, 790000, 40000, 40000, 40000, 790000, 40000, 40000, 40000, 790000, 40000],
|
|
113
|
+
OPT_MOC_CUC: [40000, 40000, 40000, 790000, 40000, 40000, 40000, 790000, 40000, 40000, 40000, 790000],
|
|
114
|
+
OPT_NA: [90000, 90000, 90000, 90000, 90000, 90000, 90000, 90000, 90000, 90000, 90000, 90000],
|
|
115
|
+
},
|
|
116
|
+
chartPredicates: {
|
|
117
|
+
OPT_THUY_CUC: {
|
|
118
|
+
op: 'or',
|
|
119
|
+
args: [
|
|
120
|
+
{ op: 'cuc_in', cucNumbers: [2] },
|
|
121
|
+
{ op: 'palace_has_any_star', palace: 'Huynh Đệ', stars: ['Cô Thần', 'Quả Tú', 'Đà La'] },
|
|
122
|
+
],
|
|
123
|
+
},
|
|
124
|
+
OPT_KIM_CUC: {
|
|
125
|
+
op: 'or',
|
|
126
|
+
args: [
|
|
127
|
+
{ op: 'cuc_in', cucNumbers: [4] },
|
|
128
|
+
{ op: 'palace_has_any_star', palace: 'Huynh Đệ', stars: ['Thất Sát', 'Kình Dương', 'Vũ Khúc'] },
|
|
129
|
+
],
|
|
130
|
+
},
|
|
131
|
+
OPT_HOA_CUC: {
|
|
132
|
+
op: 'or',
|
|
133
|
+
args: [
|
|
134
|
+
{ op: 'cuc_in', cucNumbers: [6] },
|
|
135
|
+
{ op: 'palace_has_any_star', palace: 'Huynh Đệ', stars: ['Thái Dương', 'Thiên Tướng'] },
|
|
136
|
+
{ op: 'palace_has_sihua', palace: 'Huynh Đệ', transforms: ['Hóa Quyền'] },
|
|
137
|
+
],
|
|
138
|
+
},
|
|
139
|
+
OPT_MOC_CUC: {
|
|
140
|
+
op: 'or',
|
|
141
|
+
args: [
|
|
142
|
+
{ op: 'cuc_in', cucNumbers: [3, 5] },
|
|
143
|
+
{
|
|
144
|
+
op: 'palace_has_any_star',
|
|
145
|
+
palace: 'Huynh Đệ',
|
|
146
|
+
stars: ['Thiên Phủ', 'Thái Âm', 'Thiên Đồng', 'Tả Phụ', 'Hữu Bật'],
|
|
147
|
+
},
|
|
148
|
+
],
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
id: 'Q-03',
|
|
154
|
+
category: 'RETRODICTIVE_TIMELINE',
|
|
155
|
+
minAge: 0,
|
|
156
|
+
maxAge: 120,
|
|
157
|
+
ageCohort: null,
|
|
158
|
+
promptVi: 'Trong 18 năm đầu đời, nơi ở và môi trường học tập của gia đình bạn diễn ra theo xu hướng nào?',
|
|
159
|
+
options: [
|
|
160
|
+
{ id: 'OPT_TU_CHINH', labelVi: 'Ở ổn định một địa phương, nhưng có một bước ngoặt đổi hướng rất dứt khoát' },
|
|
161
|
+
{ id: 'OPT_TU_MO', labelVi: 'Gắn bó lâu dài tại nhà cũ hoặc đất tổ tiên, hầu như không dời đổi chỗ ở' },
|
|
162
|
+
{ id: 'OPT_TU_SINH', labelVi: 'Gia đình từng chuyển nhà nhiều lần hoặc bạn sớm rời quê đi học xa' },
|
|
163
|
+
{ id: 'OPT_NA', labelVi: 'Không rõ / Thông tin trung tính' },
|
|
164
|
+
],
|
|
165
|
+
likelihoodMatrixMicro: {
|
|
166
|
+
OPT_TU_CHINH: [780000, 60000, 60000, 780000, 60000, 60000, 780000, 60000, 60000, 780000, 60000, 60000],
|
|
167
|
+
OPT_TU_MO: [60000, 780000, 60000, 60000, 780000, 60000, 60000, 780000, 60000, 60000, 780000, 60000],
|
|
168
|
+
OPT_TU_SINH: [60000, 60000, 780000, 60000, 60000, 780000, 60000, 60000, 780000, 60000, 60000, 780000],
|
|
169
|
+
OPT_NA: [100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000],
|
|
170
|
+
},
|
|
171
|
+
chartPredicates: {
|
|
172
|
+
OPT_TU_CHINH: { op: 'menh_branch_group', group: 'TU_CHINH' },
|
|
173
|
+
OPT_TU_MO: { op: 'menh_branch_group', group: 'TU_MO' },
|
|
174
|
+
OPT_TU_SINH: { op: 'menh_branch_group', group: 'TU_SINH' },
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
id: 'Q-04',
|
|
179
|
+
category: 'SOMATIC_MEDICAL',
|
|
180
|
+
minAge: 0,
|
|
181
|
+
maxAge: 120,
|
|
182
|
+
ageCohort: null,
|
|
183
|
+
promptVi: 'Nếu từng có vết sẹo nhỏ, chấn thương nhẹ hoặc bên cơ thể dễ mỏi hơn, chúng tập trung ở phía nào?',
|
|
184
|
+
options: [
|
|
185
|
+
{ id: 'OPT_YANG_BRANCH', labelVi: 'Tập trung rõ hơn ở nửa bên trái cơ thể hoặc vùng đầu mặt' },
|
|
186
|
+
{ id: 'OPT_YIN_BRANCH', labelVi: 'Tập trung rõ hơn ở nửa bên phải cơ thể' },
|
|
187
|
+
{ id: 'OPT_NA', labelVi: 'Không rõ / Cân bằng cả hai bên hoặc chưa từng có' },
|
|
188
|
+
],
|
|
189
|
+
likelihoodMatrixMicro: {
|
|
190
|
+
OPT_YANG_BRANCH: [800000, 100000, 800000, 100000, 800000, 100000, 800000, 100000, 800000, 100000, 800000, 100000],
|
|
191
|
+
OPT_YIN_BRANCH: [100000, 800000, 100000, 800000, 100000, 800000, 100000, 800000, 100000, 800000, 100000, 800000],
|
|
192
|
+
OPT_NA: [100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000],
|
|
193
|
+
},
|
|
194
|
+
chartPredicates: {
|
|
195
|
+
OPT_YANG_BRANCH: { op: 'menh_branch_parity', parity: 0 },
|
|
196
|
+
OPT_YIN_BRANCH: { op: 'menh_branch_parity', parity: 1 },
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
id: 'Q-05',
|
|
201
|
+
category: 'SOMATIC_MEDICAL',
|
|
202
|
+
minAge: 0,
|
|
203
|
+
maxAge: 120,
|
|
204
|
+
ageCohort: null,
|
|
205
|
+
promptVi: 'Khi mệt mỏi kéo dài hoặc thời tiết đổi mùa, biểu hiện cơ địa nào xuất hiện rõ hơn ở bạn?',
|
|
206
|
+
options: [
|
|
207
|
+
{ id: 'OPT_TY', labelVi: 'Dễ lạnh bàn tay bàn chân hoặc đau mỏi vùng thắt lưng' },
|
|
208
|
+
{ id: 'OPT_NGO', labelVi: 'Dễ bốc nóng mặt, nhiệt miệng hoặc mỏi rát mắt khi thiếu ngủ' },
|
|
209
|
+
{ id: 'OPT_OTHER_05', labelVi: 'Không có biểu hiện nào trong hai nhóm trên' },
|
|
210
|
+
{ id: 'OPT_NA', labelVi: 'Không rõ' },
|
|
211
|
+
],
|
|
212
|
+
likelihoodMatrixMicro: {
|
|
213
|
+
OPT_TY: [760000, 100000, 100000, 100000, 100000, 100000, 80000, 100000, 100000, 100000, 100000, 100000],
|
|
214
|
+
OPT_NGO: [80000, 100000, 100000, 100000, 100000, 100000, 760000, 100000, 100000, 100000, 100000, 100000],
|
|
215
|
+
OPT_OTHER_05: [80000, 720000, 720000, 720000, 720000, 720000, 80000, 720000, 720000, 720000, 720000, 720000],
|
|
216
|
+
OPT_NA: [80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000],
|
|
217
|
+
},
|
|
218
|
+
chartPredicates: {
|
|
219
|
+
OPT_TY: { op: 'shichen_in', branches: [0] },
|
|
220
|
+
OPT_NGO: { op: 'shichen_in', branches: [6] },
|
|
221
|
+
OPT_OTHER_05: { op: 'otherwise' },
|
|
222
|
+
},
|
|
223
|
+
},
|
|
224
|
+
{
|
|
225
|
+
id: 'Q-06',
|
|
226
|
+
category: 'SOMATIC_MEDICAL',
|
|
227
|
+
minAge: 0,
|
|
228
|
+
maxAge: 120,
|
|
229
|
+
ageCohort: null,
|
|
230
|
+
promptVi: 'Khi ăn uống thất thường hoặc căng thẳng công việc, phản ứng tiêu hóa nào hay gặp nhất ở bạn?',
|
|
231
|
+
options: [
|
|
232
|
+
{ id: 'OPT_SUU', labelVi: 'Hay căng tức vùng hạ sườn hoặc khó ngủ lại lúc nửa đêm về sáng' },
|
|
233
|
+
{ id: 'OPT_MUI', labelVi: 'Dễ trào ngược dạ dày hoặc đầy hơi chướng bụng vào đầu giờ chiều' },
|
|
234
|
+
{ id: 'OPT_OTHER_06', labelVi: 'Hệ tiêu hóa ổn định, không gặp hai dấu hiệu trên' },
|
|
235
|
+
{ id: 'OPT_NA', labelVi: 'Không rõ' },
|
|
236
|
+
],
|
|
237
|
+
likelihoodMatrixMicro: {
|
|
238
|
+
OPT_SUU: [100000, 760000, 100000, 100000, 100000, 100000, 100000, 80000, 100000, 100000, 100000, 100000],
|
|
239
|
+
OPT_MUI: [100000, 80000, 100000, 100000, 100000, 100000, 100000, 760000, 100000, 100000, 100000, 100000],
|
|
240
|
+
OPT_OTHER_06: [720000, 80000, 720000, 720000, 720000, 720000, 720000, 80000, 720000, 720000, 720000, 720000],
|
|
241
|
+
OPT_NA: [80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000],
|
|
242
|
+
},
|
|
243
|
+
chartPredicates: {
|
|
244
|
+
OPT_SUU: { op: 'shichen_in', branches: [1] },
|
|
245
|
+
OPT_MUI: { op: 'shichen_in', branches: [7] },
|
|
246
|
+
OPT_OTHER_06: { op: 'otherwise' },
|
|
247
|
+
},
|
|
248
|
+
},
|
|
249
|
+
{
|
|
250
|
+
id: 'Q-07',
|
|
251
|
+
category: 'SOMATIC_MEDICAL',
|
|
252
|
+
minAge: 0,
|
|
253
|
+
maxAge: 120,
|
|
254
|
+
ageCohort: null,
|
|
255
|
+
promptVi: 'Khi trời trở lạnh hoặc hanh khô kéo dài, dấu hiệu cơ địa nào xuất hiện rõ hơn?',
|
|
256
|
+
options: [
|
|
257
|
+
{ id: 'OPT_DAN', labelVi: 'Nhạy cảm đường hô hấp trên, dễ ho khan hoặc nghẹt mũi lúc sáng sớm' },
|
|
258
|
+
{ id: 'OPT_THAN', labelVi: 'Hay khát nước về chiều, dễ tiểu đêm hoặc mỏi bắp chân khi trời lạnh' },
|
|
259
|
+
{ id: 'OPT_OTHER_07', labelVi: 'Không có biểu hiện nào trong hai nhóm trên' },
|
|
260
|
+
{ id: 'OPT_NA', labelVi: 'Không rõ' },
|
|
261
|
+
],
|
|
262
|
+
likelihoodMatrixMicro: {
|
|
263
|
+
OPT_DAN: [100000, 100000, 760000, 100000, 100000, 100000, 100000, 100000, 80000, 100000, 100000, 100000],
|
|
264
|
+
OPT_THAN: [100000, 100000, 80000, 100000, 100000, 100000, 100000, 100000, 760000, 100000, 100000, 100000],
|
|
265
|
+
OPT_OTHER_07: [720000, 720000, 80000, 720000, 720000, 720000, 720000, 720000, 80000, 720000, 720000, 720000],
|
|
266
|
+
OPT_NA: [80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000],
|
|
267
|
+
},
|
|
268
|
+
chartPredicates: {
|
|
269
|
+
OPT_DAN: { op: 'shichen_in', branches: [2] },
|
|
270
|
+
OPT_THAN: { op: 'shichen_in', branches: [8] },
|
|
271
|
+
OPT_OTHER_07: { op: 'otherwise' },
|
|
272
|
+
},
|
|
273
|
+
},
|
|
274
|
+
{
|
|
275
|
+
id: 'Q-08',
|
|
276
|
+
category: 'SOMATIC_MEDICAL',
|
|
277
|
+
minAge: 0,
|
|
278
|
+
maxAge: 120,
|
|
279
|
+
ageCohort: null,
|
|
280
|
+
promptVi: 'Về cơ địa dị ứng hoặc nhạy cảm niêm mạc hàng ngày, bạn hay gặp tình huống nào hơn?',
|
|
281
|
+
options: [
|
|
282
|
+
{ id: 'OPT_MAO', labelVi: 'Đường ruột nhạy cảm với đồ lạnh hoặc sữa vào buổi sáng sớm' },
|
|
283
|
+
{ id: 'OPT_DAU', labelVi: 'Viêm mũi dị ứng hoặc khô rát họng mỗi khi gió lạnh buổi chiều tối' },
|
|
284
|
+
{ id: 'OPT_OTHER_08', labelVi: 'Không có cơ địa nhạy cảm ở cả hai nhóm trên' },
|
|
285
|
+
{ id: 'OPT_NA', labelVi: 'Không rõ' },
|
|
286
|
+
],
|
|
287
|
+
likelihoodMatrixMicro: {
|
|
288
|
+
OPT_MAO: [100000, 100000, 100000, 760000, 100000, 100000, 100000, 100000, 100000, 80000, 100000, 100000],
|
|
289
|
+
OPT_DAU: [100000, 100000, 100000, 80000, 100000, 100000, 100000, 100000, 100000, 760000, 100000, 100000],
|
|
290
|
+
OPT_OTHER_08: [720000, 720000, 720000, 80000, 720000, 720000, 720000, 720000, 720000, 80000, 720000, 720000],
|
|
291
|
+
OPT_NA: [80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000],
|
|
292
|
+
},
|
|
293
|
+
chartPredicates: {
|
|
294
|
+
OPT_MAO: { op: 'shichen_in', branches: [3] },
|
|
295
|
+
OPT_DAU: { op: 'shichen_in', branches: [9] },
|
|
296
|
+
OPT_OTHER_08: { op: 'otherwise' },
|
|
297
|
+
},
|
|
298
|
+
},
|
|
299
|
+
{
|
|
300
|
+
id: 'Q-09',
|
|
301
|
+
category: 'SOMATIC_MEDICAL',
|
|
302
|
+
minAge: 0,
|
|
303
|
+
maxAge: 120,
|
|
304
|
+
ageCohort: null,
|
|
305
|
+
promptVi: 'Trong nhịp sinh hoạt hàng ngày, vấn đề thể chất nào ảnh hưởng tới bạn rõ rệt hơn?',
|
|
306
|
+
options: [
|
|
307
|
+
{ id: 'OPT_THIN', labelVi: 'Hễ trễ bữa sáng là đau rát thượng vị hoặc cồn cào dạ dày ngay' },
|
|
308
|
+
{ id: 'OPT_TUAT', labelVi: 'Khó thư giãn vùng cổ vai gáy vào buổi tối khiến khó vào giấc ngủ sớm' },
|
|
309
|
+
{ id: 'OPT_OTHER_09', labelVi: 'Không gặp cả hai vấn đề trên' },
|
|
310
|
+
{ id: 'OPT_NA', labelVi: 'Không rõ' },
|
|
311
|
+
],
|
|
312
|
+
likelihoodMatrixMicro: {
|
|
313
|
+
OPT_THIN: [100000, 100000, 100000, 100000, 760000, 100000, 100000, 100000, 100000, 100000, 80000, 100000],
|
|
314
|
+
OPT_TUAT: [100000, 100000, 100000, 100000, 80000, 100000, 100000, 100000, 100000, 100000, 760000, 100000],
|
|
315
|
+
OPT_OTHER_09: [720000, 720000, 720000, 720000, 80000, 720000, 720000, 720000, 720000, 720000, 80000, 720000],
|
|
316
|
+
OPT_NA: [80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000],
|
|
317
|
+
},
|
|
318
|
+
chartPredicates: {
|
|
319
|
+
OPT_THIN: { op: 'shichen_in', branches: [4] },
|
|
320
|
+
OPT_TUAT: { op: 'shichen_in', branches: [10] },
|
|
321
|
+
OPT_OTHER_09: { op: 'otherwise' },
|
|
322
|
+
},
|
|
323
|
+
},
|
|
324
|
+
{
|
|
325
|
+
id: 'Q-10',
|
|
326
|
+
category: 'KINSHIP_TOPOLOGY',
|
|
327
|
+
minAge: 0,
|
|
328
|
+
maxAge: 120,
|
|
329
|
+
ageCohort: null,
|
|
330
|
+
promptVi: 'Theo lời kể của mẹ hoặc giấy chứng sinh, ca sinh của bạn diễn ra trong tình huống nào?',
|
|
331
|
+
options: [
|
|
332
|
+
{ id: 'OPT_TY_SNAKE', labelVi: 'Sinh thường nhanh thuận lợi vào ban ngày trời sáng' },
|
|
333
|
+
{ id: 'OPT_HOI', labelVi: 'Ca sinh vào đêm muộn hoặc có can thiệp sản khoa (sinh khó, sinh mổ, dây rốn quấn cổ)' },
|
|
334
|
+
{ id: 'OPT_OTHER_10', labelVi: 'Ca sinh bình thường nhưng không thuộc hai tình huống trên' },
|
|
335
|
+
{ id: 'OPT_NA', labelVi: 'Không còn thông tin lúc sinh' },
|
|
336
|
+
],
|
|
337
|
+
likelihoodMatrixMicro: {
|
|
338
|
+
OPT_TY_SNAKE: [100000, 100000, 100000, 100000, 100000, 760000, 100000, 100000, 100000, 100000, 100000, 80000],
|
|
339
|
+
OPT_HOI: [100000, 100000, 100000, 100000, 100000, 80000, 100000, 100000, 100000, 100000, 100000, 760000],
|
|
340
|
+
OPT_OTHER_10: [720000, 720000, 720000, 720000, 720000, 80000, 720000, 720000, 720000, 720000, 720000, 80000],
|
|
341
|
+
OPT_NA: [80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000, 80000],
|
|
342
|
+
},
|
|
343
|
+
chartPredicates: {
|
|
344
|
+
OPT_TY_SNAKE: { op: 'shichen_in', branches: [5] },
|
|
345
|
+
OPT_HOI: { op: 'shichen_in', branches: [11] },
|
|
346
|
+
OPT_OTHER_10: { op: 'otherwise' },
|
|
347
|
+
},
|
|
348
|
+
},
|
|
349
|
+
{
|
|
350
|
+
id: 'Q-11',
|
|
351
|
+
category: 'KINSHIP_TOPOLOGY',
|
|
352
|
+
minAge: 0,
|
|
353
|
+
maxAge: 120,
|
|
354
|
+
ageCohort: null,
|
|
355
|
+
promptVi: 'Nghề nghiệp chủ đạo của cha mẹ trong giai đoạn nuôi dưỡng bạn thuộc nhóm nào?',
|
|
356
|
+
options: [
|
|
357
|
+
{ id: 'OPT_GAP_A', labelVi: 'Nghề có tính biến động cao (tự doanh nhỏ, đổi việc hoặc chuyển công tác nhiều)' },
|
|
358
|
+
{ id: 'OPT_GAP_B', labelVi: 'Ngành hành chính, giáo dục, y tế hoặc chuyên môn ổn định lâu năm' },
|
|
359
|
+
{ id: 'OPT_GAP_C', labelVi: 'Kinh doanh thương mại, kỹ thuật hoặc làm việc ở nơi xa gia đình' },
|
|
360
|
+
{ id: 'OPT_NA', labelVi: 'Không rõ / Đa dạng khác' },
|
|
361
|
+
],
|
|
362
|
+
likelihoodMatrixMicro: {
|
|
363
|
+
OPT_GAP_A: [70000, 760000, 760000, 70000, 70000, 70000, 70000, 760000, 760000, 70000, 70000, 70000],
|
|
364
|
+
OPT_GAP_B: [70000, 70000, 70000, 760000, 760000, 70000, 70000, 70000, 70000, 760000, 760000, 70000],
|
|
365
|
+
OPT_GAP_C: [760000, 70000, 70000, 70000, 70000, 760000, 760000, 70000, 70000, 70000, 70000, 760000],
|
|
366
|
+
OPT_NA: [100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000],
|
|
367
|
+
},
|
|
368
|
+
chartPredicates: {
|
|
369
|
+
OPT_GAP_A: {
|
|
370
|
+
op: 'palace_has_any_star',
|
|
371
|
+
palace: 'Phụ Mẫu',
|
|
372
|
+
stars: ['Thất Sát', 'Phá Quân', 'Tham Lang', 'Thiên Mã'],
|
|
373
|
+
},
|
|
374
|
+
OPT_GAP_B: {
|
|
375
|
+
op: 'palace_has_any_star',
|
|
376
|
+
palace: 'Phụ Mẫu',
|
|
377
|
+
stars: ['Thiên Phủ', 'Thiên Lương', 'Thiên Tướng', 'Văn Xương', 'Văn Khúc'],
|
|
378
|
+
},
|
|
379
|
+
OPT_GAP_C: {
|
|
380
|
+
op: 'palace_has_any_star',
|
|
381
|
+
palace: 'Phụ Mẫu',
|
|
382
|
+
stars: ['Vũ Khúc', 'Thái Dương', 'Cự Môn', 'Lộc Tồn'],
|
|
383
|
+
},
|
|
384
|
+
},
|
|
385
|
+
},
|
|
386
|
+
{
|
|
387
|
+
id: 'Q-12',
|
|
388
|
+
category: 'RETRODICTIVE_TIMELINE',
|
|
389
|
+
minAge: 12,
|
|
390
|
+
maxAge: 120,
|
|
391
|
+
ageCohort: null,
|
|
392
|
+
promptVi: 'Trong giai đoạn 12–18 tuổi, dấu mốc thực tế nổi bật nhất của bạn là gì?',
|
|
393
|
+
options: [
|
|
394
|
+
{ id: 'OPT_REV_A', labelVi: 'Chuyển trường chuyên, lớp chọn hoặc chuyển môi trường học xa nhà' },
|
|
395
|
+
{ id: 'OPT_REV_B', labelVi: 'Gia đình trải qua biến động rõ rệt về nhà cửa hoặc tài chính' },
|
|
396
|
+
{ id: 'OPT_REV_C', labelVi: 'Đảm nhận vai trò lớp trưởng, đội trưởng hoặc đạt giải thưởng thành tích nổi bật' },
|
|
397
|
+
{ id: 'OPT_NA', labelVi: 'Giai đoạn học tập bình lặng, không có biến cố nổi bật' },
|
|
398
|
+
],
|
|
399
|
+
likelihoodMatrixMicro: {
|
|
400
|
+
OPT_REV_A: [70000, 70000, 760000, 760000, 70000, 70000, 70000, 70000, 760000, 760000, 70000, 70000],
|
|
401
|
+
OPT_REV_B: [760000, 760000, 70000, 70000, 760000, 760000, 70000, 70000, 70000, 70000, 70000, 70000],
|
|
402
|
+
OPT_REV_C: [70000, 70000, 70000, 70000, 70000, 70000, 760000, 760000, 70000, 70000, 760000, 760000],
|
|
403
|
+
OPT_NA: [100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000],
|
|
404
|
+
},
|
|
405
|
+
chartPredicates: {
|
|
406
|
+
OPT_REV_A: {
|
|
407
|
+
op: 'or',
|
|
408
|
+
args: [
|
|
409
|
+
{ op: 'palace_has_any_star', palace: 'Thiên Di', stars: ['Thiên Mã', 'Thái Dương', 'Thiên Cơ'] },
|
|
410
|
+
{ op: 'palace_has_sihua', palace: 'Quan Lộc', transforms: ['Hóa Khoa'] },
|
|
411
|
+
],
|
|
412
|
+
},
|
|
413
|
+
OPT_REV_B: {
|
|
414
|
+
op: 'or',
|
|
415
|
+
args: [
|
|
416
|
+
{ op: 'palace_has_sihua', palace: 'Điền Trạch', transforms: ['Hóa Kỵ'] },
|
|
417
|
+
{ op: 'palace_has_tuan_triet', palace: 'Phụ Mẫu' },
|
|
418
|
+
],
|
|
419
|
+
},
|
|
420
|
+
OPT_REV_C: {
|
|
421
|
+
op: 'or',
|
|
422
|
+
args: [
|
|
423
|
+
{ op: 'palace_has_sihua', palace: 'Mệnh', transforms: ['Hóa Quyền', 'Hóa Khoa'] },
|
|
424
|
+
{ op: 'palace_has_any_star', palace: 'Mệnh', stars: ['Tử Vi', 'Thái Dương', 'Thiên Khôi', 'Thiên Việt'] },
|
|
425
|
+
],
|
|
426
|
+
},
|
|
427
|
+
},
|
|
428
|
+
},
|
|
429
|
+
];
|
|
430
|
+
export function loadPublicRectificationQuestions() {
|
|
431
|
+
return [...PUBLIC_RECTIFICATION_QUESTIONS];
|
|
432
|
+
}
|
|
433
|
+
function getRawLikelihood(question, optionIdx, optionId, branch) {
|
|
434
|
+
const b = floorMod(branch, 12);
|
|
435
|
+
const mat = question.likelihoodMatrixMicro;
|
|
436
|
+
if (Array.isArray(mat)) {
|
|
437
|
+
const row = mat[optionIdx];
|
|
438
|
+
if (!row || row[b] === undefined) {
|
|
439
|
+
throw new Error(`Missing likelihood entry for question '${question.id}', option '${optionId}', branch ${b}`);
|
|
440
|
+
}
|
|
441
|
+
return Number(row[b]);
|
|
442
|
+
}
|
|
443
|
+
const entry = mat[optionId];
|
|
444
|
+
if (!entry) {
|
|
445
|
+
throw new Error(`Missing likelihood row for question '${question.id}', option '${optionId}'`);
|
|
446
|
+
}
|
|
447
|
+
if (Array.isArray(entry)) {
|
|
448
|
+
const val = entry[b];
|
|
449
|
+
if (val === undefined) {
|
|
450
|
+
throw new Error(`Likelihood array for '${question.id}' option '${optionId}' must cover 12 branches (missing ${b})`);
|
|
451
|
+
}
|
|
452
|
+
return Number(val);
|
|
453
|
+
}
|
|
454
|
+
const dictVal = entry[String(b)];
|
|
455
|
+
if (dictVal === undefined) {
|
|
456
|
+
throw new Error(`Likelihood map for '${question.id}' option '${optionId}' missing branch '${b}'`);
|
|
457
|
+
}
|
|
458
|
+
return Number(dictVal);
|
|
459
|
+
}
|
|
460
|
+
export function validateQuestionBank(questionBank) {
|
|
461
|
+
for (const q of questionBank) {
|
|
462
|
+
if (!ALLOWED_CATEGORIES.has(q.category)) {
|
|
463
|
+
throw new Error(`Anti-Barnum violation in question '${q.id}': category '${String(q.category)}' is forbidden; allowed categories are RETRODICTIVE_TIMELINE, SOMATIC_MEDICAL, KINSHIP_TOPOLOGY`);
|
|
464
|
+
}
|
|
465
|
+
if (!q.options || q.options.length < 2) {
|
|
466
|
+
throw new Error(`Rectification question '${q.id}' must define at least 2 options`);
|
|
467
|
+
}
|
|
468
|
+
for (let optIdx = 0; optIdx < q.options.length; optIdx++) {
|
|
469
|
+
const opt = q.options[optIdx];
|
|
470
|
+
for (let b = 0; b < 12; b++) {
|
|
471
|
+
const val = getRawLikelihood(q, optIdx, opt.id, b);
|
|
472
|
+
if (!Number.isFinite(val) || val <= 0) {
|
|
473
|
+
throw new Error(`Invalid likelihood ${val} in question '${q.id}' option '${opt.id}' branch ${b}: must be positive`);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* Returns a [options.length][12] table where each branch column sums to 1_000_000.
|
|
481
|
+
*/
|
|
482
|
+
function buildNormalizedColumnMatrix(question) {
|
|
483
|
+
const optCount = question.options.length;
|
|
484
|
+
const table = Array.from({ length: optCount }, () => new Array(12).fill(0));
|
|
485
|
+
for (let b = 0; b < 12; b++) {
|
|
486
|
+
const rawCol = [];
|
|
487
|
+
for (let o = 0; o < optCount; o++) {
|
|
488
|
+
rawCol.push(getRawLikelihood(question, o, question.options[o].id, b));
|
|
489
|
+
}
|
|
490
|
+
const normCol = normalizeMicroProbs(rawCol);
|
|
491
|
+
for (let o = 0; o < optCount; o++) {
|
|
492
|
+
table[o][b] = normCol[o];
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
return table;
|
|
496
|
+
}
|
|
497
|
+
function isQuestionEligibleForAge(q, userAge) {
|
|
498
|
+
if (userAge === null)
|
|
499
|
+
return true;
|
|
500
|
+
if (q.minAge !== undefined && q.minAge !== null && userAge < q.minAge)
|
|
501
|
+
return false;
|
|
502
|
+
if (q.maxAge !== undefined && q.maxAge !== null && userAge > q.maxAge)
|
|
503
|
+
return false;
|
|
504
|
+
return true;
|
|
505
|
+
}
|
|
506
|
+
function summarizeTopTwo(candidates) {
|
|
507
|
+
const sorted = [...candidates].sort((a, b) => b.probMicro !== a.probMicro ? b.probMicro - a.probMicro : a.shichenBranch - b.shichenBranch);
|
|
508
|
+
const top1 = sorted[0];
|
|
509
|
+
const top2 = sorted[1];
|
|
510
|
+
return {
|
|
511
|
+
top1ProbMicro: top1 ? top1.probMicro : 0,
|
|
512
|
+
top2ProbMicro: top2 ? top2.probMicro : 0,
|
|
513
|
+
topBranch: top1 ? top1.shichenBranch : 0,
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
function isBayesianLocked(top1ProbMicro, top2ProbMicro) {
|
|
517
|
+
return top1ProbMicro >= LOCK_PROB_THRESHOLD_MICRO && top1ProbMicro >= LOCK_RATIO_MULTIPLIER * top2ProbMicro;
|
|
518
|
+
}
|
|
519
|
+
export function createInitialRectificationState(options) {
|
|
520
|
+
const rawBranches = options?.candidateBranches ?? [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
|
|
521
|
+
const branchSet = new Set();
|
|
522
|
+
for (const b of rawBranches) {
|
|
523
|
+
branchSet.add(floorMod(Math.trunc(b), 12));
|
|
524
|
+
}
|
|
525
|
+
const branches = Array.from(branchSet).sort((a, b) => a - b);
|
|
526
|
+
if (branches.length === 0) {
|
|
527
|
+
throw new Error('createInitialRectificationState requires at least 1 candidate Shichen branch');
|
|
528
|
+
}
|
|
529
|
+
let rawWeights;
|
|
530
|
+
if (options?.priorWeights === undefined) {
|
|
531
|
+
rawWeights = branches.map(() => 1);
|
|
532
|
+
}
|
|
533
|
+
else if (Array.isArray(options.priorWeights)) {
|
|
534
|
+
const arr = options.priorWeights;
|
|
535
|
+
if (arr.length === branches.length) {
|
|
536
|
+
rawWeights = [...arr];
|
|
537
|
+
}
|
|
538
|
+
else if (arr.length === 12) {
|
|
539
|
+
rawWeights = branches.map((b) => arr[b] ?? 1);
|
|
540
|
+
}
|
|
541
|
+
else {
|
|
542
|
+
throw new Error('priorWeights array length must match candidateBranches length or 12');
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
else {
|
|
546
|
+
const rec = options.priorWeights;
|
|
547
|
+
rawWeights = branches.map((b) => rec[b] ?? 1);
|
|
548
|
+
}
|
|
549
|
+
const probsMicro = normalizeMicroProbs(rawWeights);
|
|
550
|
+
const candidates = branches.map((b, i) => ({
|
|
551
|
+
shichenBranch: b,
|
|
552
|
+
shichenBranchVi: BRANCHES_VI[b],
|
|
553
|
+
probMicro: probsMicro[i],
|
|
554
|
+
}));
|
|
555
|
+
const entropyMillibits = computeEntropyMillibits(probsMicro);
|
|
556
|
+
const maxQuestions = options?.maxQuestions ?? 5;
|
|
557
|
+
const userAge = options?.userAge ?? null;
|
|
558
|
+
const { top1ProbMicro, top2ProbMicro, topBranch } = summarizeTopTwo(candidates);
|
|
559
|
+
const locked = isBayesianLocked(top1ProbMicro, top2ProbMicro);
|
|
560
|
+
return {
|
|
561
|
+
schemaVersion: '1.0.0',
|
|
562
|
+
status: locked ? 'LOCKED' : maxQuestions <= 0 ? 'INVARIANT_INTERSECTION' : 'ACTIVE',
|
|
563
|
+
candidates,
|
|
564
|
+
entropyMillibits,
|
|
565
|
+
askedQuestionIds: [],
|
|
566
|
+
answers: [],
|
|
567
|
+
maxQuestions,
|
|
568
|
+
userAge,
|
|
569
|
+
lockedShichenBranch: locked ? topBranch : null,
|
|
570
|
+
top1ProbMicro,
|
|
571
|
+
top2ProbMicro,
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
export function selectNextQuestion(state, questionBank = PUBLIC_RECTIFICATION_QUESTIONS) {
|
|
575
|
+
validateQuestionBank(questionBank);
|
|
576
|
+
if (state.status !== 'ACTIVE' || state.answers.length >= state.maxQuestions) {
|
|
577
|
+
return null;
|
|
578
|
+
}
|
|
579
|
+
const askedSet = new Set(state.askedQuestionIds);
|
|
580
|
+
const eligible = questionBank.filter((q) => !askedSet.has(q.id) && isQuestionEligibleForAge(q, state.userAge));
|
|
581
|
+
if (eligible.length === 0) {
|
|
582
|
+
return null;
|
|
583
|
+
}
|
|
584
|
+
const priorProbs = state.candidates.map((c) => c.probMicro);
|
|
585
|
+
const currentHFloat = entropyBitsFloat(priorProbs);
|
|
586
|
+
const currentEntropyMillibits = jsRound(currentHFloat * 1000.0);
|
|
587
|
+
let bestQuestion = null;
|
|
588
|
+
let bestExpectedPostMillibits = currentEntropyMillibits;
|
|
589
|
+
let bestGainMillibits = -1;
|
|
590
|
+
let bestGainMicrobits = -1;
|
|
591
|
+
for (const q of eligible) {
|
|
592
|
+
const colMatrix = buildNormalizedColumnMatrix(q);
|
|
593
|
+
let expectedPostHFloat = 0.0;
|
|
594
|
+
for (let o = 0; o < q.options.length; o++) {
|
|
595
|
+
const optRow = colMatrix[o];
|
|
596
|
+
let marginalPico = 0n;
|
|
597
|
+
const unnormWeights = [];
|
|
598
|
+
for (const cand of state.candidates) {
|
|
599
|
+
const lMicro = BigInt(optRow[cand.shichenBranch]);
|
|
600
|
+
const wPico = BigInt(cand.probMicro) * lMicro;
|
|
601
|
+
unnormWeights.push(wPico);
|
|
602
|
+
marginalPico += wPico;
|
|
603
|
+
}
|
|
604
|
+
if (marginalPico > 0n) {
|
|
605
|
+
const postMicro = normalizeMicroProbs(unnormWeights);
|
|
606
|
+
const postH = entropyBitsFloat(postMicro);
|
|
607
|
+
const pOption = Number(marginalPico) / 1_000_000_000_000.0;
|
|
608
|
+
expectedPostHFloat += pOption * postH;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
const expectedPostMillibits = jsRound(expectedPostHFloat * 1000.0);
|
|
612
|
+
const gainMillibits = Math.max(0, currentEntropyMillibits - expectedPostMillibits);
|
|
613
|
+
const gainMicrobits = Math.max(0, jsRound((currentHFloat - expectedPostHFloat) * 1_000_000.0));
|
|
614
|
+
const isBetter = bestQuestion === null ||
|
|
615
|
+
gainMillibits > bestGainMillibits ||
|
|
616
|
+
(gainMillibits === bestGainMillibits && gainMicrobits > bestGainMicrobits) ||
|
|
617
|
+
(gainMillibits === bestGainMillibits &&
|
|
618
|
+
gainMicrobits === bestGainMicrobits &&
|
|
619
|
+
cmpStr(q.id, bestQuestion.id) < 0);
|
|
620
|
+
if (isBetter) {
|
|
621
|
+
bestQuestion = q;
|
|
622
|
+
bestExpectedPostMillibits = expectedPostMillibits;
|
|
623
|
+
bestGainMillibits = gainMillibits;
|
|
624
|
+
bestGainMicrobits = gainMicrobits;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
if (!bestQuestion)
|
|
628
|
+
return null;
|
|
629
|
+
const canonicalQuestion = {
|
|
630
|
+
id: bestQuestion.id,
|
|
631
|
+
category: bestQuestion.category,
|
|
632
|
+
ageCohort: bestQuestion.ageCohort ?? null,
|
|
633
|
+
minAge: bestQuestion.minAge ?? null,
|
|
634
|
+
maxAge: bestQuestion.maxAge ?? null,
|
|
635
|
+
promptVi: bestQuestion.promptVi,
|
|
636
|
+
options: bestQuestion.options.map((o) => ({ id: o.id, labelVi: o.labelVi })),
|
|
637
|
+
likelihoodMatrixMicro: bestQuestion.likelihoodMatrixMicro,
|
|
638
|
+
chartPredicates: bestQuestion.chartPredicates ?? null,
|
|
639
|
+
};
|
|
640
|
+
return {
|
|
641
|
+
question: canonicalQuestion,
|
|
642
|
+
questionId: bestQuestion.id,
|
|
643
|
+
currentEntropyMillibits,
|
|
644
|
+
expectedPosteriorEntropyMillibits: bestExpectedPostMillibits,
|
|
645
|
+
expectedInfoGainMillibits: bestGainMillibits,
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
export function answerQuestion(state, questionId, answerValue, questionBank = PUBLIC_RECTIFICATION_QUESTIONS) {
|
|
649
|
+
validateQuestionBank(questionBank);
|
|
650
|
+
const question = questionBank.find((q) => q.id === questionId);
|
|
651
|
+
if (!question) {
|
|
652
|
+
throw new Error(`Rectification question '${questionId}' not found in question bank`);
|
|
653
|
+
}
|
|
654
|
+
const optIndex = question.options.findIndex((o) => o.id === answerValue);
|
|
655
|
+
if (optIndex < 0) {
|
|
656
|
+
throw new Error(`Invalid option '${answerValue}' for rectification question '${questionId}'`);
|
|
657
|
+
}
|
|
658
|
+
const colMatrix = buildNormalizedColumnMatrix(question);
|
|
659
|
+
const optRow = colMatrix[optIndex];
|
|
660
|
+
const unnormWeights = state.candidates.map((c) => BigInt(c.probMicro) * BigInt(optRow[c.shichenBranch]));
|
|
661
|
+
const postMicro = normalizeMicroProbs(unnormWeights);
|
|
662
|
+
const newCandidates = state.candidates.map((c, idx) => ({
|
|
663
|
+
shichenBranch: c.shichenBranch,
|
|
664
|
+
shichenBranchVi: c.shichenBranchVi,
|
|
665
|
+
probMicro: postMicro[idx],
|
|
666
|
+
}));
|
|
667
|
+
const entropyBeforeMillibits = state.entropyMillibits;
|
|
668
|
+
const entropyAfterMillibits = computeEntropyMillibits(postMicro);
|
|
669
|
+
const askedQuestionIds = state.askedQuestionIds.includes(questionId)
|
|
670
|
+
? [...state.askedQuestionIds]
|
|
671
|
+
: [...state.askedQuestionIds, questionId];
|
|
672
|
+
const answers = [
|
|
673
|
+
...state.answers,
|
|
674
|
+
{
|
|
675
|
+
questionId,
|
|
676
|
+
optionId: answerValue,
|
|
677
|
+
entropyBeforeMillibits,
|
|
678
|
+
entropyAfterMillibits,
|
|
679
|
+
},
|
|
680
|
+
];
|
|
681
|
+
const { top1ProbMicro, top2ProbMicro, topBranch } = summarizeTopTwo(newCandidates);
|
|
682
|
+
const locked = isBayesianLocked(top1ProbMicro, top2ProbMicro);
|
|
683
|
+
const askedSet = new Set(askedQuestionIds);
|
|
684
|
+
const remainingCount = questionBank.filter((q) => !askedSet.has(q.id) && isQuestionEligibleForAge(q, state.userAge)).length;
|
|
685
|
+
let status;
|
|
686
|
+
let lockedShichenBranch = null;
|
|
687
|
+
if (locked) {
|
|
688
|
+
status = 'LOCKED';
|
|
689
|
+
lockedShichenBranch = topBranch;
|
|
690
|
+
}
|
|
691
|
+
else if (answers.length >= state.maxQuestions || remainingCount === 0) {
|
|
692
|
+
status = 'INVARIANT_INTERSECTION';
|
|
693
|
+
}
|
|
694
|
+
else {
|
|
695
|
+
status = 'ACTIVE';
|
|
696
|
+
}
|
|
697
|
+
return {
|
|
698
|
+
schemaVersion: '1.0.0',
|
|
699
|
+
status,
|
|
700
|
+
candidates: newCandidates,
|
|
701
|
+
entropyMillibits: entropyAfterMillibits,
|
|
702
|
+
askedQuestionIds,
|
|
703
|
+
answers,
|
|
704
|
+
maxQuestions: state.maxQuestions,
|
|
705
|
+
userAge: state.userAge,
|
|
706
|
+
lockedShichenBranch,
|
|
707
|
+
top1ProbMicro,
|
|
708
|
+
top2ProbMicro,
|
|
709
|
+
};
|
|
710
|
+
}
|
|
711
|
+
function isWeightedReportEntry(item) {
|
|
712
|
+
return 'report' in item && 'probMicro' in item;
|
|
713
|
+
}
|
|
714
|
+
export function computeInvariantIntersection(candidateReports, bazi, probsOrState) {
|
|
715
|
+
let survivingReports = [];
|
|
716
|
+
let survivingBranches = [];
|
|
717
|
+
if (candidateReports.length > 0 && isWeightedReportEntry(candidateReports[0])) {
|
|
718
|
+
const weighted = candidateReports;
|
|
719
|
+
const filtered = weighted.filter((w) => w.probMicro >= SURVIVING_THRESHOLD_MICRO);
|
|
720
|
+
const chosen = filtered.length > 0 ? filtered : [weighted.reduce((a, b) => (b.probMicro > a.probMicro ? b : a))];
|
|
721
|
+
survivingReports = chosen.map((w) => w.report);
|
|
722
|
+
survivingBranches = chosen
|
|
723
|
+
.map((w, idx) => (w.shichenBranch !== undefined ? w.shichenBranch : idx))
|
|
724
|
+
.sort((a, b) => a - b);
|
|
725
|
+
}
|
|
726
|
+
else {
|
|
727
|
+
const plainReports = candidateReports;
|
|
728
|
+
if (probsOrState === undefined) {
|
|
729
|
+
survivingReports = [...plainReports];
|
|
730
|
+
survivingBranches = plainReports.map((_, idx) => idx);
|
|
731
|
+
}
|
|
732
|
+
else if (Array.isArray(probsOrState)) {
|
|
733
|
+
const probs = probsOrState;
|
|
734
|
+
const indices = [];
|
|
735
|
+
for (let i = 0; i < plainReports.length; i++) {
|
|
736
|
+
if ((probs[i] ?? 0) >= SURVIVING_THRESHOLD_MICRO) {
|
|
737
|
+
indices.push(i);
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
if (indices.length === 0 && plainReports.length > 0) {
|
|
741
|
+
let bestIdx = 0;
|
|
742
|
+
for (let i = 1; i < plainReports.length; i++) {
|
|
743
|
+
if ((probs[i] ?? 0) > (probs[bestIdx] ?? 0))
|
|
744
|
+
bestIdx = i;
|
|
745
|
+
}
|
|
746
|
+
indices.push(bestIdx);
|
|
747
|
+
}
|
|
748
|
+
survivingReports = indices.map((i) => plainReports[i]);
|
|
749
|
+
survivingBranches = indices;
|
|
750
|
+
}
|
|
751
|
+
else {
|
|
752
|
+
const rectState = probsOrState;
|
|
753
|
+
const indices = [];
|
|
754
|
+
const branches = [];
|
|
755
|
+
for (let i = 0; i < plainReports.length && i < rectState.candidates.length; i++) {
|
|
756
|
+
const cand = rectState.candidates[i];
|
|
757
|
+
if (cand.probMicro >= SURVIVING_THRESHOLD_MICRO) {
|
|
758
|
+
indices.push(i);
|
|
759
|
+
branches.push(cand.shichenBranch);
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
if (indices.length === 0 && plainReports.length > 0 && rectState.candidates.length > 0) {
|
|
763
|
+
let bestIdx = 0;
|
|
764
|
+
for (let i = 1; i < Math.min(plainReports.length, rectState.candidates.length); i++) {
|
|
765
|
+
if (rectState.candidates[i].probMicro > rectState.candidates[bestIdx].probMicro) {
|
|
766
|
+
bestIdx = i;
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
indices.push(bestIdx);
|
|
770
|
+
branches.push(rectState.candidates[bestIdx].shichenBranch);
|
|
771
|
+
}
|
|
772
|
+
survivingReports = indices.map((i) => plainReports[i]);
|
|
773
|
+
survivingBranches = branches.sort((a, b) => a - b);
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
let invariantNodes = [];
|
|
777
|
+
let invariantClaimsVi = [];
|
|
778
|
+
if (survivingReports.length > 0) {
|
|
779
|
+
const firstReport = survivingReports[0];
|
|
780
|
+
const commonNodeIds = new Set(firstReport.nodes.map((n) => n.nodeId));
|
|
781
|
+
for (let i = 1; i < survivingReports.length; i++) {
|
|
782
|
+
const ids = new Set(survivingReports[i].nodes.map((n) => n.nodeId));
|
|
783
|
+
for (const id of Array.from(commonNodeIds)) {
|
|
784
|
+
if (!ids.has(id))
|
|
785
|
+
commonNodeIds.delete(id);
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
invariantNodes = firstReport.nodes
|
|
789
|
+
.filter((n) => commonNodeIds.has(n.nodeId))
|
|
790
|
+
.sort((a, b) => (a.stratum !== b.stratum ? a.stratum - b.stratum : cmpStr(a.nodeId, b.nodeId)));
|
|
791
|
+
const commonClaims = new Set(firstReport.nodes.map((n) => n.claimVi));
|
|
792
|
+
for (let i = 1; i < survivingReports.length; i++) {
|
|
793
|
+
const claims = new Set(survivingReports[i].nodes.map((n) => n.claimVi));
|
|
794
|
+
for (const cl of Array.from(commonClaims)) {
|
|
795
|
+
if (!claims.has(cl))
|
|
796
|
+
commonClaims.delete(cl);
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
const orderedClaims = [];
|
|
800
|
+
const seenClaims = new Set();
|
|
801
|
+
for (const node of firstReport.nodes) {
|
|
802
|
+
if (commonClaims.has(node.claimVi) && !seenClaims.has(node.claimVi)) {
|
|
803
|
+
seenClaims.add(node.claimVi);
|
|
804
|
+
orderedClaims.push(node.claimVi);
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
invariantClaimsVi = orderedClaims;
|
|
808
|
+
}
|
|
809
|
+
return {
|
|
810
|
+
schemaVersion: '1.0.0',
|
|
811
|
+
mode: 'INVARIANT_INTERSECTION',
|
|
812
|
+
survivingCandidateCount: survivingReports.length,
|
|
813
|
+
survivingBranches,
|
|
814
|
+
invariantNodes,
|
|
815
|
+
invariantClaimsVi,
|
|
816
|
+
invariantBaziPillars: {
|
|
817
|
+
yearPillar: bazi.yearPillar.pillar,
|
|
818
|
+
monthPillar: bazi.monthPillar.pillar,
|
|
819
|
+
dayPillar: bazi.dayPillar.pillar,
|
|
820
|
+
},
|
|
821
|
+
};
|
|
822
|
+
}
|
|
823
|
+
const MENH_GROUP_BRANCHES = {
|
|
824
|
+
TU_CHINH: [0, 3, 6, 9],
|
|
825
|
+
TU_MO: [1, 4, 7, 10],
|
|
826
|
+
TU_SINH: [2, 5, 8, 11],
|
|
827
|
+
};
|
|
828
|
+
function normalizePalaceKey(name) {
|
|
829
|
+
const trimmed = name.trim();
|
|
830
|
+
return trimmed === 'Giao Hữu' || trimmed === 'Nô Bộc' ? 'Nô Bộc' : trimmed;
|
|
831
|
+
}
|
|
832
|
+
function findZiweiPalace(ziwei, palaceQuery) {
|
|
833
|
+
const target = normalizePalaceKey(palaceQuery);
|
|
834
|
+
if (target === 'Thân') {
|
|
835
|
+
return ziwei.palaces.find((p) => p.isThan || p.branchIndex === ziwei.thanBranch);
|
|
836
|
+
}
|
|
837
|
+
return ziwei.palaces.find((p) => normalizePalaceKey(p.palaceName) === target);
|
|
838
|
+
}
|
|
839
|
+
export function computeCertaintyPriorWeights(mode, ambiguousBranches = [], centerBranch, window) {
|
|
840
|
+
if (mode === 'UNKNOWN') {
|
|
841
|
+
return new Array(12).fill(1);
|
|
842
|
+
}
|
|
843
|
+
if (mode === 'TIME_WINDOW') {
|
|
844
|
+
const w = new Array(12).fill(5);
|
|
845
|
+
if (window === 'SANG') {
|
|
846
|
+
for (const b of [3, 4, 5])
|
|
847
|
+
w[b] = 280;
|
|
848
|
+
for (const b of [2, 6])
|
|
849
|
+
w[b] = 65;
|
|
850
|
+
return w;
|
|
851
|
+
}
|
|
852
|
+
if (window === 'TRUA') {
|
|
853
|
+
w.fill(8);
|
|
854
|
+
w[6] = 500;
|
|
855
|
+
for (const b of [5, 7])
|
|
856
|
+
w[b] = 190;
|
|
857
|
+
for (const b of [4, 8])
|
|
858
|
+
w[b] = 30;
|
|
859
|
+
return w;
|
|
860
|
+
}
|
|
861
|
+
if (window === 'CHIEU') {
|
|
862
|
+
for (const b of [7, 8, 9])
|
|
863
|
+
w[b] = 280;
|
|
864
|
+
for (const b of [6, 10])
|
|
865
|
+
w[b] = 65;
|
|
866
|
+
return w;
|
|
867
|
+
}
|
|
868
|
+
if (window === 'TOI') {
|
|
869
|
+
w.fill(7);
|
|
870
|
+
for (const b of [9, 10])
|
|
871
|
+
w[b] = 320;
|
|
872
|
+
w[11] = 220;
|
|
873
|
+
for (const b of [8, 0])
|
|
874
|
+
w[b] = 45;
|
|
875
|
+
return w;
|
|
876
|
+
}
|
|
877
|
+
if (window === 'DEM') {
|
|
878
|
+
for (const b of [11, 0, 1, 2])
|
|
879
|
+
w[b] = 225;
|
|
880
|
+
for (const b of [10, 3])
|
|
881
|
+
w[b] = 35;
|
|
882
|
+
return w;
|
|
883
|
+
}
|
|
884
|
+
return new Array(12).fill(1);
|
|
885
|
+
}
|
|
886
|
+
// EXACT_TIME
|
|
887
|
+
const uniqueAmbig = Array.from(new Set(ambiguousBranches.map((b) => floorMod(Math.trunc(b), 12))));
|
|
888
|
+
const center = centerBranch !== undefined ? floorMod(Math.trunc(centerBranch), 12) : (uniqueAmbig[0] ?? 0);
|
|
889
|
+
if (uniqueAmbig.length >= 2) {
|
|
890
|
+
const w = new Array(12).fill(10);
|
|
891
|
+
for (const b of uniqueAmbig)
|
|
892
|
+
w[b] = 450;
|
|
893
|
+
w[center] = 480;
|
|
894
|
+
return w;
|
|
895
|
+
}
|
|
896
|
+
const w = new Array(12).fill(20);
|
|
897
|
+
w[floorMod(center - 1, 12)] = 120;
|
|
898
|
+
w[floorMod(center + 1, 12)] = 120;
|
|
899
|
+
w[center] = 700;
|
|
900
|
+
return w;
|
|
901
|
+
}
|
|
902
|
+
export function evalChartPredicate(predicate, chart) {
|
|
903
|
+
const ziwei = chart.ziweiNamPhai;
|
|
904
|
+
switch (predicate.op) {
|
|
905
|
+
case 'always':
|
|
906
|
+
return true;
|
|
907
|
+
case 'otherwise':
|
|
908
|
+
return false;
|
|
909
|
+
case 'shichen_in':
|
|
910
|
+
return predicate.branches.includes(chart.temporal.trueSolarTime.shichenBranch);
|
|
911
|
+
case 'menh_vo_chinh_dieu': {
|
|
912
|
+
const menh = ziwei.palaces[ziwei.menhBranch] ?? findZiweiPalace(ziwei, 'Mệnh');
|
|
913
|
+
return menh ? Boolean(menh.voChinhDieu) === predicate.expected : false;
|
|
914
|
+
}
|
|
915
|
+
case 'menh_branch_parity':
|
|
916
|
+
return floorMod(ziwei.menhBranch, 2) === predicate.parity;
|
|
917
|
+
case 'menh_branch_group':
|
|
918
|
+
return MENH_GROUP_BRANCHES[predicate.group].includes(floorMod(ziwei.menhBranch, 12));
|
|
919
|
+
case 'cuc_in':
|
|
920
|
+
return predicate.cucNumbers.includes(ziwei.cucNumber);
|
|
921
|
+
case 'than_palace_in': {
|
|
922
|
+
const thanPalace = ziwei.palaces.find((p) => p.isThan || p.branchIndex === ziwei.thanBranch);
|
|
923
|
+
if (!thanPalace)
|
|
924
|
+
return false;
|
|
925
|
+
const actual = normalizePalaceKey(thanPalace.palaceName);
|
|
926
|
+
return predicate.palaces.some((p) => normalizePalaceKey(p) === actual);
|
|
927
|
+
}
|
|
928
|
+
case 'palace_has_any_star': {
|
|
929
|
+
const palace = findZiweiPalace(ziwei, predicate.palace);
|
|
930
|
+
if (!palace)
|
|
931
|
+
return false;
|
|
932
|
+
const starSet = new Set(predicate.stars);
|
|
933
|
+
return (palace.majorStars.some((s) => starSet.has(s.name)) ||
|
|
934
|
+
palace.minorStars.some((s) => starSet.has(s.name)));
|
|
935
|
+
}
|
|
936
|
+
case 'palace_has_sihua': {
|
|
937
|
+
const palace = findZiweiPalace(ziwei, predicate.palace);
|
|
938
|
+
if (!palace)
|
|
939
|
+
return false;
|
|
940
|
+
const tSet = new Set(predicate.transforms);
|
|
941
|
+
return (palace.majorStars.some((s) => s.sihuaTransform !== null && tSet.has(s.sihuaTransform)) ||
|
|
942
|
+
palace.minorStars.some((s) => s.sihuaTransform !== null && tSet.has(s.sihuaTransform)));
|
|
943
|
+
}
|
|
944
|
+
case 'palace_has_tuan_triet': {
|
|
945
|
+
const palace = findZiweiPalace(ziwei, predicate.palace);
|
|
946
|
+
return palace ? Boolean(palace.tuanTrung || palace.trietLo) : false;
|
|
947
|
+
}
|
|
948
|
+
case 'and':
|
|
949
|
+
return predicate.args.every((arg) => evalChartPredicate(arg, chart));
|
|
950
|
+
case 'or':
|
|
951
|
+
return predicate.args.some((arg) => evalChartPredicate(arg, chart));
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
export function extractChartRectificationFeatures(chart) {
|
|
955
|
+
const ziwei = chart.ziweiNamPhai;
|
|
956
|
+
const menhPalace = ziwei.palaces[ziwei.menhBranch] ?? findZiweiPalace(ziwei, 'Mệnh');
|
|
957
|
+
const thanPalace = ziwei.palaces.find((p) => p.isThan || p.branchIndex === ziwei.thanBranch) ??
|
|
958
|
+
ziwei.palaces[ziwei.thanBranch];
|
|
959
|
+
const palaceSignatureTags = {};
|
|
960
|
+
for (const p of ziwei.palaces) {
|
|
961
|
+
const tags = [];
|
|
962
|
+
for (const s of p.majorStars) {
|
|
963
|
+
tags.push(s.name);
|
|
964
|
+
if (s.sihuaTransform)
|
|
965
|
+
tags.push(s.sihuaTransform);
|
|
966
|
+
}
|
|
967
|
+
for (const s of p.minorStars) {
|
|
968
|
+
if (s.category !== 'ring')
|
|
969
|
+
tags.push(s.name);
|
|
970
|
+
if (s.sihuaTransform)
|
|
971
|
+
tags.push(s.sihuaTransform);
|
|
972
|
+
}
|
|
973
|
+
if (p.tuanTrung)
|
|
974
|
+
tags.push('Tuần');
|
|
975
|
+
if (p.trietLo)
|
|
976
|
+
tags.push('Triệt');
|
|
977
|
+
palaceSignatureTags[p.palaceName] = tags;
|
|
978
|
+
}
|
|
979
|
+
return {
|
|
980
|
+
shichenBranch: chart.temporal.trueSolarTime.shichenBranch,
|
|
981
|
+
shichenBranchVi: chart.temporal.trueSolarTime.shichenBranchVi,
|
|
982
|
+
menhBranch: ziwei.menhBranch,
|
|
983
|
+
thanBranch: ziwei.thanBranch,
|
|
984
|
+
thanPalaceName: thanPalace.palaceName,
|
|
985
|
+
cucNumber: ziwei.cucNumber,
|
|
986
|
+
menhVoChinhDieu: Boolean(menhPalace.voChinhDieu),
|
|
987
|
+
menhParity: (floorMod(ziwei.menhBranch, 2) === 0 ? 0 : 1),
|
|
988
|
+
menhMajorStars: menhPalace.majorStars.map((s) => s.name),
|
|
989
|
+
palaceSignatureTags,
|
|
990
|
+
};
|
|
991
|
+
}
|
|
992
|
+
function distributeIntTotal(total, indices, out) {
|
|
993
|
+
const n = indices.length;
|
|
994
|
+
if (n === 0)
|
|
995
|
+
return;
|
|
996
|
+
const base = Math.floor(total / n);
|
|
997
|
+
const rem = total % n;
|
|
998
|
+
for (let i = 0; i < n; i++) {
|
|
999
|
+
out[indices[i]] = base + (i < rem ? 1 : 0);
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
export function compileChartConditionedBank(candidateCharts, bank = PUBLIC_RECTIFICATION_QUESTIONS) {
|
|
1003
|
+
const chartByBranch = new Map();
|
|
1004
|
+
for (const chart of candidateCharts) {
|
|
1005
|
+
chartByBranch.set(floorMod(chart.temporal.trueSolarTime.shichenBranch, 12), chart);
|
|
1006
|
+
}
|
|
1007
|
+
return bank.map((q) => {
|
|
1008
|
+
const preds = q.chartPredicates ?? null;
|
|
1009
|
+
if (!preds) {
|
|
1010
|
+
return {
|
|
1011
|
+
id: q.id,
|
|
1012
|
+
category: q.category,
|
|
1013
|
+
ageCohort: q.ageCohort ?? null,
|
|
1014
|
+
minAge: q.minAge ?? null,
|
|
1015
|
+
maxAge: q.maxAge ?? null,
|
|
1016
|
+
promptVi: q.promptVi,
|
|
1017
|
+
options: q.options.map((o) => ({ id: o.id, labelVi: o.labelVi })),
|
|
1018
|
+
likelihoodMatrixMicro: q.likelihoodMatrixMicro,
|
|
1019
|
+
chartPredicates: null,
|
|
1020
|
+
};
|
|
1021
|
+
}
|
|
1022
|
+
const optCount = q.options.length;
|
|
1023
|
+
const cols = Array.from({ length: optCount }, () => new Array(12).fill(0));
|
|
1024
|
+
const naIndices = [];
|
|
1025
|
+
const subIndices = [];
|
|
1026
|
+
const otherwiseIndices = [];
|
|
1027
|
+
for (let o = 0; o < optCount; o++) {
|
|
1028
|
+
const optId = q.options[o].id;
|
|
1029
|
+
const pred = preds[optId];
|
|
1030
|
+
if (!pred && (optId === 'OPT_NA' || optId.endsWith('_NA'))) {
|
|
1031
|
+
naIndices.push(o);
|
|
1032
|
+
}
|
|
1033
|
+
else {
|
|
1034
|
+
subIndices.push(o);
|
|
1035
|
+
if (pred && pred.op === 'otherwise') {
|
|
1036
|
+
otherwiseIndices.push(o);
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
for (let b = 0; b < 12; b++) {
|
|
1041
|
+
const chart = chartByBranch.get(b);
|
|
1042
|
+
if (!chart) {
|
|
1043
|
+
const rawCol = [];
|
|
1044
|
+
for (let o = 0; o < optCount; o++) {
|
|
1045
|
+
rawCol.push(getRawLikelihood(q, o, q.options[o].id, b));
|
|
1046
|
+
}
|
|
1047
|
+
const norm = normalizeMicroProbs(rawCol);
|
|
1048
|
+
for (let o = 0; o < optCount; o++) {
|
|
1049
|
+
cols[o][b] = norm[o];
|
|
1050
|
+
}
|
|
1051
|
+
continue;
|
|
1052
|
+
}
|
|
1053
|
+
const naProbTotal = naIndices.length > 0 ? 80_000 : 0;
|
|
1054
|
+
const subBudget = MICRO_SCALE - naProbTotal;
|
|
1055
|
+
const col = new Array(optCount).fill(0);
|
|
1056
|
+
distributeIntTotal(naProbTotal, naIndices, col);
|
|
1057
|
+
const hitIndices = [];
|
|
1058
|
+
const missIndices = [];
|
|
1059
|
+
for (const o of subIndices) {
|
|
1060
|
+
const pred = preds[q.options[o].id];
|
|
1061
|
+
if (pred && evalChartPredicate(pred, chart)) {
|
|
1062
|
+
hitIndices.push(o);
|
|
1063
|
+
}
|
|
1064
|
+
else {
|
|
1065
|
+
missIndices.push(o);
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
if (hitIndices.length > 0 && missIndices.length > 0) {
|
|
1069
|
+
const hitBudget = naIndices.length > 0 ? 760_000 : 820_000;
|
|
1070
|
+
const missBudget = subBudget - hitBudget;
|
|
1071
|
+
distributeIntTotal(hitBudget, hitIndices, col);
|
|
1072
|
+
distributeIntTotal(missBudget, missIndices, col);
|
|
1073
|
+
}
|
|
1074
|
+
else if (hitIndices.length === 0 && otherwiseIndices.length > 0 && otherwiseIndices.length < subIndices.length) {
|
|
1075
|
+
const nonOther = subIndices.filter((o) => !otherwiseIndices.includes(o));
|
|
1076
|
+
const otherBudget = naIndices.length > 0 ? 720_000 : 780_000;
|
|
1077
|
+
const remBudget = subBudget - otherBudget;
|
|
1078
|
+
distributeIntTotal(otherBudget, otherwiseIndices, col);
|
|
1079
|
+
distributeIntTotal(remBudget, nonOther, col);
|
|
1080
|
+
}
|
|
1081
|
+
else {
|
|
1082
|
+
distributeIntTotal(subBudget, subIndices, col);
|
|
1083
|
+
}
|
|
1084
|
+
for (let o = 0; o < optCount; o++) {
|
|
1085
|
+
cols[o][b] = col[o];
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
const matrixRecord = {};
|
|
1089
|
+
for (let o = 0; o < optCount; o++) {
|
|
1090
|
+
matrixRecord[q.options[o].id] = cols[o];
|
|
1091
|
+
}
|
|
1092
|
+
return {
|
|
1093
|
+
id: q.id,
|
|
1094
|
+
category: q.category,
|
|
1095
|
+
ageCohort: q.ageCohort ?? null,
|
|
1096
|
+
minAge: q.minAge ?? null,
|
|
1097
|
+
maxAge: q.maxAge ?? null,
|
|
1098
|
+
promptVi: q.promptVi,
|
|
1099
|
+
options: q.options.map((o) => ({ id: o.id, labelVi: o.labelVi })),
|
|
1100
|
+
likelihoodMatrixMicro: matrixRecord,
|
|
1101
|
+
chartPredicates: preds,
|
|
1102
|
+
};
|
|
1103
|
+
});
|
|
1104
|
+
}
|
|
1105
|
+
export function createRectificationSessionFromCharts(candidateCharts, options) {
|
|
1106
|
+
if (candidateCharts.length === 0) {
|
|
1107
|
+
throw new Error('createRectificationSessionFromCharts requires at least 1 candidate ChartIR');
|
|
1108
|
+
}
|
|
1109
|
+
const sortedCharts = [...candidateCharts].sort((a, b) => a.temporal.trueSolarTime.shichenBranch - b.temporal.trueSolarTime.shichenBranch);
|
|
1110
|
+
const candidateBranches = Array.from(new Set(sortedCharts.map((c) => floorMod(c.temporal.trueSolarTime.shichenBranch, 12)))).sort((a, b) => a - b);
|
|
1111
|
+
const mode = options?.mode ?? 'UNKNOWN';
|
|
1112
|
+
const priorWeights = computeCertaintyPriorWeights(mode, options?.ambiguousBranches ?? [], options?.centerBranch, options?.birthTimeWindow);
|
|
1113
|
+
const maxQuestions = options?.maxQuestions ?? (mode === 'UNKNOWN' ? 6 : 5);
|
|
1114
|
+
const state = createInitialRectificationState({
|
|
1115
|
+
candidateBranches,
|
|
1116
|
+
priorWeights,
|
|
1117
|
+
maxQuestions,
|
|
1118
|
+
userAge: options?.userAge ?? null,
|
|
1119
|
+
});
|
|
1120
|
+
const compiledBank = compileChartConditionedBank(sortedCharts, options?.questionBank ?? PUBLIC_RECTIFICATION_QUESTIONS);
|
|
1121
|
+
const chartFeatures = sortedCharts.map(extractChartRectificationFeatures);
|
|
1122
|
+
return {
|
|
1123
|
+
state,
|
|
1124
|
+
compiledBank,
|
|
1125
|
+
chartFeatures,
|
|
1126
|
+
};
|
|
1127
|
+
}
|
|
1128
|
+
//# sourceMappingURL=rectify.js.map
|