Skip to content

Commit

Permalink
analysis score averaging
Browse files Browse the repository at this point in the history
  • Loading branch information
Woozl committed Jul 31, 2023
1 parent 3b6ad81 commit 3004960
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/pages/answer/useAnswerStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ export default function useAnswerStore() {
setResultJSON({});
}

/**
* Takes a TRAPI message and average the analysis scores for each
* result, and places that average in a new key called `score` on
* each result object.
* @param {object} msg - TRAPI message
*/
function averageAnalysesScores(msg) {
const resultsWithScore = msg.results.map((result) => {
// get average score of all analyses
const score = result.analyses.reduce((sum, analysis) => sum + analysis.score, 0) / result.analyses.length;
return {
...result,
score,
};
});

return {
...msg,
results: resultsWithScore,
};
}

/**
* Initialize the answer store with a message
*
Expand All @@ -37,7 +59,7 @@ export default function useAnswerStore() {
* @param {object} msg - TRAPI message
*/
function initialize(msg, updateDisplayState) {
setMessage(msg);
setMessage(averageAnalysesScores(msg));
if (msg.knowledge_graph && msg.results) {
setKgNodes(kgUtils.makeDisplayNodes(msg, hierarchies));
updateDisplayState({ type: 'toggle', payload: { component: 'kg', show: true } });
Expand Down

0 comments on commit 3004960

Please sign in to comment.