Skip to content

Commit

Permalink
fix pr conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
André Canuto authored and André Canuto committed Oct 16, 2023
1 parent f8cfdde commit 75ade53
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 49 deletions.
43 changes: 43 additions & 0 deletions src/editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,47 @@ export abstract class EditorAdapter {
): Promise<void> {
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;
}
}
48 changes: 0 additions & 48 deletions src/syncfusion/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand All @@ -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
}

Expand Down
7 changes: 6 additions & 1 deletion src/syncfusion/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 75ade53

Please sign in to comment.