apache / incubator-annotator

Apache Annotator provides annotation enabling code for browsers, servers, and humans.
https://annotator.apache.org/
Apache License 2.0
218 stars 44 forks source link

Fuzzy text quote matching #83

Open Treora opened 4 years ago

Treora commented 4 years ago

Many annotation tools want to match a quote also when it has been modified slightly, but we have yet to implement this.

Enabling approximate/fuzzy string matching could be an option to our existing implementation (perhaps a parameter that tells how fuzzy the match should be, where 0 means exact matching); alternatively it could be exposed as a separate implementation.

A second question is whether the matcher should return information about the quality of the match, and if so, how the API would look. I suppose a match object could have an extra attribute expressing the ‘match quality’; though in case of refined/range selectors we should figure out how to propagate this information.

Prior art we could borrow from:

Treora commented 3 years ago

Prior art we could borrow from:

Update: @judell kindly informed us that Hypothesis now switched from the former to the latter. See https://github.com/hypothesis/client/pull/2814 and https://github.com/hypothesis/client/pull/2779

I’d be eager to see how they compare (at least it’s supposed to be much faster now!), what could still be improved, etc.

Some observations from looking at how exactly approx-string-match-js is being used in H:

CC @robertknight (happy to hear if you have more research notes, experimental results or other relevant tips from your experience developing this!)

robertknight commented 3 years ago

CC @robertknight (happy to hear if you have more research notes, experimental results or other relevant tips from your experience developing this!)

Aside from taking ideas from Hypothesis's technical implementation, which you've already posted pointers to, the other resource I would suggest to make use of from Hypothesis are datasets of annotations in the "Public" channel. Here are some I found useful for testing quote matching performance and accuracy:

The new quote matching implementation in Hypothesis has a couple of areas where we've noticed matching quality can be improved:

  1. It can find spurious matches for short quotes (in particular, those of one or two words). In the PR I mention a couple of examples.
  2. In the case where the match is not exact, alignment can be sub-optimal in some cases. Looking at public Hypothesis annotations on http://www.americanyawp.com/text/01-the-new-world/ for example you can find cases where the Hypothesis client draws highlights that start or end in unlikely places (eg. middle of a word).

Related to point (1), one of the goals of the new implementation was to try to make it easier for other Hypothesis developers and staff to understand how exactly the "fuzzy" aspect of "fuzzy matching" works. The thinking is that if it is imperfect, then there is value in at least being predictable.

In terms of performance, the new implementation is indeed a lot faster in the worst case where there are many selectors that either do not match at all or match with significant edits. The actual approximate string matching code is pretty well optimized at this point. The next lowest-hanging fruit is optimizing the extraction of text from the document and mapping between text positions and DOM (node, offset) points. If we find that we need to make significant improvements from the current implementation in future then we'd likely need to do some offline processing of the document text before searching for matches.