diff --git a/src/mongodb/scores.controller.ts b/src/mongodb/scores.controller.ts index 8e5cb2b..e297812 100644 --- a/src/mongodb/scores.controller.ts +++ b/src/mongodb/scores.controller.ts @@ -183,7 +183,7 @@ export class ScoresController { missing_token_scoresArr = identifyTokens.missing_token_scoresArr; anomaly_scoreArr = identifyTokens.anomaly_scoreArr; - const textEvalMatrices = await this.scoresService.getTextMetrics(originalText, constructText, language, audioFile) + const textEvalMatrices = await this.scoresService.getTextMetrics(originalText, language, constructText) if (process.env.denoiserEnabled === "true") { let improved = false; @@ -1197,10 +1197,9 @@ export class ScoresController { const url = process.env.ALL_TEXT_EVAL_API + "/getTextMatrices"; const textData = { - reference: CreateLearnerProfileDto.original_text, - hypothesis: CreateLearnerProfileDto.output[0].source, - language: 'kn', - base64_string: audioFile.toString('base64'), + "reference": CreateLearnerProfileDto.original_text, + "construct_text": constructText, + language: 'kn' }; const textEvalMatrices = await lastValueFrom( @@ -1500,7 +1499,13 @@ export class ScoresController { responseText = await this.scoresService.processText(CreateLearnerProfileDto.output[0].source); - const textEvalMatrices = await this.scoresService.getTextMetrics(originalText, responseText, language, audioFile) + // Constructed Logic starts from here + let constructedTextRepCountData = await this.scoresService.getConstructedText(originalText, responseText); + let constructText = constructedTextRepCountData.constructText; + let repetitions = constructedTextRepCountData.reptitionCount; + // End Constructed Text Logic + + const textEvalMatrices = await this.scoresService.getTextMetrics(originalText, language, constructText) for (const confidence_char of textEvalMatrices.confidence_char_list) { const hexcode = await this.scoresService.getTokenHexcode(tokenHexcodeDataArr, confidence_char); @@ -1569,11 +1574,6 @@ export class ScoresController { await this.scoresService.addDenoisedOutputLog(createDenoiserOutputLog); } - // Constructed Logic starts from here - let constructedTextRepCountData = await this.scoresService.getConstructedText(originalText, responseText); - let repetitions = constructedTextRepCountData.reptitionCount; - // End Constructed Text Logic - let fluencyScore = await this.scoresService.getCalculatedFluency(textEvalMatrices, repetitions, originalText, responseText, pause_count); let createdAt = new Date().toISOString().replace('Z', '+00:00') @@ -2233,9 +2233,8 @@ export class ScoresController { const textData = { "reference": CreateLearnerProfileDto.original_text, - "hypothesis": CreateLearnerProfileDto.output[0].source, + "construct_text": constructText, "language": "te", - "base64_string": audioFile.toString('base64') }; const textEvalMatrices = await lastValueFrom( diff --git a/src/mongodb/scores.service.ts b/src/mongodb/scores.service.ts index a0ac18b..ce8fa85 100644 --- a/src/mongodb/scores.service.ts +++ b/src/mongodb/scores.service.ts @@ -2188,23 +2188,67 @@ export class ScoresService { async getConstructedText(original_text: string, response_text: string) { let constructText = ''; const compareCharArr = []; - const constructTextSet = new Set(); + const constructTextSet = []; let reptitionCount = 0; - for (const originalEle of original_text.split( - ' ', - )) { + const original_text_arr = original_text.split(' '); + const response_text_arr = response_text.split(' '); + + for (const [originalEleindex, originalEle] of original_text_arr.entries()) { + let originalSplitText = ''; + if (originalEleindex < original_text_arr.length - 1 && originalEle.length > 2) { + originalSplitText = originalEle + original_text_arr[originalEleindex + 1]; + } let originalRepCount = 0; - for (const sourceEle of response_text.split(' ')) { + + let similiaritythreshold = 0.85; + + for (const [sourceEleindex, sourceEle] of response_text_arr.entries()) { + let responseSplitText = ''; + if (sourceEleindex < response_text_arr.length - 1 && sourceEle.length > 2) { + responseSplitText = sourceEle + response_text_arr[sourceEleindex + 1]; + } const similarityScore = await this.getTextSimilarity(originalEle, sourceEle); - if (similarityScore >= 0.4) { + if (similarityScore >= similiaritythreshold) { compareCharArr.push({ original_text: originalEle, response_text: sourceEle, score: similarityScore, }); + break; } - if (similarityScore >= 0.6) { + + const originalSimilarityScoreSplit = await this.getTextSimilarity(originalSplitText, sourceEle); + + if (originalSimilarityScoreSplit >= similiaritythreshold) { + compareCharArr.push({ + original_text: originalEle, + response_text: originalEle, + score: originalSimilarityScoreSplit, + }); + + compareCharArr.push({ + original_text: originalSplitText, + response_text: original_text_arr[originalEleindex + 1], + score: originalSimilarityScoreSplit, + }); + + break; + + } + + const responseSimilarityScoreSplit = await this.getTextSimilarity(originalEle, responseSplitText); + + if (responseSimilarityScoreSplit >= similiaritythreshold) { + compareCharArr.push({ + original_text: originalEle, + response_text: responseSplitText, + score: responseSimilarityScoreSplit, + }); + break; + } + + if (similarityScore >= similiaritythreshold) { originalRepCount++; } } @@ -2217,17 +2261,14 @@ export class ScoresService { let score = 0; let word = ''; for (const compareCharArrCmpEle of compareCharArr) { - if ( - compareCharArrEle.original_text === - compareCharArrCmpEle.original_text - ) { + if (compareCharArrEle.original_text === compareCharArrCmpEle.original_text) { if (compareCharArrCmpEle.score > score) { score = compareCharArrCmpEle.score; word = compareCharArrCmpEle.response_text; } } } - constructTextSet.add(word); + constructTextSet.push(word); } for (const constructTextSetEle of constructTextSet) { @@ -2239,14 +2280,13 @@ export class ScoresService { return { constructText, reptitionCount } } - async getTextMetrics(original_text: string, response_text: string, language: string, base64_string) { + async getTextMetrics(original_text: string, language: string, construct_text: string) { const url = process.env.ALL_TEXT_EVAL_API + "/getTextMatrices"; const textData = { reference: original_text, - hypothesis: response_text, language: language, - base64_string: base64_string.toString('base64'), + construct_text: construct_text }; const textEvalMatrices = await lastValueFrom(