
Two spellings of a book title may represent a typo, a sequel or a different edition. A similarity score can prioritize a human review queue without merging records automatically. A high score can occur for different edition numbers. The disagreement's location matters to a cataloguer even when most letters match.
Reproduce the input boundary
The script runs difflib.SequenceMatcher against three independent inputs. Each call starts from the value shown, so changing one example leaves the other expectations available for comparison. The table records real return values from the example. Quotation marks around text in the table make its boundaries visible.
import difflib
def evaluate(value):
return round(difflib.SequenceMatcher(None,value[0],value[1],autojunk=False).ratio(),3)
for sample in [['night train', 'night trains'], ['Atlas 1', 'Atlas 2'], ['map', 'river']]:
print(evaluate(sample))Inspect the boundary cases
| Input to evaluate | Observed result |
|---|---|
| ["night train", "night trains"] | 0.957 |
| ["Atlas 1", "Atlas 2"] | 0.857 |
| ["map", "river"] | 0.0 |
For ["night train", "night trains"], the observed output is 0.957. With ["Atlas 1", "Atlas 2"], it becomes 0.857. These are useful regression fixtures because an integration test can send both through the same entry point. A test of only the first case can stay green after a change that affects the other.
The value for ["map", "river"] is 0.0. Record the unit or indexing convention next to that expectation. A second implementation can return the same type and still disagree about where a boundary falls or which observations enter the calculation.
Check the calling code
Calibrate any review threshold on labelled examples in the application's actual mixture of languages and title lengths.
A practical test can begin with assert evaluate(['night train', 'night trains']) == 0.957. Add the other rows as separate expectations so one failure identifies the input that changed. For a service fed by stored data, repeat the check on one anonymized record from an earlier version of its file or message format.
One more fixture before integrating
Retain the two original titles and the changed characters in the review UI. A high ratio can be driven by a long shared series prefix even when the short distinguishing edition suffix is precisely what a librarian cares about.
The same Python function was also run with [] / ["night train"] / ["night trains", "night train"]. These additional inputs change the boundary without changing the implementation. An exception name in the table records where the operation rejects an input; a returned empty or zero value records a successful call. Choose deliberately which of those outcomes the surrounding feature should expose.
| Additional fixture | Result or exception |
|---|---|
| [] | raises IndexError |
| ["night train"] | raises IndexError |
| ["night trains", "night train"] | 0.957 |
Python’s difflib reference · Python standard library
Sources and further reading
Cover photograph: MacBook Pro 16 (M1 Pro, 2021) - Wikipedia by Premeditated. CC BY-SA 4.0. Cropped and typeset for this cover. The photograph illustrates the topic. This image adaptation uses the same share-alike license.
