mirror of
https://github.com/bitbaum/substrata.git
synced 2026-12-05 10:38:40 +01:00
Every country, company, resource and lab is filled against fixed boxes, each full/thin/empty by one written rule over the rows that exist (only stated and corroborated rows count). lib/dossier holds the boxes with named thresholds, allowed sources and a worked example; compute.ts derives each box from config, research/*.json and the candidate files; tasks.ts and scripts/research/gaps.ts emit one GapTask per empty or thin box with a prompt-ready instruction, ordered by demand then the owner's country list. Seams: RelationCounter for the graph, recordDemand for the answer ladder. Shown on /data/quality (priority countries) and on every /world/<iso2> page. GapTask.entity widens to SubjectId so a resource's boxes can be tasks. Claude-Session: https://claude.ai/code/session_018WZnBnaLP6RhwFkLxRTKgE Co-authored-by: Claude <noreply@anthropic.com>
84 lines
2.8 KiB
TypeScript
84 lines
2.8 KiB
TypeScript
/**
|
|
* Counting helpers every kind's counters share: how a row is graded when it
|
|
* carries no grade of its own, and a typed read of the USGS facility rows.
|
|
*/
|
|
import usgs from '@/research/usgs-producers.json';
|
|
import type { RowCounts } from '../definitions';
|
|
|
|
/**
|
|
* A curated row (committed to config by a person, or an agency table) reads
|
|
* `stated`; a candidate row (config/candidates, unpromoted) reads `lead`.
|
|
*/
|
|
export function gradeOf(row: { evidence?: string }): 'stated' | 'lead' {
|
|
return row.evidence === 'candidate' ? 'lead' : 'stated';
|
|
}
|
|
|
|
/** Counts by grade from a list of rows and a grader. */
|
|
export function tally<T>(
|
|
rows: readonly T[],
|
|
grade: (row: T) => 'stated' | 'corroborated' | 'derived' | 'lead',
|
|
basis: string,
|
|
extra: Partial<Pick<RowCounts, 'expected' | 'core'>> = {},
|
|
): RowCounts {
|
|
const c: RowCounts = { stated: 0, corroborated: 0, derived: 0, lead: 0, basis, ...extra };
|
|
for (const row of rows) c[grade(row)] += 1;
|
|
return c;
|
|
}
|
|
|
|
/** Add two counts, joining their bases. */
|
|
export function plus(a: RowCounts, b: RowCounts): RowCounts {
|
|
return {
|
|
stated: a.stated + b.stated,
|
|
corroborated: a.corroborated + b.corroborated,
|
|
derived: a.derived + b.derived,
|
|
lead: a.lead + b.lead,
|
|
expected: a.expected ?? b.expected,
|
|
core: a.core === undefined && b.core === undefined ? undefined : (a.core ?? 0) + (b.core ?? 0),
|
|
basis: [a.basis, b.basis].filter(Boolean).join('; '),
|
|
};
|
|
}
|
|
|
|
export const NOTHING: RowCounts = { stated: 0, corroborated: 0, derived: 0, lead: 0, basis: '' };
|
|
|
|
export interface UsgsFacilityRow {
|
|
iso2: string;
|
|
year: string;
|
|
resource: string;
|
|
companies: string;
|
|
location: string;
|
|
}
|
|
|
|
const FILE = usgs as unknown as {
|
|
countries: { iso2: string; year: string; facilities: Omit<UsgsFacilityRow, 'iso2' | 'year'>[] }[];
|
|
};
|
|
|
|
/** Every USGS Minerals Yearbook facility row, flattened once. */
|
|
export const USGS_ROWS: readonly UsgsFacilityRow[] = FILE.countries.flatMap((c) =>
|
|
c.facilities.map((f) => ({
|
|
iso2: c.iso2,
|
|
year: c.year,
|
|
resource: f.resource,
|
|
companies: f.companies,
|
|
location: f.location,
|
|
})),
|
|
);
|
|
|
|
/** The chapter year USGS printed for a country, or null when no chapter was read. */
|
|
export function usgsYear(iso2: string): string | null {
|
|
return FILE.countries.find((c) => c.iso2 === iso2)?.year ?? null;
|
|
}
|
|
|
|
/**
|
|
* The operating company a USGS row names: the text before its bracketed owner
|
|
* list. `Aluminium of Kazakhstan JSC [Eurasian Resources Group (ERG), 100%]`
|
|
* → `Aluminium of Kazakhstan JSC`. Some rows are cut off inside the bracket,
|
|
* so the bracket is never parsed here; the graph's operator parser does that.
|
|
*/
|
|
export function operatorOf(companies: string): string | null {
|
|
const name = companies
|
|
.split(/[[{(]/)[0]
|
|
.replace(/[,.;\s]+$/, '')
|
|
.trim();
|
|
return name.length >= 3 && !/^(na|n\.a|do|ditto|undetermined)$/i.test(name) ? name : null;
|
|
}
|