✨ first working version (bugs with missing codes)

This commit is contained in:
2023-05-20 16:36:24 +02:00
parent 631d1b2514
commit e9a19071c2
34 changed files with 985 additions and 524 deletions

View File

@@ -0,0 +1,26 @@
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class AnswersService {
answersIndexes: string[] = [];
constructor() { }
addAnswer = (answerIndex: string) => {
this.answersIndexes.push(answerIndex);
console.log(this.answersIndexes);
}
removeAnswer = (answerIndex: string) => {
this.answersIndexes.splice(this.answersIndexes.indexOf(answerIndex), 1);
console.log(this.answersIndexes);
}
resetAnswers = () => {
this.answersIndexes = [];
}
}

View File

@@ -0,0 +1,145 @@
import {Injectable, OnInit} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {Treatment} from "../models";
interface Outcomes {
firstIntent: string[];
secondIntent: string[];
discouraged: string[];
}
type Recommendations = Record<string, number>;
export interface RecommendationModel {
treatment: Treatment;
score: number;
}
const FIRST_INTENT_SCORE = 0.5;
const SECOND_INTENT_SCORE = 0.15;
const DISCOURAGED_SCORE = -1;
@Injectable({
providedIn: 'root'
})
export class EngineService {
OUTCOMES_MAP: Record<string, Outcomes> = {};
TREATMENTS_MAP: Record<string, Treatment> = {};
constructor(private http: HttpClient) {
}
loadOutcomes(): void {
this.http.get('assets/engine/outcomes.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.OUTCOMES_MAP[row[0]] = {
firstIntent: row[1].replace('\r', '').split(''),
secondIntent: row[2].replace('\r', '').split(''),
discouraged: row[3].replace('\r', '').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 = {
firstIntent: [],
secondIntent: [],
discouraged: []
};
answerIndexes.forEach((answerIndex: string) => {
const answerOutcomes = this.OUTCOMES_MAP[answerIndex];
if(!answerOutcomes){
throw new Error(`Could not find answer index: ${answerIndex}`);
}
globalOutcomes.firstIntent = globalOutcomes.firstIntent.concat(answerOutcomes.firstIntent);
globalOutcomes.secondIntent = globalOutcomes.secondIntent.concat(answerOutcomes.secondIntent);
globalOutcomes.discouraged = globalOutcomes.discouraged.concat(answerOutcomes.discouraged);
});
return globalOutcomes;
}
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);
if(!recommendations[treatmentCode]){
recommendations[treatmentCode] = 0;
}
recommendations[treatmentCode] += FIRST_INTENT_SCORE * treatment.weight;
});
globalOutcomes.secondIntent.forEach((treatmentCode: string) => {
const treatment = this.fromCodeToTreatment(treatmentCode);
if(!recommendations[treatmentCode]){
recommendations[treatmentCode] = 0;
}
recommendations[treatmentCode] += SECOND_INTENT_SCORE * treatment.weight;
});
globalOutcomes.discouraged.forEach((treatmentCode: string) => {
if(!recommendations[treatmentCode]){
recommendations[treatmentCode] = 0;
}
recommendations[treatmentCode] = DISCOURAGED_SCORE;
});
return recommendations;
}
public getRecommendations = (answerIndexes: string[]): RecommendationModel[] => {
const globalOutcomes = this.fromAnswerIndexesToOutcomes(answerIndexes);
const recommendations = this.fromOutcomeToRecommendations(globalOutcomes);
const recommendationArray: RecommendationModel[] = [];
for (let k in recommendations){
recommendationArray.push({
treatment: this.fromCodeToTreatment(k as string),
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;
}
}