From 9305a5398fb5bb87c239ee6e371950d5451f58f2 Mon Sep 17 00:00:00 2001 From: Amit Markel Date: Tue, 19 Dec 2023 18:59:56 +0200 Subject: [PATCH] Bail-out earlier on the score summation loop to improve performance. --- javascript/src/parser.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/javascript/src/parser.ts b/javascript/src/parser.ts index 6343ece4..ce2ae3bc 100644 --- a/javascript/src/parser.ts +++ b/javascript/src/parser.ts @@ -72,12 +72,15 @@ export class Parser { score += this.model.get('UW1')?.get(sentence.substring(i - 3, i - 2)) || 0; score += this.model.get('UW2')?.get(sentence.substring(i - 2, i - 1)) || 0; score += this.model.get('UW3')?.get(sentence.substring(i - 1, i)) || 0; + if (score > 0) { result.push(i); continue; } score += this.model.get('UW4')?.get(sentence.substring(i, i + 1)) || 0; score += this.model.get('UW5')?.get(sentence.substring(i + 1, i + 2)) || 0; score += this.model.get('UW6')?.get(sentence.substring(i + 2, i + 3)) || 0; + if (score > 0) { result.push(i); continue; } score += this.model.get('BW1')?.get(sentence.substring(i - 2, i)) || 0; score += this.model.get('BW2')?.get(sentence.substring(i - 1, i + 1)) || 0; score += this.model.get('BW3')?.get(sentence.substring(i, i + 2)) || 0; + if (score > 0) { result.push(i); continue; } score += this.model.get('TW1')?.get(sentence.substring(i - 3, i)) || 0; score += this.model.get('TW2')?.get(sentence.substring(i - 2, i + 1)) || 0; score += this.model.get('TW3')?.get(sentence.substring(i - 1, i + 2)) || 0;