Skip to content

Commit

Permalink
feat: improve syncfusion adapter content management (#28)
Browse files Browse the repository at this point in the history
* feat: improve content management and major refactorings

* fix: fix insertTemplate method

* fix: fix build errors
  • Loading branch information
dsouza95 authored Nov 20, 2023
1 parent 2918978 commit 73f508d
Show file tree
Hide file tree
Showing 14 changed files with 3,437 additions and 343 deletions.
49 changes: 49 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"devDependencies": {
"@types/jsdom": "^20.0.1",
"@types/node": "^18.11.18",
"@types/tinymce": "^4.6.8",
"@typescript-eslint/eslint-plugin": "^5.48.2",
"@typescript-eslint/parser": "^5.48.2",
"@vitest/coverage-c8": "^0.29.8",
Expand Down
100 changes: 54 additions & 46 deletions src/editor/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
import { IaraInference } from "../speech";
import { IaraSpeechRecognitionDetail } from "../speech";

export class IaraEditorInferenceFormatter {
constructor() {}

public _addTrailingSpaces(
text: string,
wordAfter: string,
wordBefore: string
) {
): string {
const addSpaceBefore =
wordBefore.length &&
!wordBefore.endsWith(" ") &&
!/^[\.,:;?!]/.test(text);
wordBefore.length && !wordBefore.endsWith(" ") && !/^[.,:;?!]/.test(text);

const addSpaceAfter =
wordAfter.length &&
!wordAfter.startsWith(" ") &&
!/^[\.,:;?!]/.test(wordAfter);
!/^[.,:;?!]/.test(wordAfter);

return `${addSpaceBefore ? " " : ""}${text}${addSpaceAfter ? " " : ""}`;
}

public _capitalize(text: string, wordBefore: string) {
const capitalize = !wordBefore.length || /[\.:;?!]$/.test(wordBefore);
public _capitalize(text: string, wordBefore: string): string {
const capitalize = !wordBefore.length || /[.:;?!]$/.test(wordBefore);
return capitalize
? `${text.charAt(0).toLocaleUpperCase()}${text.slice(1)}`
: text;
}

protected _estimateVolume(text: string, regex: string) {
protected _estimateVolume(text: string, regex: string): string {
let converted = false;
const iterator = text.matchAll(RegExp(regex, 'giu'));
const iterator = text.matchAll(RegExp(regex, "giu"));
const matches = [...iterator];
matches.forEach((match) => {

matches.forEach(match => {
// Check if all desired groups were captured
if (match && match.length === 7) {
// Volume estimation given 3 elipsoid radius
Expand All @@ -42,30 +38,32 @@ export class IaraEditorInferenceFormatter {
let volume =
(4 / 3) *
Math.PI *
parseFloat(match[1].replace(',', '.')) *
parseFloat(match[3].replace(',', '.')) *
parseFloat(match[5].replace(',', '.'));
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') {

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('.', ',');
}`.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' ? '³': ''})`,
`${match[0].replace("por", "x")} (volume estimado em ${estimation} ${
converted ? "cm" : match[6]
}${match[6] === "cm" || match[6] === "mm" ? "³" : ""})`
);
}
});
Expand All @@ -74,43 +72,53 @@ export class IaraEditorInferenceFormatter {

protected _parseMeasurements(text: string): string {
const numberMap = [
{ um: '1' },
{ dois: '2' },
{ três: '3' },
{ quatro: '4' },
{ cinco: '5' },
{ seis: '6' },
{ sete: '7' },
{ oito: '8' },
{ nove: '9' }
]
{ um: "1" },
{ dois: "2" },
{ três: "3" },
{ quatro: "4" },
{ cinco: "5" },
{ seis: "6" },
{ sete: "7" },
{ oito: "8" },
{ nove: "9" },
];
//convert the number by extensive number before the 'por' into numerals and change 'por' to 'x'
text = numberMap.reduce((a, c) => {
const [[oldText, newText]] = Object.entries(c)
return a.replace(new RegExp(`${oldText} (por|x)`, 'gui'), `${newText} x`)
}, text)
const [[oldText, newText]] = Object.entries(c);
return a.replace(new RegExp(`${oldText} (por|x)`, "gui"), `${newText} x`);
}, text);
//convert the number by extensive after the 'por' into numerals and change 'por' to 'x'
text = numberMap.reduce((a, c) => {
const [[oldText, newText]] = Object.entries(c)
return a.replace(new RegExp(`(por|x) ${oldText}`, 'gui'), `x ${newText}`)
}, text)
const [[oldText, newText]] = Object.entries(c);
return a.replace(new RegExp(`(por|x) ${oldText}`, "gui"), `x ${newText}`);
}, text);

//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')
return text
text = text.replace(/(\d+(?:,\d+)?) (por|x) (?=\d+(?:,\d+)?)/giu, "$1x");

return text;
}

format(inference: IaraInference, _wordBefore: string, _wordAfter: string) {
format(
inference: IaraSpeechRecognitionDetail,
_wordBefore: string,
_wordAfter: string
): string {
let text = inference.richTranscript
.replace(/^<div>/, "")
.replace(/<\/div>$/, "");

text = this._parseMeasurements(text);

// 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\\(|³)');
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\\(|³)"
);

text = this._addTrailingSpaces(text, _wordAfter, _wordBefore);
text = this._capitalize(text, _wordBefore);
Expand Down
Loading

0 comments on commit 73f508d

Please sign in to comment.