r/LanguageTechnology • • 8d ago

Matching text (strings) from different algorithms

I have a problem that has preoccupied me for a while now and I'm hoping to get some input here.

On the face of it, the problem itself is fairly simple, match each element from one list of strings to a (combination of) element(s) in another list of strings.

The catch is that one list represents document segments (as obtained from an embedding-based segmentation algorithm) while the other list represents raw pdf element texts (from a parsing algorithm using fitz and spacy) and they don't tend to match on an individual basis. On an aggregate basis both lists represent the full text of the document but segments tend to be larger and correspond to a number of pdf elements.

A possible solution was to simply concatenate pdf element texts based on each individual segment's token length and while this works okay-ish, this approach can get very slow for large documents.

I have been using rapidfuzz to do the matching. The purpose of the matching is that I intend to do interactive pdf highlighting based on coordinates. These coordinates exist on pdf element level while similarity search is usually done on segment embedding level.

Any ideas come to mind?

4 Upvotes

7 comments sorted by

3

u/BoyetteMercadez 5d ago

fuzzy matching gets brutal at scale because of the quadratic comparisons. imo the cleanest fix is normalizing whitespace on both sides and tracking character spans from the start, that way youre just doing integer range lookups to get the bounding boxes

1

u/RDA92 3d ago

Quite frankly I don't think fuzzymatching is the main culprit behind slow speeds atm, rather that goes to creating concatenated strings of the same size than the reference string for lookup purposes. At least there is a huge difference in speed if I manage to narrow down the volume of those concatenated strings.

Not sure I understand the idea on character spans, especially since the reference string is very likely to be a combination of multiple individual items from the PDF element texts list.

2

u/Achrus 6d ago

I’m not sure if I am following 100% but I have used a Longest Common Substring approach for something similar. There’s also Longest Common Subsequence if you’re doing fuzzy matching which allows for insertions and deletions.

If speed is an issue, you could try fuzzywuzzy with pylevenshtein or python-Levenshtein (they keep changing the name) which might speed it up. There is also `regex`, different from the built in `re`, that supports partial Regex matches for insertions, deletions, and substitutions.

1

u/RDA92 3d ago

I have tried sub-string matching in the past and it unfortunately did take a toll on accuracy. The main problem is that pdf element texts are, on average, quite a bit smaller than reference segments

2

u/boodleboodle 6d ago

Adding character index metadata on both types of chunks and converting this to a integer range problem

1

u/RDA92 3d ago

so if I understand correctly, instead of comparing strings I should turn each string into a integer vector where each character is represented with its unique integer ID and compare it this way?