Home / Blog / Plagiarism & similarity

Programmatic text comparisons

Compare Two Text Sets by Membership, Not Line Order

Compare Two Text Sets by Membership, Not Line Order. Apply explicit data rules and checked examples while preserving the original inputs.

Compare Two Text Sets by Membership, Not Line Order

To compare two sets of text, first decide what counts as one item. If each line is an item, set operations can show shared values, values unique to either input and the combined unique collection. Sets ignore repeated occurrences and original order, so they answer a different question from a line-by-line diff.

Work through a small pair

Let A contain red, blue and red on separate lines. Let B contain blue, green and green. Their shared set contains blue; only A contains red; only B contains green. The combined set contains blue, green and red, and the symmetric difference contains green and red.

The two red occurrences in A do not create two set members. If the number of occurrences matters, use a frequency table or multiset comparison instead. A set cannot tell you that red appeared twice because that information was discarded when the list became a set.

Read one item per line and report the operations

from pathlib import Path
import json

a = set(Path("a.txt").read_text(encoding="utf-8").splitlines())
b = set(Path("b.txt").read_text(encoding="utf-8").splitlines())
result = {
    "in_both": sorted(a & b),
    "only_a": sorted(a - b),
    "only_b": sorted(b - a),
    "in_either": sorted(a | b),
    "in_exactly_one": sorted(a ^ b),
}
Path("sets.json").write_text(
    json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")

The script reads UTF-8 files and writes a separate JSON report. Sorting makes the display deterministic; it does not preserve source order or apply a language-specific collation. The JSON keys identify which mathematical operation produced each list, avoiding an ambiguous output labeled merely differences.

Define item identity explicitly

This example preserves case and spaces within each line. Red and red are different strings, as are red and a version with a trailing space. It removes line terminators through splitlines, but does not strip every item or silently lowercase it. Add normalization only when that is part of the agreed matching rule.

An empty stored line becomes an empty-string item. An empty file has no items. Decide whether blank items are meaningful before filtering them out. For identifiers, keep leading zeros: 00123 and 123 remain distinct text values in this comparison.

Keep directional differences labeled

A minus B means values in A that are absent from B. Reversing the inputs changes that result to values in B absent from A. The symmetric difference combines both directions but loses which side supplied each value unless you retain the separate lists as well.

For a reconciliation task, label A as the expected list and B as the observed list only if that is their real role. A missing value and an unexpected extra value can require different follow-up actions. The mathematical operation cannot determine which input is authoritative.

Check a second pair and retain the originals

Use A containing oak and pine and B containing pine and birch. Expect pine in both, oak only in A and birch only in B. This different fixture catches an accidental reuse of a previous report or input path. Check both source files remain unchanged after writing sets.json.

Use the Text Diff Checker when you need ordering, additions and removals in context. Reordering the same set can produce a line diff even though its membership is unchanged. Choose the representation that matches the question rather than treating those results as contradictory.

Parse structured records first

Do not assume each physical line is a complete record in every format. A quoted CSV field can contain a newline. Parse that format first, then choose the record key or tuple that defines membership. Otherwise a line-based set can split records and discard duplicates under the wrong definition.

Python set reference

Python set operations document the operators in the executed example. The result is exact for this stated string-item rule, not a semantic comparison of the meanings of sentences.