diff --git a/src/editor/index.ts b/src/editor/index.ts index f257063..6835a62 100644 --- a/src/editor/index.ts +++ b/src/editor/index.ts @@ -40,4 +40,47 @@ export abstract class EditorAdapter { ): Promise { return this._recognition.report.change(plainContent, richContent); } + _estimateVolume(text: string, regex: string) { + let converted = false; + const iterator = text.matchAll(RegExp(regex, 'giu')); + const matches = [...iterator]; + + matches.forEach((match) => { + // Check if all desired groups were captured + if (match && match.length === 7) { + // Volume estimation given 3 elipsoid radius + // original formula is: 4/3 * π * a * b * c + // where a, b and c are elipsoid radius + let volume = + (4 / 3) * + Math.PI * + parseFloat(match[1].replace(',', '.')) * + parseFloat(match[3].replace(',', '.')) * + parseFloat(match[5].replace(',', '.')); + + // If user dictated measures as diameter + // then each one must be converted to radius: + // (4/3 * π * (a/2) * (b/2) * (c/2)) + + // Volume estimation given 3 elipsoid radius + volume /= 8; + + if (volume >= 1000 && match[6] == 'mm') { + // convert the volume from mm³ to cm³ + volume /= 1000; + converted = true; + } + // Round volume to 2 decimal places + const estimation = `${ + Math.round(volume * Math.pow(10, 2)) / Math.pow(10, 2) + }`.replace('.', ','); + + text = text.replace( + match[0], + `${match[0].replace('por', 'x')} (volume estimado em ${estimation} ${converted ? 'cm' : match[6]}${match[6] === 'cm'||match[6] === 'mm' ? '³': ''})`, + ); + } + }); + return text; + } } diff --git a/src/syncfusion/formatter.ts b/src/syncfusion/formatter.ts index bdb355d..eea325c 100644 --- a/src/syncfusion/formatter.ts +++ b/src/syncfusion/formatter.ts @@ -34,50 +34,6 @@ export class IaraSyncfusionInferenceFormatter { : text; } - private _estimateVolume(text: string, regex: string) { - let converted = false; - const iterator = text.matchAll(RegExp(regex, 'giu')); - const matches = [...iterator]; - - matches.forEach((match) => { - // Check if all desired groups were captured - if (match && match.length === 7) { - // Volume estimation given 3 elipsoid radius - // original formula is: 4/3 * π * a * b * c - // where a, b and c are elipsoid radius - let volume = - (4 / 3) * - Math.PI * - parseFloat(match[1].replace(',', '.')) * - parseFloat(match[3].replace(',', '.')) * - parseFloat(match[5].replace(',', '.')); - - // If user dictated measures as diameter - // then each one must be converted to radius: - // (4/3 * π * (a/2) * (b/2) * (c/2)) - - // Volume estimation given 3 elipsoid radius - volume /= 8; - - if (volume >= 1000 && match[6] == 'mm') { - // convert the volume from mm³ to cm³ - volume /= 1000; - converted = true; - } - // Round volume to 2 decimal places - const estimation = `${ - Math.round(volume * Math.pow(10, 2)) / Math.pow(10, 2) - }`.replace('.', ','); - - text = text.replace( - match[0], - `${match[0].replace('por', 'x')} (volume estimado em ${estimation} ${converted ? 'cm' : match[6]}${match[6] === 'cm'||match[6] === 'mm' ? '³': ''})`, - ); - } - }); - return text; - } - public _parseMeasurements(text: string): string { const numberMap = [ { um: '1' }, @@ -104,10 +60,6 @@ export class IaraSyncfusionInferenceFormatter { //convert the 'por' before or after a number and return the formatted expression without a space ex:1x1 text = text.replace(/(\d+(?:,\d+)?) (por|x) (?=\d+(?:,\d+)?)/gui, '$1x') - //expression to estimate volume - text = this._estimateVolume(text, '(\\d+(?:,\\d+)?)(\\spor\\s|x)(\\d+(?:,\\d+)?)(\\spor\\s|x)(\\d+(?:,\\d+)?) (cm³|mm³)(?!\\s\\()') - text = this._estimateVolume(text, '(\\d+(?:,\\d+)?)(\\spor\\s|x)(\\d+(?:,\\d+)?)(\\spor\\s|x)(\\d+(?:,\\d+)?) (cm|mm)(?!\\s\\(|³)') - return text } diff --git a/src/syncfusion/index.ts b/src/syncfusion/index.ts index 81586ba..2407f28 100644 --- a/src/syncfusion/index.ts +++ b/src/syncfusion/index.ts @@ -130,7 +130,12 @@ export class IaraSyncfusionAdapter } textFormatter(text: IaraInference): string { - const formatted = this._inferenceFormatter.format(text); + let formatted = this._inferenceFormatter.format(text); + + //expression to estimate volume + formatted = this._estimateVolume(formatted, '(\\d+(?:,\\d+)?)(\\spor\\s|x)(\\d+(?:,\\d+)?)(\\spor\\s|x)(\\d+(?:,\\d+)?) (cm³|mm³)(?!\\s\\()') + formatted = this._estimateVolume(formatted, '(\\d+(?:,\\d+)?)(\\spor\\s|x)(\\d+(?:,\\d+)?)(\\spor\\s|x)(\\d+(?:,\\d+)?) (cm|mm)(?!\\s\\(|³)') + return formatted; } }