@tekkare/romulus 0.1.348 → 0.1.349
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/dist/module.json +1 -1
- package/dist/runtime/components/Drawer.vue +10 -3
- package/dist/runtime/components/ProjectCreateDialog.vue +1 -1
- package/dist/runtime/components/ProjectResources.vue +63 -3
- package/dist/runtime/utils/projects.d.ts +9 -0
- package/dist/runtime/utils/projects.js +2 -1
- package/layer/app/components/oip-science/resources/Phrc.vue +161 -8
- package/layer/app/components/project/Guided.vue +1475 -0
- package/layer/app/components/project/NewButton.vue +21 -17
- package/layer/app/oip-defaults/projects.ts +13 -3
- package/layer/app/oip-drugs/esIndices.ts +5 -0
- package/layer/app/oip-drugs/kinds.ts +12 -0
- package/layer/app/oip-guided/families.ts +984 -0
- package/layer/app/oip-guided/shared.ts +179 -0
- package/layer/app/oip-guided/subjects.ts +384 -0
- package/layer/app/utils/oipResources.ts +2 -1
- package/package.json +1 -1
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Briques communes de la creation guidee de projet (cf.
|
|
3
|
+
* components/project/Guided.vue) : types, appel au proxy du layer, clauses
|
|
4
|
+
* de recherche. Portage de ce que fr_drugs_app (useHubrProjectSuggestions) et
|
|
5
|
+
* France Augmented Science (useProjectSuggestions) ecrivaient chacun.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { OipEsCluster } from "../utils/oipResources";
|
|
9
|
+
|
|
10
|
+
export type GuidedLang = "fr" | "en";
|
|
11
|
+
|
|
12
|
+
/** Ce sur quoi porte un sujet du projet. */
|
|
13
|
+
export type GuidedFocus = "disease" | "molecule" | "brand";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Un sujet du projet. Une maladie porte son code quand elle vient d'une
|
|
17
|
+
* suggestion : CIM-10 (C50, C509) ou ORPHA (589). Le code part dans ses
|
|
18
|
+
* propres clauses, le libelle dans les clauses texte.
|
|
19
|
+
*/
|
|
20
|
+
export interface GuidedSubject {
|
|
21
|
+
focus: GuidedFocus;
|
|
22
|
+
label: string;
|
|
23
|
+
code?: string;
|
|
24
|
+
system?: "icd10" | "orpha";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Comment une fiche rejoint le sujet :
|
|
29
|
+
* - `direct` : le sujet est dans la fiche (maladie, marque, molecule, code) ;
|
|
30
|
+
* - `mention` : le sujet n'y est cite qu'en passant (population ciblee, titre) ;
|
|
31
|
+
* - `deduced` : la fiche passe par une marque ou un essai retenu.
|
|
32
|
+
*/
|
|
33
|
+
export type GuidedMatchKind = "direct" | "mention" | "deduced";
|
|
34
|
+
|
|
35
|
+
/** Un endroit ou la correspondance se lit : un libelle, des valeurs ou des extraits en `<mark>`. */
|
|
36
|
+
export interface GuidedEvidence {
|
|
37
|
+
label: string;
|
|
38
|
+
values: string[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Une fiche proposee : l'`_id` Elasticsearch exact que l'API des projets verifie (R-02). */
|
|
42
|
+
export interface GuidedRow {
|
|
43
|
+
id: string;
|
|
44
|
+
title: string;
|
|
45
|
+
subtitle?: string;
|
|
46
|
+
kind: GuidedMatchKind;
|
|
47
|
+
evidence: GuidedEvidence[];
|
|
48
|
+
/** Le document relu, pour le lien vers sa fiche. */
|
|
49
|
+
source: Record<string, any>;
|
|
50
|
+
/**
|
|
51
|
+
* La marque sous laquelle la revue range la fiche, pour les familles que
|
|
52
|
+
* fr_drugs_app montre par marque (prix, ANSM...) : une ligne par marque,
|
|
53
|
+
* depliable presentation par presentation.
|
|
54
|
+
*/
|
|
55
|
+
group?: string;
|
|
56
|
+
/** Ce qui decrit la marque dans la revue (molecule, laboratoire). */
|
|
57
|
+
groupHint?: string;
|
|
58
|
+
/** Une date de la fiche (dernier prix, autorisation), la plus recente sort sur la marque. */
|
|
59
|
+
date?: string;
|
|
60
|
+
/**
|
|
61
|
+
* Les avis HAS (`_id` des fiches FrHasBrand) par lesquels la fiche a ete
|
|
62
|
+
* deduite : decocher ces avis decoche la fiche, tant qu'on n'y a pas touche.
|
|
63
|
+
*/
|
|
64
|
+
viaHas?: string[];
|
|
65
|
+
/** Deduite aussi d'une marque ou d'une molecule choisie comme sujet : elle tient sans avis. */
|
|
66
|
+
viaFixed?: boolean;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Taille maximale d'une liste : au-dela, la relecture n'est plus possible. */
|
|
70
|
+
export const LIMIT = 100;
|
|
71
|
+
|
|
72
|
+
/** Les valeurs que les index ecrivent pour « rien » : elles ne s'affichent pas. */
|
|
73
|
+
const EMPTY = new Set(["NONE", "NULL", "INCONNU", "UNKNOWN", "NAN"]);
|
|
74
|
+
|
|
75
|
+
export const clean = (value: unknown) => {
|
|
76
|
+
const text = String(value ?? "").trim();
|
|
77
|
+
return text && !EMPTY.has(text.toUpperCase()) ? text : "";
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/** Un champ tantot objet, tantot tableau, tantot absent : toujours un tableau. */
|
|
81
|
+
export const list = (value: unknown): any[] => (value == null ? [] : Array.isArray(value) ? value : [value]);
|
|
82
|
+
|
|
83
|
+
export const uniq = (values: unknown[]) => [...new Set(values.flat().map(clean).filter(Boolean))];
|
|
84
|
+
|
|
85
|
+
export const joinDot = (...values: unknown[]) => values.map(clean).filter(Boolean).join(" · ");
|
|
86
|
+
|
|
87
|
+
/** Sans accent ni casse, pour comparer ce qu'on tape a ce qu'on lit. */
|
|
88
|
+
export function fold(text: string) {
|
|
89
|
+
return String(text ?? "")
|
|
90
|
+
.normalize("NFD")
|
|
91
|
+
.replace(/[\u0300-\u036f]/g, "")
|
|
92
|
+
.toLowerCase()
|
|
93
|
+
.trim();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Majuscules des index (« TUMEUR MALIGNE DU SEIN ») en casse de phrase. */
|
|
97
|
+
export function sentence(text: string) {
|
|
98
|
+
const lower = String(text ?? "").toLowerCase();
|
|
99
|
+
return lower.charAt(0).toUpperCase() + lower.slice(1);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Echappe une saisie pour une `regexp` Elasticsearch. */
|
|
103
|
+
export function escapeRegexp(value: string) {
|
|
104
|
+
return value.replace(/[.?+*|{}[\]()"\\#@&<>~]/g, "\\$&");
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Une requete `_search` par le proxy du layer (`/api/oip/es`), ou rien si elle echoue. */
|
|
108
|
+
export async function guidedSearch(cluster: OipEsCluster, index: string, body: Record<string, unknown>): Promise<any> {
|
|
109
|
+
return await $fetch(`/api/oip/es?cluster=${cluster}&index=${encodeURIComponent(index)}`, { method: "POST", body });
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export async function guidedHits(cluster: OipEsCluster, index: string, body: Record<string, unknown>): Promise<any[]> {
|
|
113
|
+
const response = await guidedSearch(cluster, index, body);
|
|
114
|
+
return response?.hits?.hits ?? [];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Texte libre sur plusieurs mots : la phrase classe, le flou rattrape les
|
|
119
|
+
* fautes et les pluriels.
|
|
120
|
+
*/
|
|
121
|
+
export function textMatch(term: string, fields: string[]) {
|
|
122
|
+
return {
|
|
123
|
+
bool: {
|
|
124
|
+
should: [
|
|
125
|
+
{ multi_match: { query: term, fields, type: "phrase", slop: 3, boost: 2 } },
|
|
126
|
+
{
|
|
127
|
+
multi_match: {
|
|
128
|
+
query: term,
|
|
129
|
+
fields,
|
|
130
|
+
type: "best_fields",
|
|
131
|
+
operator: "and",
|
|
132
|
+
fuzziness: "AUTO",
|
|
133
|
+
prefix_length: 2,
|
|
134
|
+
boost: 0.3,
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
],
|
|
138
|
+
minimum_should_match: 1,
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Surlignage en `<mark>`, comme les cartes de resultats. */
|
|
144
|
+
export const highlightOf = (fields: string[]) => ({
|
|
145
|
+
pre_tags: ["<mark>"],
|
|
146
|
+
post_tags: ["</mark>"],
|
|
147
|
+
fields: Object.fromEntries(fields.map((field) => [field, { number_of_fragments: 2, fragment_size: 160 }])),
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
/** Les extraits surlignes d'un document (ou d'un inner hit), champ par champ. */
|
|
151
|
+
export function highlightsOf(hit: any, fields?: string[]) {
|
|
152
|
+
const source: Record<string, string[]> = hit?.highlight ?? {};
|
|
153
|
+
return uniq((fields ?? Object.keys(source)).flatMap((field) => source[field] ?? []));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Les ecritures d'un code CIM-10 : le dictionnaire des hospitalisations
|
|
158
|
+
* ecrit « C509 », les essais et les publications « C50.9 ».
|
|
159
|
+
*/
|
|
160
|
+
export function icdForms(code: string) {
|
|
161
|
+
const plain = code.replace(".", "").toUpperCase();
|
|
162
|
+
return plain.length > 3 ? [plain, `${plain.slice(0, 3)}.${plain.slice(3)}`] : [plain];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Le code CIM-10 en prefixe, sous ses deux ecritures : « C50 » couvre « C50.9 ». */
|
|
166
|
+
export function icdPrefix(field: string, code: string) {
|
|
167
|
+
const forms = icdForms(code).map((value) => ({ prefix: { [field]: { value, case_insensitive: true } } }));
|
|
168
|
+
return forms.length === 1 ? forms[0]! : { bool: { should: forms, minimum_should_match: 1 } };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Une clause par niveau `nested` : `diseases` puis `diseases.icd10`. */
|
|
172
|
+
export function icd10Nested(path: string, code: string) {
|
|
173
|
+
return {
|
|
174
|
+
nested: {
|
|
175
|
+
path,
|
|
176
|
+
query: { nested: { path: `${path}.icd10`, query: icdPrefix(`${path}.icd10.icd_code`, code) } },
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
}
|
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Suggestions de la barre de sujets de la creation guidee : maladies,
|
|
3
|
+
* molecules et marques, dans une seule liste groupee.
|
|
4
|
+
*
|
|
5
|
+
* - Maladies, CIM-10 : le dictionnaire des hospitalisations par diagnostic
|
|
6
|
+
* (libelles FR et EN, codes a 3 et 4 caracteres), interroge a chaque frappe.
|
|
7
|
+
* - Maladies rares : les tags Orphanet des avis HAS (FR et EN) et des essais
|
|
8
|
+
* NCT (EN), charges une fois puis filtres dans le navigateur, comme
|
|
9
|
+
* l'assistant de fr_drugs_app. Le code relie les deux langues.
|
|
10
|
+
* - Molecules et marques : le repertoire des marques de fr_drugs_app
|
|
11
|
+
* (`brand`, `atc5_french_label` / `atc5_english_label`).
|
|
12
|
+
*
|
|
13
|
+
* Une source inaccessible ne casse rien : elle ne propose simplement rien.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { ES_INDEX } from "../oip-drugs/esIndices";
|
|
17
|
+
import { clean, fold, guidedSearch, sentence, type GuidedLang, type GuidedSubject } from "./shared";
|
|
18
|
+
|
|
19
|
+
export interface GuidedSubjectSuggestion extends GuidedSubject {
|
|
20
|
+
/** La ligne sous le libelle : le code, la molecule d'une marque... */
|
|
21
|
+
hint: string;
|
|
22
|
+
/** Cle stable : un meme sujet ne se choisit qu'une fois. */
|
|
23
|
+
key: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface GuidedSubjectGroups {
|
|
27
|
+
diseases: GuidedSubjectSuggestion[];
|
|
28
|
+
molecules: GuidedSubjectSuggestion[];
|
|
29
|
+
brands: GuidedSubjectSuggestion[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const MAX = 6;
|
|
33
|
+
|
|
34
|
+
export const subjectKey = (subject: GuidedSubject) =>
|
|
35
|
+
subject.code ? `${subject.focus}:${subject.system}:${subject.code}` : `${subject.focus}:${fold(subject.label)}`;
|
|
36
|
+
|
|
37
|
+
/** Exact, puis debut, puis debut de mot, puis contenu : 4 si rien. */
|
|
38
|
+
function rank(text: string, query: string) {
|
|
39
|
+
const value = fold(text);
|
|
40
|
+
if (value === query) return 0;
|
|
41
|
+
if (value.startsWith(query)) return 1;
|
|
42
|
+
if (new RegExp(`(^|\\W)${query.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}`).test(value)) return 2;
|
|
43
|
+
if (value.includes(query)) return 3;
|
|
44
|
+
return 4;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Les mots porteurs de la saisie (« maladie de », « syndrome »... ne disent rien). */
|
|
48
|
+
const FILLER = new Set(["de", "des", "du", "la", "le", "les", "l", "d", "of", "the", "and", "et"]);
|
|
49
|
+
|
|
50
|
+
function keywords(query: string) {
|
|
51
|
+
return query
|
|
52
|
+
.split(/[^a-z0-9]+/)
|
|
53
|
+
.filter((word) => word.length > 1 && !FILLER.has(word))
|
|
54
|
+
.map((word) => word.replace(/[sx]$/, ""));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Aucune faute sous 5 lettres, une jusqu'a 7, deux au-dela. */
|
|
58
|
+
function allowedTypos(word: string) {
|
|
59
|
+
if (word.length < 5) return 0;
|
|
60
|
+
return word.length < 8 ? 1 : 2;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Distance d'edition avec transposition, arretee des qu'elle depasse `max`. */
|
|
64
|
+
function typos(a: string, b: string, max: number) {
|
|
65
|
+
if (Math.abs(a.length - b.length) > max) return max + 1;
|
|
66
|
+
let prev2: number[] = [];
|
|
67
|
+
let prev = Array.from({ length: b.length + 1 }, (_, j) => j);
|
|
68
|
+
for (let i = 1; i <= a.length; i++) {
|
|
69
|
+
const row = [i];
|
|
70
|
+
let best = i;
|
|
71
|
+
for (let j = 1; j <= b.length; j++) {
|
|
72
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
73
|
+
let value = Math.min(prev[j]! + 1, row[j - 1]! + 1, prev[j - 1]! + cost);
|
|
74
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) value = Math.min(value, prev2[j - 2]! + 1);
|
|
75
|
+
row.push(value);
|
|
76
|
+
best = Math.min(best, value);
|
|
77
|
+
}
|
|
78
|
+
if (best > max) return max + 1;
|
|
79
|
+
prev2 = prev;
|
|
80
|
+
prev = row;
|
|
81
|
+
}
|
|
82
|
+
return prev[b.length]!;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Chaque mot porteur retrouve un mot du libelle, a quelques fautes pres. */
|
|
86
|
+
function matchesFuzzy(text: string, words: string[]) {
|
|
87
|
+
if (!words.length || words.every((word) => allowedTypos(word) === 0)) return false;
|
|
88
|
+
const tokens = text.split(/[^a-z0-9]+/).filter(Boolean);
|
|
89
|
+
return words.every((word, index) => {
|
|
90
|
+
const max = allowedTypos(word);
|
|
91
|
+
const typing = index === words.length - 1;
|
|
92
|
+
return tokens.some(
|
|
93
|
+
(token) =>
|
|
94
|
+
token.startsWith(word) ||
|
|
95
|
+
typos(word, token, max) <= max ||
|
|
96
|
+
(typing && token.length > word.length && typos(word, token.slice(0, word.length), max) <= max),
|
|
97
|
+
);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
interface RareTag {
|
|
102
|
+
label: string;
|
|
103
|
+
code: string;
|
|
104
|
+
lang: GuidedLang;
|
|
105
|
+
/** 1 : avis HAS, 2 : essais. */
|
|
106
|
+
priority: number;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** « Lymphome (ORPHA:223735) » en nom et ORPHAcode. */
|
|
110
|
+
function splitOrpha(value: string) {
|
|
111
|
+
const match = value.match(/^(.*?)\s*\(ORPHA:(\d+)\)\s*$/);
|
|
112
|
+
return match ? { label: match[1]!.trim(), code: match[2]! } : { label: value.trim(), code: "" };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
let rareTags: Promise<RareTag[]> | null = null;
|
|
116
|
+
|
|
117
|
+
function loadRare(): Promise<RareTag[]> {
|
|
118
|
+
if (rareTags) return rareTags;
|
|
119
|
+
const of = (aggregation: any, lang: GuidedLang, priority: number): RareTag[] =>
|
|
120
|
+
(aggregation?.buckets ?? [])
|
|
121
|
+
.map((bucket: any) => ({ ...splitOrpha(String(bucket.key)), lang, priority }))
|
|
122
|
+
.filter((tag: RareTag) => tag.code && tag.label);
|
|
123
|
+
|
|
124
|
+
const has = guidedSearch("drugs", ES_INDEX.BRAND_HAS, {
|
|
125
|
+
size: 0,
|
|
126
|
+
aggs: {
|
|
127
|
+
fr: { terms: { field: "rare_diseases.concat_disease_name_orpha_code.keyword", size: 2000 } },
|
|
128
|
+
en: { terms: { field: "rare_diseases_en.concat_disease_name_orpha_code.keyword", size: 2000 } },
|
|
129
|
+
},
|
|
130
|
+
}).then((response) => [...of(response?.aggregations?.fr, "fr", 1), ...of(response?.aggregations?.en, "en", 1)]);
|
|
131
|
+
|
|
132
|
+
const trials = guidedSearch("drugs", ES_INDEX.TRIAL_NCT, {
|
|
133
|
+
size: 0,
|
|
134
|
+
aggs: {
|
|
135
|
+
rare: {
|
|
136
|
+
nested: { path: "rare_diseases" },
|
|
137
|
+
aggs: { labels: { terms: { field: "rare_diseases.concat_disease_name_orpha_code.keyword", size: 5000 } } },
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
}).then((response) => of(response?.aggregations?.rare?.labels, "en", 2));
|
|
141
|
+
|
|
142
|
+
rareTags = Promise.allSettled([has, trials]).then((results) => {
|
|
143
|
+
const all = results.flatMap((result) => (result.status === "fulfilled" ? result.value : []));
|
|
144
|
+
// Rien de lu : nouvel essai a la prochaine frappe.
|
|
145
|
+
if (!all.length) rareTags = null;
|
|
146
|
+
return all;
|
|
147
|
+
});
|
|
148
|
+
return rareTags;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async function rareDiseases(query: string, lang: GuidedLang): Promise<GuidedSubjectSuggestion[]> {
|
|
152
|
+
const tags = await loadRare();
|
|
153
|
+
const words = keywords(query);
|
|
154
|
+
const byCode = new Map<string, RareTag[]>();
|
|
155
|
+
for (const tag of tags) byCode.set(tag.code, [...(byCode.get(tag.code) ?? []), tag]);
|
|
156
|
+
return tags
|
|
157
|
+
.map((tag) => {
|
|
158
|
+
const score = rank(tag.label, query);
|
|
159
|
+
if (score < 4) return { tag, score };
|
|
160
|
+
return { tag, score: matchesFuzzy(fold(tag.label), words) ? 3.5 : 4 };
|
|
161
|
+
})
|
|
162
|
+
.filter((item) => item.score < 4)
|
|
163
|
+
.sort((a, b) => a.score - b.score || a.tag.priority - b.tag.priority || a.tag.label.length - b.tag.label.length)
|
|
164
|
+
// Une maladie ne sort qu'une fois, sous son nom dans la langue de l'ecran
|
|
165
|
+
// quand il existe : « myasthenia gravis » propose « Myasthenie grave ».
|
|
166
|
+
.filter((item, index, all) => all.findIndex((other) => other.tag.code === item.tag.code) === index)
|
|
167
|
+
.slice(0, MAX)
|
|
168
|
+
.map(({ tag }) => {
|
|
169
|
+
const shown = (byCode.get(tag.code) ?? []).filter((other) => other.lang === lang).sort((a, b) => a.priority - b.priority)[0] ?? tag;
|
|
170
|
+
const subject: GuidedSubject = { focus: "disease", label: shown.label, code: tag.code, system: "orpha" };
|
|
171
|
+
return { ...subject, hint: `ORPHA:${tag.code}`, key: subjectKey(subject) };
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async function icd10Diseases(query: string, lang: GuidedLang): Promise<GuidedSubjectSuggestion[]> {
|
|
176
|
+
const code = query.replace(/[.\s]/g, "").toUpperCase();
|
|
177
|
+
const isCode = /^[A-Z]\d{1,3}[A-Z]?$/.test(code);
|
|
178
|
+
// Le dictionnaire dit « tumeur maligne du sein », on tape « cancer du sein » :
|
|
179
|
+
// sans le mot « cancer », l'organe cherche parmi les tumeurs (codes C).
|
|
180
|
+
const cancerWords = /\b(cancers?|tumeurs?|malignes?|carcinomes?|neoplasms?|malignant|tumou?rs?)\b/gi;
|
|
181
|
+
const organ = query
|
|
182
|
+
.replace(cancerWords, " ")
|
|
183
|
+
.replace(/\b(de la|de l'|du|des|de|d'|of the|of)\b/gi, " ")
|
|
184
|
+
.replace(/\s+/g, " ")
|
|
185
|
+
.trim();
|
|
186
|
+
const cancerClause =
|
|
187
|
+
organ.length >= 2 && new RegExp(cancerWords.source, "i").test(query)
|
|
188
|
+
? [
|
|
189
|
+
{
|
|
190
|
+
bool: {
|
|
191
|
+
must: [{ multi_match: { query: organ, fields: ["diagnosis_label_fr", "diagnosis_label"], operator: "and", fuzziness: "AUTO", prefix_length: 2 } }],
|
|
192
|
+
filter: [{ prefix: { "diagnosis_code.keyword": "C" } }],
|
|
193
|
+
boost: 2,
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
]
|
|
197
|
+
: [];
|
|
198
|
+
const response = await guidedSearch("drugs", ES_INDEX.WW_HOSPITALIZATION_DIAGNOSIS, {
|
|
199
|
+
size: MAX,
|
|
200
|
+
_source: ["diagnosis_code", "diagnosis_label", "diagnosis_label_fr"],
|
|
201
|
+
// Un document par code : le dictionnaire repete chaque code par pays et
|
|
202
|
+
// par niveau de diagnostic.
|
|
203
|
+
collapse: { field: "diagnosis_code.keyword" },
|
|
204
|
+
query: {
|
|
205
|
+
bool: {
|
|
206
|
+
// Pas de filtre sur `diagnosis_label_fr` : la copie de prod n'a pas encore les
|
|
207
|
+
// libelles FR (seule celle de staging les porte). Le libelle EN prend le relais.
|
|
208
|
+
// Codes CIM-10 de l'OMS seulement (C50, C509) : les variantes nationales
|
|
209
|
+
// (Z391A, danoises) ne se retrouvent dans aucun autre index.
|
|
210
|
+
filter: [{ term: { "code_type.keyword": "icd_10" } }, { regexp: { "diagnosis_code.keyword": "[A-Z][0-9]{2}[0-9]?" } }],
|
|
211
|
+
should: [
|
|
212
|
+
...(isCode ? [{ prefix: { "diagnosis_code.keyword": { value: code, boost: 4 } } }] : []),
|
|
213
|
+
...cancerClause,
|
|
214
|
+
{ match_phrase_prefix: { diagnosis_label_fr: { query, boost: 3 } } },
|
|
215
|
+
{ match_phrase_prefix: { diagnosis_label: { query, boost: 3 } } },
|
|
216
|
+
{
|
|
217
|
+
multi_match: {
|
|
218
|
+
query,
|
|
219
|
+
fields: ["diagnosis_label_fr", "diagnosis_label"],
|
|
220
|
+
operator: "and",
|
|
221
|
+
fuzziness: "AUTO",
|
|
222
|
+
prefix_length: 2,
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
],
|
|
226
|
+
minimum_should_match: 1,
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
// A pertinence egale, le code court (C50) passe avant ses sous-codes.
|
|
230
|
+
sort: [{ _score: "desc" }, { "diagnosis_code.keyword": "asc" }],
|
|
231
|
+
});
|
|
232
|
+
return (response?.hits?.hits ?? []).map((hit: any) => {
|
|
233
|
+
const source = hit._source ?? {};
|
|
234
|
+
const label = sentence(clean(lang === "en" ? source.diagnosis_label : source.diagnosis_label_fr) || clean(source.diagnosis_label));
|
|
235
|
+
const subject: GuidedSubject = { focus: "disease", label, code: clean(source.diagnosis_code), system: "icd10" };
|
|
236
|
+
return { ...subject, hint: `CIM-10 ${subject.code}`, key: subjectKey(subject) };
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
interface Cancer {
|
|
241
|
+
code: string;
|
|
242
|
+
fr: string;
|
|
243
|
+
en: string;
|
|
244
|
+
/** Toutes les ecritures, sans accent ni casse. */
|
|
245
|
+
haystack: string[];
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
let cancers: Promise<Cancer[]> | null = null;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Le dictionnaire des cancers de global_oncology_app (CIM-10 a 3 caracteres,
|
|
252
|
+
* libelles FR et EN). Il double le dictionnaire des hospitalisations pour le
|
|
253
|
+
* francais : la copie de prod de celui-ci n'a encore que l'anglais.
|
|
254
|
+
*/
|
|
255
|
+
function loadCancers(): Promise<Cancer[]> {
|
|
256
|
+
cancers ??= guidedSearch("drugs", ES_INDEX.ONCOLOGY_DICTIONARY, {
|
|
257
|
+
size: 500,
|
|
258
|
+
_source: ["cancer_code", "primary_label", "translations"],
|
|
259
|
+
query: {
|
|
260
|
+
bool: {
|
|
261
|
+
filter: [{ regexp: { "cancer_code.keyword": "[A-Z][0-9]{2}" } }],
|
|
262
|
+
must_not: [{ term: { is_combinaison: true } }],
|
|
263
|
+
},
|
|
264
|
+
},
|
|
265
|
+
})
|
|
266
|
+
.then((response) =>
|
|
267
|
+
(response?.hits?.hits ?? []).map((hit: any): Cancer => {
|
|
268
|
+
const source = hit._source ?? {};
|
|
269
|
+
const translations: { label: string; language: string }[] = source.translations ?? [];
|
|
270
|
+
const fr = translations.find((entry) => entry.language?.startsWith("fr"))?.label ?? source.primary_label;
|
|
271
|
+
const en = translations.find((entry) => entry.language?.startsWith("en"))?.label ?? source.primary_label;
|
|
272
|
+
return {
|
|
273
|
+
code: source.cancer_code,
|
|
274
|
+
fr: sentence(fr ?? ""),
|
|
275
|
+
en: sentence(en ?? ""),
|
|
276
|
+
haystack: [source.cancer_code, fr, en, ...translations.map((entry) => entry.label)].filter(Boolean).map(fold),
|
|
277
|
+
};
|
|
278
|
+
}),
|
|
279
|
+
)
|
|
280
|
+
.catch(() => {
|
|
281
|
+
cancers = null;
|
|
282
|
+
return [];
|
|
283
|
+
});
|
|
284
|
+
return cancers;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** Mots qui ne disent pas quel cancer : « cancer des ovaires » / « tumeur maligne de l'ovaire ». */
|
|
288
|
+
const CANCER_FILLER = new Set(["cancer", "cancers", "tumeur", "tumeurs", "maligne", "malignes", "malignant", "neoplasm", ...FILLER]);
|
|
289
|
+
|
|
290
|
+
async function cancerDiseases(query: string, lang: GuidedLang): Promise<GuidedSubjectSuggestion[]> {
|
|
291
|
+
const list = await loadCancers();
|
|
292
|
+
const words = query
|
|
293
|
+
.split(/[^a-z0-9]+/)
|
|
294
|
+
.filter((word) => word.length > 1 && !CANCER_FILLER.has(word))
|
|
295
|
+
.map((word) => word.replace(/[sx]$/, ""));
|
|
296
|
+
return list
|
|
297
|
+
.map((cancer) => {
|
|
298
|
+
const score = Math.min(...cancer.haystack.map((text) => rank(text, query)));
|
|
299
|
+
if (score < 4) return { cancer, score };
|
|
300
|
+
if (words.length && cancer.haystack.some((text) => words.every((word) => text.includes(word)))) return { cancer, score: 3 };
|
|
301
|
+
return { cancer, score: cancer.haystack.some((text) => matchesFuzzy(text, words)) ? 3.5 : 4 };
|
|
302
|
+
})
|
|
303
|
+
.filter((entry) => entry.score < 4)
|
|
304
|
+
.sort((a, b) => a.score - b.score || a.cancer.code.localeCompare(b.cancer.code))
|
|
305
|
+
.slice(0, MAX)
|
|
306
|
+
.map(({ cancer }) => {
|
|
307
|
+
const subject: GuidedSubject = { focus: "disease", label: lang === "en" ? cancer.en : cancer.fr, code: cancer.code, system: "icd10" };
|
|
308
|
+
return { ...subject, hint: `CIM-10 ${cancer.code}`, key: subjectKey(subject) };
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
async function drugs(query: string, lang: GuidedLang) {
|
|
313
|
+
const moleculeField = lang === "en" ? "atc5_english_label" : "atc5_french_label";
|
|
314
|
+
const response = await guidedSearch("drugs", ES_INDEX.BRAND_DIRECTORY, {
|
|
315
|
+
size: 0,
|
|
316
|
+
aggs: {
|
|
317
|
+
brands: {
|
|
318
|
+
filter: { match_phrase_prefix: { brand: query } },
|
|
319
|
+
aggs: {
|
|
320
|
+
names: {
|
|
321
|
+
terms: { field: "brand.keyword", size: MAX },
|
|
322
|
+
aggs: { molecule: { top_hits: { size: 1, _source: [moleculeField] } } },
|
|
323
|
+
},
|
|
324
|
+
},
|
|
325
|
+
},
|
|
326
|
+
molecules: {
|
|
327
|
+
filter: {
|
|
328
|
+
bool: {
|
|
329
|
+
should: [
|
|
330
|
+
{ match_phrase_prefix: { atc5_french_label: query } },
|
|
331
|
+
{ match_phrase_prefix: { atc5_english_label: query } },
|
|
332
|
+
{ prefix: { "atc5_code.keyword": { value: query.toUpperCase() } } },
|
|
333
|
+
],
|
|
334
|
+
minimum_should_match: 1,
|
|
335
|
+
},
|
|
336
|
+
},
|
|
337
|
+
aggs: {
|
|
338
|
+
names: {
|
|
339
|
+
terms: { field: `${moleculeField}.keyword`, size: MAX },
|
|
340
|
+
aggs: { code: { terms: { field: "atc5_code.keyword", size: 1 } } },
|
|
341
|
+
},
|
|
342
|
+
},
|
|
343
|
+
},
|
|
344
|
+
},
|
|
345
|
+
});
|
|
346
|
+
const aggs = response?.aggregations ?? {};
|
|
347
|
+
const brands: GuidedSubjectSuggestion[] = (aggs.brands?.names?.buckets ?? []).map((bucket: any) => {
|
|
348
|
+
const subject: GuidedSubject = { focus: "brand", label: String(bucket.key) };
|
|
349
|
+
const molecule = clean(bucket.molecule?.hits?.hits?.[0]?._source?.[moleculeField]);
|
|
350
|
+
return { ...subject, hint: molecule ? sentence(molecule) : "", key: subjectKey(subject) };
|
|
351
|
+
});
|
|
352
|
+
const molecules: GuidedSubjectSuggestion[] = (aggs.molecules?.names?.buckets ?? []).map((bucket: any) => {
|
|
353
|
+
const subject: GuidedSubject = { focus: "molecule", label: sentence(String(bucket.key)) };
|
|
354
|
+
return { ...subject, hint: clean(bucket.code?.buckets?.[0]?.key), key: subjectKey(subject) };
|
|
355
|
+
});
|
|
356
|
+
return { brands, molecules };
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** Les suggestions d'une saisie, groupees ; vides sous 2 caracteres. */
|
|
360
|
+
export async function suggestGuidedSubjects(search: string, lang: GuidedLang): Promise<GuidedSubjectGroups> {
|
|
361
|
+
const query = fold(search);
|
|
362
|
+
if (query.length < 2) return { diseases: [], molecules: [], brands: [] };
|
|
363
|
+
const [cancer, icd, rare, drug] = await Promise.allSettled([
|
|
364
|
+
cancerDiseases(query, lang),
|
|
365
|
+
icd10Diseases(search.trim(), lang),
|
|
366
|
+
rareDiseases(query, lang),
|
|
367
|
+
drugs(search.trim(), lang),
|
|
368
|
+
]);
|
|
369
|
+
const value = <T>(result: PromiseSettledResult<T>, fallback: T) => (result.status === "fulfilled" ? result.value : fallback);
|
|
370
|
+
// Les cancers d'abord (libelles FR surs), puis le reste de la CIM-10, puis
|
|
371
|
+
// les maladies rares ; un meme code ne sort qu'une fois.
|
|
372
|
+
const seen = new Set<string>();
|
|
373
|
+
const diseases = [...value(cancer, []), ...value(icd, []), ...value(rare, [])]
|
|
374
|
+
.filter((entry) => !seen.has(entry.key) && !!seen.add(entry.key))
|
|
375
|
+
.slice(0, MAX + 2);
|
|
376
|
+
const { brands, molecules } = value(drug, { brands: [], molecules: [] });
|
|
377
|
+
return { diseases, molecules, brands };
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** Charge la liste des maladies rares avant la premiere frappe. */
|
|
381
|
+
export function preloadGuidedSubjects() {
|
|
382
|
+
loadRare();
|
|
383
|
+
loadCancers();
|
|
384
|
+
}
|
|
@@ -447,7 +447,8 @@ export const OIP_RESOURCE_SPECS: OipResourceSpec[] = [
|
|
|
447
447
|
},
|
|
448
448
|
routeId: (id) => id,
|
|
449
449
|
base: (id) => `/appel-a-projet/${id}`,
|
|
450
|
-
// Pas de fiche a onglets dans France Augmented Science :
|
|
450
|
+
// Pas de fiche a onglets dans France Augmented Science : sa page de detail
|
|
451
|
+
// (resume, etablissement, investigateur, essai lie), d'un seul tenant.
|
|
451
452
|
tabs: [],
|
|
452
453
|
view: "science-phrc",
|
|
453
454
|
detailPath: (id) => `/appel-a-projet/${id}`,
|