diff --git a/worker/lib/extraction.js b/worker/lib/extraction.js new file mode 100644 index 0000000..765850f --- /dev/null +++ b/worker/lib/extraction.js @@ -0,0 +1,67 @@ +/** + * Sortie du modèle → extraction que dedoublonner et aiguiller consomment (lot 15, 21/11/2026). + * + * Le modèle rend un seul JSON par fiche : les champs d'avant (nom, description, type_suggere, + * ville, tags, confiance) et, depuis ce lot, objet, variantes, porteurs, point_de_vue, + * designations (schéma de départ : worker/test/mesure-modele.mjs), type_organisation_suggere + * et postures_suggerees. Rien de ce qu'il rend n'est cru sur parole : chaque champ est borné, + * typé, et ce qui sort du format tombe à null ou à la liste vide. + * + * Règle ajoutée : sans contenu de page, la confiance de l'extraction est « faible », quoi que + * dise le modèle (R6 étape 4 : jamais sur le libellé du soumetteur ; sans page, le texte du + * contributeur est la seule source de l'objet et du porteur). + */ + +const CONFIANCES = ['haute', 'moyenne', 'faible']; +const INDICES = ['indexable', 'agit', 'peut_cesser', 's_utilise', 'methode', 'personne', 'concept']; +const MAX_NOM = 200; +const MAX_LISTE = 10; +const MAX_DESIGNATIONS = 5; + +function texte(v, max = MAX_NOM) { + if (typeof v !== 'string') return null; + const s = v.replace(/\s+/g, ' ').trim(); + return s ? s.slice(0, max) : null; +} + +function listeTextes(v, max = MAX_LISTE) { + if (!Array.isArray(v)) return []; + return [...new Set(v.map(x => texte(x)).filter(Boolean))].slice(0, max); +} + +function designation(d) { + if (!d || typeof d !== 'object') return null; + const nom = texte(d.nom); + if (!nom) return null; + const indices = {}; + for (const k of INDICES) if (typeof d.indices?.[k] === 'boolean') indices[k] = d.indices[k]; + const sortie = { + nom, + variantes: listeTextes(d.variantes), + auteurs: listeTextes(d.auteurs), + date: typeof d.date === 'string' && /^\d{4}(-\d{2}(-\d{2})?)?$/.test(d.date) ? d.date : null, + indices, + licence: texte(d.licence), + depot: texte(d.depot, 500), + }; + if (Array.isArray(d.porteurs)) sortie.porteurs = listeTextes(d.porteurs); + return sortie; +} + +/** + * @param {object} sortie le JSON rendu par le modèle + * @param {{ avecPage: boolean }} contexte + * @returns {{ objet, variantes, porteurs, point_de_vue, designations, confiance }} + */ +export function extractionDepuisModele(sortie, { avecPage }) { + const s = sortie && typeof sortie === 'object' ? sortie : {}; + const confianceModele = CONFIANCES.includes(s.confiance) ? s.confiance : 'faible'; + return { + objet: texte(s.objet), + variantes: listeTextes(s.variantes), + porteurs: listeTextes(s.porteurs), + point_de_vue: s.point_de_vue === 'porteur' || s.point_de_vue === 'tiers' ? s.point_de_vue : null, + designations: (Array.isArray(s.designations) ? s.designations : []).map(designation).filter(Boolean).slice(0, MAX_DESIGNATIONS), + confiance: avecPage ? confianceModele : 'faible', + }; +} diff --git a/worker/lib/taxonomie.js b/worker/lib/taxonomie.js new file mode 100644 index 0000000..6fff65e --- /dev/null +++ b/worker/lib/taxonomie.js @@ -0,0 +1,54 @@ +/** + * Proposition de type d'organisation et de postures par le worker (lot 15, 21/11/2026). + * + * Le vocabulaire est celui du site (`utils/taxonomie.ts`, taxonomie B4b) : sept types, sept + * postures. Le worker est un paquet à part, déployé seul sur le VPS : il le recopie, et + * `worker/test/proposition.test.js` garde l'égalité avec le fichier du site. + * + * Décision du lot 15 (LOT-B4b : « le worker le proposera, un humain valide ») : la proposition + * ne s'écrit JAMAIS dans les colonnes `type_organisation` ni `postures`. Elle va dans le JSON + * de `ai_raw_output` ; un modérateur la recopie s'il la valide. Hors vocabulaire : null (type) + * ou retirée (postures). + */ + +export const TYPES_ORGANISATION = ['institution', 'association', 'syndicat', 'agence', 'formation', 'media', 'autre']; +export const POSTURES = ['regenerative', 'bas-carbone', 'cooperative', 'patrimoine', 'ecoconstruction', 'low-tech', 'participatif']; + +/** Les libellés du site, acceptés en entrée (le modèle peut rendre « Média » ou « Coopératif »). */ +export const LIBELLES_TYPE = { + institution: 'Institution', association: 'Association', syndicat: 'Syndicat', agence: 'Agence', + formation: 'Formation', media: 'Média', autre: 'Autre', +}; +export const LIBELLES_POSTURE = { + regenerative: 'Régénératif', 'bas-carbone': 'Bas carbone', cooperative: 'Coopératif', patrimoine: 'Patrimoine', + ecoconstruction: 'Écoconstruction', 'low-tech': 'Low-tech', participatif: 'Participatif', +}; + +const forme = v => String(v ?? '') + .normalize('NFD').replace(/[̀-ͯ]/g, '') + .toLowerCase().trim() + .replace(/[\s_]+/g, '-'); + +function index(valeurs, libelles) { + const table = new Map(); + for (const v of valeurs) { + table.set(forme(v), v); + table.set(forme(libelles[v]), v); + } + return table; +} +const PAR_TYPE = index(TYPES_ORGANISATION, LIBELLES_TYPE); +const PAR_POSTURE = index(POSTURES, LIBELLES_POSTURE); + +/** Une valeur rendue par le modèle → un type du vocabulaire, ou null. */ +export function proposerType(brut) { + if (typeof brut !== 'string') return null; + return PAR_TYPE.get(forme(brut)) ?? null; +} + +/** Une liste rendue par le modèle → les postures du vocabulaire, sans doublon, dans l'ordre du site. */ +export function proposerPostures(brut) { + if (!Array.isArray(brut)) return []; + const vues = new Set(brut.filter(x => typeof x === 'string').map(x => PAR_POSTURE.get(forme(x))).filter(Boolean)); + return POSTURES.filter(p => vues.has(p)); +} diff --git a/worker/test/proposition.test.js b/worker/test/proposition.test.js new file mode 100644 index 0000000..1fb62fc --- /dev/null +++ b/worker/test/proposition.test.js @@ -0,0 +1,100 @@ +/** + * Lot 15 — la proposition de type et de postures reste dans le vocabulaire du site (sinon null), + * et l'extraction rendue par le modèle est bornée avant d'entrer dans R6 et R3. + */ +import { describe, it } from 'node:test'; +import assert from 'node:assert/strict'; +import { readFileSync } from 'node:fs'; +import { dirname, join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { TYPES_ORGANISATION, POSTURES, LIBELLES_TYPE, LIBELLES_POSTURE, proposerType, proposerPostures } from '../lib/taxonomie.js'; +import { extractionDepuisModele } from '../lib/extraction.js'; + +const ici = dirname(fileURLToPath(import.meta.url)); +const SITE = readFileSync(join(ici, '..', '..', 'utils', 'taxonomie.ts'), 'utf-8'); + +/** Lit `export const NOM = [ ... ] as const` dans le fichier du site. */ +function tableauDuSite(nom) { + const m = SITE.match(new RegExp(`export const ${nom} = \\[([^\\]]*)\\]`)); + assert.ok(m, `${nom} introuvable dans utils/taxonomie.ts`); + return [...m[1].matchAll(/'([^']+)'/g)].map(x => x[1]); +} +/** Lit `export const NOM: Record<…> = { clé: 'libellé', … }` dans le fichier du site. */ +function libellesDuSite(nom) { + const m = SITE.match(new RegExp(`export const ${nom}[^=]*= \\{([^}]*)\\}`)); + assert.ok(m, `${nom} introuvable dans utils/taxonomie.ts`); + return Object.fromEntries([...m[1].matchAll(/'?([a-z-]+)'?:\s*'([^']+)'/g)].map(x => [x[1], x[2]])); +} + +describe('lot 15 · vocabulaire du worker = vocabulaire du site (utils/taxonomie.ts)', () => { + it('sept types, sept postures, mêmes valeurs dans le même ordre', () => { + assert.deepEqual(TYPES_ORGANISATION, tableauDuSite('TYPES_ORGANISATION')); + assert.deepEqual(POSTURES, tableauDuSite('POSTURES')); + assert.equal(TYPES_ORGANISATION.length, 7); + assert.equal(POSTURES.length, 7); + }); + + it('mêmes libellés', () => { + assert.deepEqual(LIBELLES_TYPE, libellesDuSite('LIBELLES_TYPE')); + assert.deepEqual(LIBELLES_POSTURE, libellesDuSite('LIBELLES_POSTURE')); + }); +}); + +describe('lot 15 · type et postures proposés', () => { + it('type : valeur ou libellé du vocabulaire, sinon null', () => { + assert.equal(proposerType('association'), 'association'); + assert.equal(proposerType('Média'), 'media'); + assert.equal(proposerType(' Syndicat '), 'syndicat'); + for (const hors of ['collectif', 'ecosysteme', 'reseau', 'Association loi 1901', '', null, undefined, 42, ['association'], { type: 'agence' }]) { + assert.equal(proposerType(hors), null, JSON.stringify(hors)); + } + }); + + it('postures : seules celles du vocabulaire, sans doublon, dans l’ordre du site', () => { + assert.deepEqual(proposerPostures(['Low tech', 'participatif', 'décroissance', 'Coopératif', 'participatif', 7]), ['cooperative', 'low-tech', 'participatif']); + assert.deepEqual(proposerPostures('regenerative'), []); + assert.deepEqual(proposerPostures(null), []); + assert.deepEqual(proposerPostures(['Bas carbone', 'Écoconstruction', 'Régénératif']), ['regenerative', 'bas-carbone', 'ecoconstruction']); + }); +}); + +describe('lot 15 · extraction rendue par le modèle, bornée', () => { + it('champs hors format → null ou liste vide ; indices booléens seulement', () => { + const x = extractionDepuisModele({ + objet: ' Portal.Place ', variantes: ['Portal Place', 'Portal Place', 3], porteurs: 'Mike Gilliland', + point_de_vue: 'officiel', confiance: 'certaine', + designations: [ + { nom: 'Portal.Place', indices: { agit: true, peut_cesser: 'oui', methode: false, inconnu: true }, date: 'hier' }, + { indices: { agit: true } }, + 'texte', + ], + }, { avecPage: true }); + assert.equal(x.objet, 'Portal.Place'); + assert.deepEqual(x.variantes, ['Portal Place']); + assert.deepEqual(x.porteurs, []); + assert.equal(x.point_de_vue, null); + assert.equal(x.confiance, 'faible', 'confiance hors vocabulaire → faible'); + assert.equal(x.designations.length, 1); + assert.deepEqual(x.designations[0].indices, { agit: true, methode: false }); + assert.equal(x.designations[0].date, null); + }); + + it('sans page, la confiance est faible quoi que dise le modèle', () => { + assert.equal(extractionDepuisModele({ objet: 'X', confiance: 'haute' }, { avecPage: false }).confiance, 'faible'); + assert.equal(extractionDepuisModele({ objet: 'X', confiance: 'haute' }, { avecPage: true }).confiance, 'haute'); + }); + + it('au plus cinq désignations, noms bornés à 200 caractères', () => { + const x = extractionDepuisModele({ objet: 'a'.repeat(500), designations: Array.from({ length: 9 }, (_, i) => ({ nom: `d${i}`, indices: {} })) }, { avecPage: true }); + assert.equal(x.objet.length, 200); + assert.equal(x.designations.length, 5); + }); + + it('sortie absente ou illisible → extraction vide, jamais d’exception', () => { + for (const s of [null, undefined, 'texte', 42, []]) { + const x = extractionDepuisModele(s, { avecPage: true }); + assert.equal(x.objet, null); + assert.deepEqual(x.designations, []); + } + }); +});