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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file text_diff_engine-2.0.0.tar.gz.
File metadata
- Download URL: text_diff_engine-2.0.0.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
518cc3a1fb525b6d660efbe925f4c273ba638ea687ad097551c3da6cbba414c7
|
|
| MD5 |
7718a00cec785df61770ee7c31a644a4
|
|
| BLAKE2b-256 |
137e4dce041a1e96f24c9961b4185e8e3695f3f59d55561e110f9728564a5524
|
File details
Details for the file text_diff_engine-2.0.0-py3-none-any.whl.
File metadata
- Download URL: text_diff_engine-2.0.0-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a37f44138d0b192de38f7cfb033dd2b3c4958b71cf87e40827342cac2ad2cc35
|
|
| MD5 |
21ce8445a12dad27753b9f363f5e37fa
|
|
| BLAKE2b-256 |
cd2cc253264005b047f1b32c7854e767dd48e4affce545a0f52c86dd3a80e50f
|