Skip to main content

A Python wrapper for the Text Diff Engine with block moves API

Project description

text-diff-engine

A Python wrapper for the Text Diff Engine with block moves API. This package provides an easy-to-use interface to compare two versions of text and highlight differences such as added, deleted, and moved blocks.

Installation

Since this package is private and part of your Django repository, you can install it locally:

pip install text-diff-engine

Usage

Initializing the Client

Import the TextDiffEngine class and initialize it with your API key:

from text_diff_engine import TextDiffEngine

# Replace 'YOUR_API_KEY' with your actual API key.
diff_engine = TextDiffEngine(api_key='YOUR_API_KEY')

Comparing Texts

Use the compare method to compare an original text with an updated text. Choose the output format (json or html):

old_text = "The quick brown fox jumps over the lazy dog."
new_text = "The quick red fox jumped over the sleeping dog."

# Compare the texts and get the result in JSON format.
result_json = diff_engine.compare(old_text, new_text, output_format="json")
print(result_json)

# Compare the texts and get the result in HTML format.
result_html = diff_engine.compare(old_text, new_text, output_format="html")
print(result_html)

Return Structure

The compare method returns a dict object with two main sections:

  • old_diff: Represents the differences in the old text.
  • new_diff: Represents the differences in the new text.

Additional metadata:

  • identical: Boolean indicating if the texts are identical.
  • tokens_left: Number of API tokens remaining.

Like so:

{
  "old_diff": [...],
  "new_diff": [...],
  "identical": false,
  "tokens_left": 467
}

HTML diff output structure

<span class='deleted'>...</span> <!-- Marks removed text (old_diff) -->
<span class='added'>...</span> <!-- Marks newly added text (new_diff) -->
<span class='move-from-block' id='move-from-X'>...</span> <!-- Marks moved text origin (old_diff) -->
<span class='move-to-block' id='move-to-X'>...</span> <!-- Marks moved text destination (new_diff) -->
<span data-identifier='X'>...</span> <!-- Marks unchanged segments (for scroll sync) -->

JSON diff output structure

{ "type": "DELETED", "text": "..." }  // Marks removed text (old_diff)
{ "type": "ADDED", "text": "..." }    // Marks newly added text (new_diff)
{ "type": "MOVE-FROM", "move_id": X, "blocks": [...] }  // Marks moved text origin (old_diff)
{ "type": "MOVE-TO", "move_id": X, "blocks": [...] }    // Marks moved text destination (new_diff)
{ "type": "UNCHANGED", "text": "...", "identifier": X} // Marks unchanged segments (for scroll sync)

Example output of the compare method

{
  "old_diff": [
    { "move_id": 2, "type": "MOVE-FROM", "blocks": [
      { "type": "UNCHANGED", "text": "The golden " },
      { "type": "DELETED", "text": "sunlight" },
      { "type": "UNCHANGED", "text": " filtered through the trees" },
      { "type": "DELETED", "text": ". Casting"},
      { "type": "UNCHANGED", "text": " long shadows on the quiet street." }
    ] },
    { "type": "DELETED", "text": " " },
    { "type": "UNCHANGED", "text": "A cat slept near the window.", "identifier": 1 },
    { "type": "DELETED", "text": "The clock ticked steadily." },
    { "type": "UNCHANGED", "text": "Wind moved the curtains slightly, making them sway gently.", "identifier": 2 }
  ],
  "new_diff": [
    { "type": "UNCHANGED", "text": "A cat slept near the window.", "identifier": 1 },
    { "type": "UNCHANGED", "text": "Wind moved the curtains slightly, making them sway gently.", "identifier": 2 },
    { "type": "ADDED", "text": " " },
    { "type": "ADDED", "text": "The clock ticked steadily." },
    { "move_id": 2, "type": "MOVE-TO", "blocks": [
      { "type": "UNCHANGED", "text": "The golden " },
      { "type": "ADDED", "text": "rays" },
      { "type": "UNCHANGED", "text": " filtered through the trees"},
      { "type": "ADDED", "text": ", casting" },
      { "type": "UNCHANGED", "text": " long shadows on the quiet street." }
    ] }
  ],
  "identical": false,
  "tokens_left": 467
}

For further details, refer to the Formamind Text Diff Engine documentation.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contact

For support, contact your internal development team or email at contact@formamind.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

text_diff_engine-3.0.1.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

text_diff_engine-3.0.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file text_diff_engine-3.0.1.tar.gz.

File metadata

  • Download URL: text_diff_engine-3.0.1.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.18

File hashes

Hashes for text_diff_engine-3.0.1.tar.gz
Algorithm Hash digest
SHA256 114ae791605b07177b35e6469f768d491cb9ac42893e03abeb83e8ed1e36eb69
MD5 c04ce8ccd89698ac9dea216052c4eecf
BLAKE2b-256 318d3eb1fa09b3057cc47ebf0cb21b92bc218ebc7a2e59f03bbee7ca17dabbb9

See more details on using hashes here.

File details

Details for the file text_diff_engine-3.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for text_diff_engine-3.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 942e62246656b5e3cfb2dc5931b2cefdc864b734e17cb85f767a6ed59a6d56cc
MD5 ce2724ace0c89b6d8384da1e37589059
BLAKE2b-256 bdfbaa43ef387717d38066d214a039a514d68a422dc231de74a46e8a69d184c2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page