diff options
Diffstat (limited to 'src/client/views/nodes/ImageBox.tsx')
-rw-r--r-- | src/client/views/nodes/ImageBox.tsx | 93 |
1 files changed, 54 insertions, 39 deletions
diff --git a/src/client/views/nodes/ImageBox.tsx b/src/client/views/nodes/ImageBox.tsx index 86da64e5e..1b1431373 100644 --- a/src/client/views/nodes/ImageBox.tsx +++ b/src/client/views/nodes/ImageBox.tsx @@ -385,7 +385,11 @@ export class ImageBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { console.log('RESPONSE:'); console.log(response.data['boxes']); console.log(response.data['text']); - this.createBoxes(response.data['boxes'], response.data['text']); + if (response.data['boxes'].length != 0) { + this.createBoxes(response.data['boxes'], response.data['text']); + } else { + this._loading = false; + } }; createBoxes = (boxes: [[[number, number]]], texts: [string]) => { @@ -397,12 +401,13 @@ export class ImageBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { const height = coords[2][1] - coords[0][1]; const text = texts[i]; - const newCol = Docs.Create.TextDocument('', { + const newCol = Docs.Create.LabelDocument({ _width: width, //width * NumCast(this.dataDoc[this.fieldKey + '_nativeWidth']), _height: height, //height * NumCast(this.dataDoc[this.fieldKey + '_nativeHeight']), _layout_fitWidth: true, + title: '', // _layout_autoHeight: true, }); const scaling = 1 / (this._props.NativeDimScaling?.() || 1); @@ -414,6 +419,7 @@ export class ImageBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { newCol.forceActive = true; newCol.quiz = text; newCol.showQuiz = false; + newCol[DocData].textTransform = 'none'; this._quizBoxes.push(newCol); this.addDocument(newCol); this._loading = false; @@ -434,7 +440,6 @@ export class ImageBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { }; makeLabels = async () => { - this._loading = true; try { const hrefBase64 = await this.createCanvas(); this.pushInfo(quizMode.NORMAL, hrefBase64); @@ -443,43 +448,51 @@ export class ImageBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { } }; - levenshteinDistance = (a: string, b: string) => { - const an = a.length; - const bn = b.length; - const matrix = []; + levenshteinDistance = (str1: string, str2: string) => { + const len1 = str1.length; + const len2 = str2.length; + const dp = Array.from(Array(len1 + 1), () => Array(len2 + 1).fill(0)); + + if (len1 === 0) return len2; + if (len2 === 0) return len1; + + for (let i = 0; i <= len1; i++) dp[i][0] = i; + for (let j = 0; j <= len2; j++) dp[0][j] = j; + + for (let i = 1; i <= len1; i++) { + for (let j = 1; j <= len2; j++) { + const cost = str1[i - 1] === str2[j - 1] ? 0 : 1; + dp[i][j] = Math.min( + dp[i - 1][j] + 1, // deletion + dp[i][j - 1] + 1, // insertion + dp[i - 1][j - 1] + cost // substitution + ); + } + } - // Ensure non-zero length strings - if (an === 0) return bn; - if (bn === 0) return an; + return dp[len1][len2]; + }; - // Initialize the matrix - for (let i = 0; i <= an; i++) { - matrix[i] = [i]; - } - for (let j = 0; j <= bn; j++) { - matrix[0][j] = j; - } + jaccardSimilarity = (str1: string, str2: string) => { + const set1 = new Set(str1.split(' ')); + const set2 = new Set(str2.split(' ')); - // Populate the matrix - for (let i = 1; i <= an; i++) { - for (let j = 1; j <= bn; j++) { - if (a[i - 1] === b[j - 1]) { - matrix[i][j] = matrix[i - 1][j - 1]; - } else { - matrix[i][j] = Math.min( - matrix[i - 1][j - 1] + 1, // substitution - Math.min( - matrix[i][j - 1] + 1, // insertion - matrix[i - 1][j] + 1 // deletion - ) - ); - } - } - } + const intersection = new Set([...set1].filter(x => set2.has(x))); + const union = new Set([...set1, ...set2]); - return matrix[an][bn]; + return intersection.size / union.size; }; + stringSimilarity(str1: string, str2: string) { + const levenshteinDist = this.levenshteinDistance(str1, str2); + const levenshteinScore = 1 - levenshteinDist / Math.max(str1.length, str2.length); + + const jaccardScore = this.jaccardSimilarity(str1, str2); + + // Combine the scores with a higher weight on Jaccard similarity + return 0.5 * levenshteinScore + 0.5 * jaccardScore; + } + @computed get checkIcon() { return ( <Tooltip title={<div className="dash-tooltip">Check</div>}> @@ -501,9 +514,9 @@ export class ImageBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { } compareWords = (input: string, target: string) => { - const distance = this.levenshteinDistance(input.toLowerCase(), target.toLowerCase()); - const threshold = Math.max(input.length, target.length) * 0.2; // Allow up to 20% of the length as difference - return distance <= threshold; + const distance = this.stringSimilarity(input.toLowerCase(), target.toLowerCase()); + // const threshold = Math.max(input.length, target.length) * 0.2; // Allow up to 20% of the length as difference + return distance >= 0.7; }; extractHexAndSentences = (inputString: string) => { @@ -523,7 +536,7 @@ export class ImageBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { check = () => { this._loading = true; this._quizBoxes.forEach(async doc => { - const input = StrCast(RTFCast(DocCast(doc).text)?.Text); + const input = StrCast(doc[DocData].title); console.log('INP: ' + StrCast(input) + '; DOC: ' + StrCast(doc.quiz)); if (this._quizMode == quizMode.SMART && input) { const questionText = 'Question: What was labeled in this image?'; @@ -553,7 +566,7 @@ export class ImageBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { redo = () => { this._quizBoxes.forEach(doc => { - DocCast(doc)[DocData].text = ''; + doc[DocData].title = ''; doc.backgroundColor = '#e4e4e4'; doc.showQuiz = false; }); @@ -564,8 +577,10 @@ export class ImageBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { this._quizBoxes.forEach(doc => { // this._props.removeDocument?.(DocCast(doc)); // this._props.DocumentView?.()._props.removeDocument?.(doc); + this.removeDocument?.(doc); }); this._quizBoxes = []; + console.log('remove'); }; @action |