r/LanguageTechnology • u/RDA92 • 3d 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?
1
u/Achrus 1d 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/boodleboodle 15h ago
Adding character index metadata on both types of chunks and converting this to a integer range problem
1
u/BoyetteMercadez 14h 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