Files
hirnli/scripts/lib/theme-classifier.ts
CatoandClaude Opus 5.5 493f7f7cc0 feat(programmes): public funding programmes beside foundations and investors (#223)
* wip(programmes): the programme schema; the theme matcher moves into the app

Work in progress on a branch with no pull request yet. The theme matcher
(keywords and compounds of an organisation's themes) moves from scripts/lib
into src/lib/domain so public programmes are matched by the same rule as
foundations; the script re-exports it. The programme schema follows the
investor register: every fact with its page, date and verbatim sentence.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtMwPqoAJ2pHxdSn3t

* feat(programmes): public funding programmes beside foundations and investors

Seven federal, cantonal and EU programmes, read on 2026-12-01 from the
operators' own pages: Innosuisse (three instruments), the EIC Accelerator
via Euresearch, the federal media-literacy grants (Jugend und Medien), and
the canton of Zurich's integration projects and charitable fund. Every fact
carries the page, the day and the sentence it rests on; every sentence was
checked against the downloaded page. What a page does not say is named with
the reason; six programmes looked for and not entered are listed with why.

- Table fundraising_programmes (migration 0023), import script (dry run
  unless --write), and a re-check script that reads every cited page again
  and fails when a quoted sentence is gone.
- Workspace page: open programmes first, then those whose own words hit the
  organisation's themes, by the same keyword rule as the register screen
  (moved to src/lib/domain/theme-match.ts). A programme that asks for an
  organisation is marked for one that has no legal entity yet; programmes
  are now one of the kinds the legal-standing notice names.
- A grant Vorhaben links to the programmes as well as the foundations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtMwPqoAJ2pHxdSn3t

* feat(programmes): the seven programmes load with the deploy

Migration 0024 inserts the rows of data/programmes/seed-2026-12-01.json, so
the register is filled when this goes live and nobody runs an import by
hand. A test keeps the migration identical to the sourced data file; ON
CONFLICT DO NOTHING never puts a row back to this first reading once a
later re-check has updated it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtMwPqoAJ2pHxdSn3t

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-12-01 14:28:49 +01:00

190 lines
5.2 KiB
TypeScript

/**
* Theme Classifier — keyword classification of register text, per organisation.
*
* The keywords are the organisation's own: each theme in its vocabulary
* (`org_content['themes']`, authored in `org-context/<org>/themes.json`) lists
* the words that assign it, and combinations of word groups that must all
* appear. This module used to hold one organisation's keyword rules, so every
* organisation's register was classified into that organisation's fields of
* work. The rules moved into that organisation's vocabulary unchanged.
*
* The rest of this file (funder or operator, application method, foundation
* type) reads the foundation, not the applicant, and is the same for everyone.
*
* Used by: scripts/{zhaw-ingest, bulk-enrich, zefix-ingest, org-screen,
* pipeline-incremental, pipeline-graduate}.
*/
import type { ThemeVocabulary } from '../../src/lib/schemas/theme';
// ============================================================================
// THEMES — the organisation's own keywords
// ============================================================================
/**
* Which words assigned a theme. A compound hit is reported as its matched words
* joined with " + ", so every assignment can be checked by eye.
*/
// Shared with the app, which matches public programmes the same way.
export { themeHits, matchThemes } from '../../src/lib/domain/theme-match';
import { matchThemes } from '../../src/lib/domain/theme-match';
export function classifyThemes(
purposeLower: string,
nameLower: string,
themes: ThemeVocabulary,
): string[] {
return Object.keys(matchThemes(`${nameLower} ${purposeLower}`, themes));
}
// ============================================================================
// FUNDER / OPERATOR SCORING
// ============================================================================
const FUNDER_KW = [
'fördert',
'förderung',
'unterstützt',
'unterstützung',
'beiträge',
'zuwendungen',
'zuschüsse',
'finanzielle hilfe',
'projektförderung',
'stipend',
'ausricht',
'gewährt',
'vergab',
'vergibt',
'zusprech',
'finanziell',
'gemeinnützig',
];
const OPERATOR_KW = [
'betreibt',
'führt',
'unterhält',
'verwaltet',
'betrieb von',
'heim',
'klinik',
'spital',
'pflegeheim',
'altersheim',
'werkstatt',
'wohnheim',
'kinderheim',
'tagesstätte',
'krippe',
'trägerin',
'museum',
'theater',
];
export function scoreFunderOperator(purpose: string): { funder: number; operator: number } {
const p = purpose.toLowerCase();
let funder = 0;
let operator = 0;
for (const kw of FUNDER_KW) {
if (p.includes(kw)) funder++;
}
for (const kw of OPERATOR_KW) {
if (p.includes(kw)) operator++;
}
return { funder, operator };
}
// ============================================================================
// SLUG GENERATION
// ============================================================================
export function toSlug(name: string): string {
return name
.toLowerCase()
.replace(/ä/g, 'ae')
.replace(/ö/g, 'oe')
.replace(/ü/g, 'ue')
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '')
.substring(0, 60);
}
// ============================================================================
// APPLICATION METHOD DETECTION
// ============================================================================
export function detectApplicationMethod(
purpose: string,
): 'online' | 'email' | 'invitation' | 'unknown' {
const p = purpose.toLowerCase();
if (
p.includes('online') &&
(p.includes('gesuch') || p.includes('antrag') || p.includes('bewerbung'))
)
return 'online';
if (
p.includes('auf einladung') ||
p.includes('nur auf') ||
p.includes('kein gesuch') ||
p.includes('keine bewerbung')
)
return 'invitation';
if (
p.includes('schriftlich') ||
p.includes('per mail') ||
p.includes('per post') ||
p.includes('per e-mail')
)
return 'email';
return 'unknown';
}
// ============================================================================
// TYPE CLASSIFICATION (Robert Schmuki A/B/C/D)
// ============================================================================
export function classifyType(
purpose: string,
name: string,
funderScore: number,
operatorScore: number,
): 'A' | 'B' | 'C' | 'D' | 'network' {
const p = purpose.toLowerCase();
const n = name.toLowerCase();
if (n.includes('ag') || n.includes('group') || n.includes('corporate') || n.includes('company'))
return 'D';
if (
n.includes('verband') ||
n.includes('verein') ||
n.includes('netzwerk') ||
n.includes('dachverband')
)
return 'network';
if (
funderScore >= 3 &&
purpose.length > 200 &&
(p.includes('förderrichtlinien') ||
p.includes('programm') ||
p.includes('bewerbungsverfahren') ||
p.includes('ausschreibung') ||
p.includes('gesuche'))
)
return 'A';
if (
funderScore >= 2 &&
purpose.length > 100 &&
(p.includes('famili') ||
n.includes('famili') ||
/\b[A-Z][a-z]+-[A-Z][a-z]+\b/.test(name) ||
n.includes('-stiftung'))
)
return 'B';
if (operatorScore >= 3) return 'network';
return purpose.length > 200 && funderScore >= 2 ? 'B' : 'C';
}