|
| 1 | +String-Distance Similarity Metrics |
| 2 | +================================== |
| 3 | + |
| 4 | +``fuzzy`` exposes only difflib's gestalt ratio. This adds the edit-distance and |
| 5 | +token-set metrics it lacks — Levenshtein / Damerau-Levenshtein, Jaro and |
| 6 | +Jaro-Winkler (the standard for short names and labels), and character-n-gram |
| 7 | +Jaccard / Dice — for better matching of typos and reordered tokens, especially |
| 8 | +from OCR. |
| 9 | + |
| 10 | +Pure standard library; imports no ``PySide6``. Every function is pure (two |
| 11 | +strings in, a number out), so it is fully deterministic in CI. Pair with |
| 12 | +``normalize_text`` to make matches accent- and form-insensitive first. |
| 13 | + |
| 14 | +Headless API |
| 15 | +------------ |
| 16 | + |
| 17 | +.. code-block:: python |
| 18 | +
|
| 19 | + from je_auto_control import ( |
| 20 | + levenshtein, damerau_levenshtein, jaro_winkler, jaccard, dice, |
| 21 | + similarity, normalize_text, |
| 22 | + ) |
| 23 | +
|
| 24 | + levenshtein("kitten", "sitting") # 3 |
| 25 | + damerau_levenshtein("ab", "ba") # 1 (transposition) |
| 26 | + jaro_winkler("MARTHA", "MARHTA") # ~0.961 |
| 27 | + jaccard("night", "nacht", n=2) # char-bigram overlap |
| 28 | +
|
| 29 | + # normalised [0, 1] score for any metric (edit distance -> 1 - d/max_len): |
| 30 | + similarity(normalize_text(a), normalize_text(b), metric="jaro_winkler") |
| 31 | +
|
| 32 | +``levenshtein`` / ``damerau_levenshtein`` return integer edit distances (the |
| 33 | +latter counting an adjacent transposition as one edit). ``jaro`` / ``jaro_winkler`` |
| 34 | +and ``jaccard`` / ``dice`` return ``[0, 1]`` similarities. ``similarity`` is the |
| 35 | +unified entry point — it returns the Jaro/Jaccard/Dice metrics directly and |
| 36 | +converts edit distances to ``1 - distance / max_len`` so every metric is |
| 37 | +comparable on the same scale. |
| 38 | + |
| 39 | +Executor command |
| 40 | +---------------- |
| 41 | + |
| 42 | +``AC_text_similarity`` returns ``{score}`` for two strings ``a`` / ``b`` and an |
| 43 | +optional ``metric``. It is exposed as the MCP tool ``ac_text_similarity`` and as |
| 44 | +a Script Builder command under **Data**. |
0 commit comments