Home / Blog / Plagiarism & similarity

File and document comparison

Python Compare Text Similarity

Measure a defined text similarity score in Python with a reproducible example, explicit handling of whitespace and revision direction, and checks before saving the reviewed result.

Python Compare Text Similarity

Use a named algorithm and report what it compares. Python’s SequenceMatcher can compare character sequences, but its ratio is not a probability that one passage copied another and does not measure whether the passages have the same meaning. Preserve the original text and make any normalization an explicit part of the experiment.

Calibrate the score on your own examples

Record an identical pair, a lightly edited pair and a pair that changes a crucial claim. Those examples show what the chosen ratio notices and what it misses. A threshold suitable for catching near-duplicate labels may be unsuitable for evaluating paragraphs or code. Keep the raw score and the algorithm settings with each result, and inspect borderline cases rather than presenting a decimal as a final semantic judgment.

Measure a defined text similarity score in Python

from difflib import SequenceMatcher

a = "The meeting starts at noon."
b = "The meeting starts at two."
forward = SequenceMatcher(None, a, b, autojunk=False).ratio()
reverse = SequenceMatcher(None, b, a, autojunk=False).ratio()
print({"forward_ratio": forward, "reverse_ratio": reverse})
print({"exact_equal": a == b})

Verify a known change

Run an identical pair, a completely different pair and a pair that changes just one number or a negation. The identical pair should return 1.0. A sentence with “not” inserted can still receive a high character similarity even though its meaning changes. The example above changes the meeting time; a high ratio does not make the statements interchangeable. SequenceMatcher can depend on argument order, so recording both directions makes that limitation visible instead of presenting one unexplained percentage.

Choose the right interpretation

Set a threshold only after testing data representative of the decision you need to make. For duplicate detection, exact equality after a documented normalization may be clearer than an arbitrary fuzzy score. For academic or source review, inspect the matching passages and citations. An algorithm comparing two supplied strings has not searched the web, private submissions or subscription databases. Do not label its result as proof of originality or misconduct.

Keep the comparison reproducible

Label the two inputs Original and Revised and keep those roles consistent. Save a copy of each before experimenting with normalization or accepting changes. A reversed comparison can show the same edit with the opposite insertion/deletion direction, which matters when a reviewer is deciding what to retain. Record the two filenames and the settings used with the result.

Test an identical pair first: it should produce no differences under the chosen contract. Then change one ordinary word, one number and one punctuation mark in the revised copy. Verify each known change before using an unfamiliar document. Include a blank line and a moved paragraph so you can see whether the method reports a move or a deletion followed by an insertion. Those presentations may describe the same underlying edit.

Decide whether spaces, capitalization and line endings matter before normalizing them. Ignoring every space can hide a meaningful change in code, a quoted value or a fixed-width record. Case folding is useful for some lookup tasks but is wrong for case-sensitive identifiers. Retain the unmodified inputs alongside any normalized copies so a surprising result can be investigated rather than explained away.

Review the complete changed region, including its surrounding context. An inserted “not,” a removed qualification or a changed unit can reverse a statement while contributing very few changed characters. Conversely, a formatter can change many lines without changing an application’s intended behavior. A difference count describes the chosen representation; it does not by itself measure importance, quality, originality or semantic equivalence.

Finish the review deliberately

For each meaningful difference, decide whether to keep the original, keep the revised version or write a new combined version. Save that reviewed result under a new filename and compare it again with the intended baseline. Do not overwrite the only original while you are still interpreting the report. Keep a short record of unresolved differences when the review is handed to another person.

Use only files you are authorized to inspect. If a web tool is involved, read its stated processing behavior and file limits before supplying confidential material. A local comparison can avoid uploading the content, but browser downloads, clipboard history and shared-device storage still need ordinary care. Clear temporary copies according to your own retention needs after the review is complete.

Primary reference

Official comparison documentation