Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix pdf text box mapping #328

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,11 @@ export class MappingSourceTextProvider {
isBlank(text: string): boolean {
return this.normalizer.isBlank(text);
}

/**
* Cleanup the consumed text position history
*/
resetHistory() {
this.provider.resetHistory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,11 @@ export class TextProvider {
});
this.history = validSpans.slice(0, MAX_HISTORY);
}

/**
* Cleanup the consumed text position history
*/
resetHistory() {
this.history = [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,35 @@ describe('getTextBoxMapping', () => {
expect(normalized2?.cell).toBe(target.cellAt(1)); // mapped to 2nd cell in target
expect(normalized2?.span).toEqual([0, 3]); // mapped to the span [0, 3] in the 2nd cell
});

it('should map text after long match properly', () => {
const source = new SimpleTextLayout([
// text_mappings box
{
text: '1 abc def ghi jkl 2 abc def ghi jkl 3 abc def ghi jkl 4 abc def ghi jkl',
bbox: bbox(0, 0, 2, 4)
}
]);
const target = new SimpleTextLayout([
// 1st line
{ bbox: bbox(0, 0), text: '1 abc def ghi' },
{ bbox: bbox(1, 0), text: 'jkl' },
// 2nd line
{ bbox: bbox(0, 1), text: '2 abc def ghi' },
{ bbox: bbox(1, 1), text: 'jkl' },
// 3nd line
{ bbox: bbox(0, 2), text: '3 abc def ghi' },
{ bbox: bbox(1, 2), text: 'jkl' },
// 4th line
{ bbox: bbox(0, 3), text: '4 abc def ghi' },
{ bbox: bbox(1, 3), text: 'jkl' }
]);
const mapping = getTextBoxMappings(source, target);

// verify the mapping result of the 1st 'jkl' in the source
const result1 = mapping.apply(source.cellAt(0).getPartial([14, 17]));
expect(result1).toHaveLength(1);
const normalized1 = result1[0].cell?.getNormalized();
expect(normalized1?.cell).toBe(target.cellAt(1)); // mapped to first cell in target
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export function getTextBoxMappings<
matchInSource.markAsMapped();
}
});
source.resetHistory();
}
return builder.toTextBoxMapping();
}
Expand Down Expand Up @@ -178,6 +179,13 @@ class Source<SourceCell extends TextLayoutCell, TargetCell extends TextLayoutCel
};
}

/**
* Cleanup history in sources to process text from the start
*/
resetHistory() {
this.sourceProviders.forEach(provider => provider.resetHistory());
}

/**
* Find the best source (larger text layout cell) where text `textToMatch` is in
* @param sources source (larger) text layout cells overlapping the current target cell
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { bboxesIntersect } from 'components/DocumentPreview/utils/box';
import { PDFPageViewport, PDFPageViewportOptions, TextContentItem } from 'pdfjs-dist';
import { sortBy } from 'lodash';
import { Bbox, TextSpan } from '../../types';
import { BaseTextLayoutCell } from './BaseTextLayout';
import { getAdjustedCellByOffsetByDom } from './dom';
Expand All @@ -18,16 +19,29 @@ export class PdfTextContentTextLayout implements TextLayout<PdfTextContentTextLa

const textContentItems = textContentInfo.textContent.items;

this.cells = textContentItems.map((item, index) => {
const htmlBboxIndicesByIndex = textContentItems.map(item => {
const cellBbox = getBbox(item, this.viewport);
let isInHtmlBbox = false;
if (htmlBboxInfo?.bboxes?.length) {
isInHtmlBbox = htmlBboxInfo.bboxes.some(bbox => {
return htmlBboxInfo.bboxes.findIndex(bbox => {
return bboxesIntersect(cellBbox, [bbox.left, bbox.top, bbox.right, bbox.bottom]);
});
}
return -1;
});
const cells = textContentItems.map((item, index) => {
const cellBbox = getBbox(item, this.viewport);
const isInHtmlBbox = htmlBboxIndicesByIndex[index] >= 0;
return new PdfTextContentTextLayoutCell(this, index, item, pageNum, cellBbox, isInHtmlBbox);
});

const htmlBboxIndicesPatched = htmlBboxIndicesByIndex.reduce(
(acc, value, index) => {
acc[index] = value >= 0 ? value : index > 0 ? acc[index - 1] : -1;
return acc;
},
[...htmlBboxIndicesByIndex]
);
this.cells = sortBy(cells, cell => htmlBboxIndicesPatched[cell.id]);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2163,7 +2163,7 @@ __metadata:
languageName: node
linkType: hard

"@ibm-watson/discovery-react-components@^1.5.0-beta.29, @ibm-watson/discovery-react-components@workspace:packages/discovery-react-components":
"@ibm-watson/discovery-react-components@^1.5.0-beta.30, @ibm-watson/discovery-react-components@workspace:packages/discovery-react-components":
version: 0.0.0-use.local
resolution: "@ibm-watson/discovery-react-components@workspace:packages/discovery-react-components"
dependencies:
Expand Down Expand Up @@ -10664,7 +10664,7 @@ __metadata:
dependencies:
"@carbon/icons": ^10.5.0
"@cypress/webpack-preprocessor": ^5.11.0
"@ibm-watson/discovery-react-components": ^1.5.0-beta.29
"@ibm-watson/discovery-react-components": ^1.5.0-beta.30
"@ibm-watson/discovery-styles": ^1.5.0-beta.24
"@testing-library/cypress": ^7.0.7
"@types/proper-url-join": ^2
Expand Down