🎉 first commit

This commit is contained in:
2023-09-10 17:31:20 +02:00
parent e9a19071c2
commit a6bc695506
28 changed files with 1336 additions and 97 deletions

View File

@@ -1,6 +1,7 @@
import {Injectable, OnInit} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {Treatment} from "../models";
import {TreatmentsService} from "./treatments.service";
interface Outcomes {
firstIntent: string[];
@@ -25,9 +26,8 @@ const DISCOURAGED_SCORE = -1;
export class EngineService {
OUTCOMES_MAP: Record<string, Outcomes> = {};
TREATMENTS_MAP: Record<string, Treatment> = {};
constructor(private http: HttpClient) {
constructor(private http: HttpClient, private treatmentsService: TreatmentsService) {
}
loadOutcomes(): void {
@@ -37,30 +37,15 @@ export class EngineService {
for (let index = 1; index < csvToRowArray.length - 1; index++) {
let row = csvToRowArray[index].split("\t");
this.OUTCOMES_MAP[row[0]] = {
firstIntent: row[1].replace('\r', '').split(''),
secondIntent: row[2].replace('\r', '').split(''),
discouraged: row[3].replace('\r', '').split('')
firstIntent: row[1].replace('\r', '').replace('_', '').split(''),
secondIntent: row[2].replace('\r', '').replace('_', '').split(''),
discouraged: row[3].replace('\r', '').replace('_', '').split('')
};
}
console.log('Outcomes loaded', this.OUTCOMES_MAP);
});
};
loadTreatments(): void {
this.http.get('assets/engine/treatments.tsv', {responseType: 'text'})
.subscribe((data) => {
let csvToRowArray = data.split("\n");
for (let index = 1; index < csvToRowArray.length - 1; index++) {
let row = csvToRowArray[index].split("\t");
this.TREATMENTS_MAP[row[0]] = {
weight: parseFloat(row[1]),
label: row[2]
};
}
console.log('Treatments loaded', this.TREATMENTS_MAP);
});
}
fromAnswerIndexesToOutcomes = (answerIndexes: string[]): Outcomes => {
const globalOutcomes: Outcomes = {
@@ -87,12 +72,8 @@ export class EngineService {
fromOutcomeToRecommendations = (globalOutcomes: Outcomes): Recommendations => {
const recommendations: Recommendations = {};
console.log(globalOutcomes.firstIntent);
console.log(globalOutcomes.secondIntent);
console.log(globalOutcomes.discouraged);
globalOutcomes.firstIntent.forEach((treatmentCode: string) => {
const treatment = this.fromCodeToTreatment(treatmentCode);
const treatment = this.treatmentsService.getTreatmentDetails(treatmentCode).treatment;
if(!recommendations[treatmentCode]){
recommendations[treatmentCode] = 0;
@@ -101,7 +82,7 @@ export class EngineService {
});
globalOutcomes.secondIntent.forEach((treatmentCode: string) => {
const treatment = this.fromCodeToTreatment(treatmentCode);
const treatment = this.treatmentsService.getTreatmentDetails(treatmentCode).treatment;
if(!recommendations[treatmentCode]){
recommendations[treatmentCode] = 0;
@@ -127,19 +108,11 @@ export class EngineService {
for (let k in recommendations){
recommendationArray.push({
treatment: this.fromCodeToTreatment(k as string),
treatment: this.treatmentsService.getTreatmentDetails(k as string).treatment,
score: recommendations[k]
})
}
return recommendationArray;
}
fromCodeToTreatment = (treatmentCode: string): Treatment => {
const treatment: Treatment = this.TREATMENTS_MAP[treatmentCode];
if(!treatment){
throw new Error(`Could not find treatment for code: ${treatmentCode}`);
}
return treatment;
}
}

View File

@@ -0,0 +1,138 @@
import {Injectable} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {take} from "rxjs";
import {Section, SectionType, Treatment, TreatmentDetails} from "../models";
const isEmptyLine = (row: string): boolean => {
const trimmed = row.trim();
return trimmed === "" || trimmed === "\n";
}
const isSectionLine = (row: string): boolean => {
return row.startsWith('#');
}
const getSectionType = (firstSectionRow: string): SectionType => {
if(firstSectionRow.startsWith('###')){
return "free";
}else if (firstSectionRow.startsWith('##')){
return "formatted";
}else if (firstSectionRow.startsWith('#')){
return "info";
}else{
throw new Error(`First row of section has no recognizable pattern: ${firstSectionRow}`);
}
}
const parseTreatmentInfo = (row: string): Treatment => {
const split = row.replace('#', '').split(',');
if(split.length !== 3){
throw new Error(`Invalid treatment line: ${row}`);
}
return {
code: split[0].trim(),
weight: parseFloat(split[1].trim()),
label: split[2].trim()
}
}
const parseSectionTitle = (row: string, sectionType: SectionType): string => {
switch(sectionType){
case "info":
throw new Error(`Should not try to parse section title for info line: ${row}`);
case "formatted":
return row.replace('##', '').trim();
case "free":
return row.replace('###', '').trim();
}
}
@Injectable({
providedIn: 'root'
})
export class TreatmentsService {
private TREATMENT_DETAILS: TreatmentDetails[] = [];
constructor(private http: HttpClient) {}
public loadTreatments = () => {
this.http.get('assets/engine/treatments.txt', {responseType: 'text'})
.pipe(take(1))
.subscribe((data) => {
let rows = data.split("\n");
let currentSectionType: SectionType | null = null;
let currentSection: Section | null = null;
let currentTreatmentDetails: TreatmentDetails | null = null;
rows.forEach((row: string) => {
// ignore empty lines
if(isEmptyLine(row)){
return;
}
if(isSectionLine(row)){
// new section starts
currentSectionType = getSectionType(row);
if(currentSectionType === "info"){
// new treatment starts
// push previous treatment if exists
if(currentTreatmentDetails){
this.TREATMENT_DETAILS.push(currentTreatmentDetails);
}
// init next treatment
currentTreatmentDetails = {
treatment: parseTreatmentInfo(row),
sections: []
};
}else{
// new free or formatted section starts
// push previous section if exists
if(currentSection){
if(currentTreatmentDetails){
currentTreatmentDetails.sections.push(currentSection)
}else{
throw new Error(`Tried to push section in a non existing treatment: ${currentSection}`);
}
}
currentSection = {
type: currentSectionType,
title: parseSectionTitle(row, currentSectionType),
rows: []
}
}
return;
}
// new line of current section
if(currentSection){
currentSection.rows.push(row.trim());
}else{
throw new Error(`Tried to push row without a section: ${row}`);
}
});
console.log(this.TREATMENT_DETAILS);
});
}
public getTreatmentDetails = (code: string): TreatmentDetails => {
const treatment: TreatmentDetails | undefined = this.TREATMENT_DETAILS.find((t: TreatmentDetails) => t.treatment.code === code);
if(!treatment){
throw new Error(`Could not find treatment of code: ${code}`);
}
return treatment;
}
}