Package translate :: Package misc :: Module diff_match_patch :: Class diff_match_patch
[hide private]
[frames] | no frames]

Class diff_match_patch

source code

Class containing the diff, match and patch methods.

Also contains the behaviour settings.

Instance Methods [hide private]
 
__init__(self)
Inits a diff_match_patch object with default settings.
source code
 
diff_main(self, text1, text2, checklines=True)
Find the differences between two texts.
source code
 
diff_compute(self, text1, text2, checklines)
Find the differences between two texts.
source code
 
diff_linesToChars(self, text1, text2)
Split two texts into an array of strings.
source code
 
diff_charsToLines(self, diffs, lineArray)
Rehydrate the text in a diff from a string of line hashes to real lines of text.
source code
 
diff_map(self, text1, text2)
Explore the intersection points between the two texts.
source code
 
diff_path1(self, v_map, text1, text2)
Work from the middle back to the start to determine the path.
source code
 
diff_path2(self, v_map, text1, text2)
Work from the middle back to the end to determine the path.
source code
 
diff_commonPrefix(self, text1, text2)
Determine the common prefix of two strings.
source code
 
diff_commonSuffix(self, text1, text2)
Determine the common suffix of two strings.
source code
 
diff_halfMatch(self, text1, text2)
Do the two texts share a substring which is at least half the length of the longer text?
source code
 
diff_cleanupSemantic(self, diffs)
Reduce the number of edits by eliminating semantically trivial equalities.
source code
 
diff_cleanupSemanticLossless(self, diffs)
Look for single edits surrounded on both sides by equalities which can be shifted sideways to align the edit to a word boundary.
source code
 
diff_cleanupEfficiency(self, diffs)
Reduce the number of edits by eliminating operationally trivial equalities.
source code
 
diff_cleanupMerge(self, diffs)
Reorder and merge like edit sections.
source code
 
diff_xIndex(self, diffs, loc)
loc is a location in text1, compute and return the equivalent location in text2.
source code
 
diff_prettyHtml(self, diffs)
Convert a diff array into a pretty HTML report.
source code
 
diff_text1(self, diffs)
Compute and return the source text (all equalities and deletions).
source code
 
diff_text2(self, diffs)
Compute and return the destination text (all equalities and insertions).
source code
 
diff_levenshtein(self, diffs)
Compute the Levenshtein distance; the number of inserted, deleted or substituted characters.
source code
 
diff_toDelta(self, diffs)
Crush the diff into an encoded string which describes the operations required to transform text1 into text2.
source code
 
diff_fromDelta(self, text1, delta)
Given the original text1, and an encoded string which describes the operations required to transform text1 into text2, compute the full diff.
source code
 
match_main(self, text, pattern, loc)
Locate the best instance of 'pattern' in 'text' near 'loc'.
source code
 
match_bitap(self, text, pattern, loc)
Locate the best instance of 'pattern' in 'text' near 'loc' using the Bitap algorithm.
source code
 
match_alphabet(self, pattern)
Initialise the alphabet for the Bitap algorithm.
source code
 
patch_addContext(self, patch, text)
Increase the context until it is unique, but don't let the pattern expand beyond Match_MaxBits.
source code
 
patch_make(self, a, b=None, c=None)
Compute a list of patches to turn text1 into text2.
source code
 
patch_deepCopy(self, patches)
Given an array of patches, return another array that is identical.
source code
 
patch_apply(self, patches, text)
Merge a set of patches onto the text.
source code
 
patch_addPadding(self, patches)
Add some padding on text start and end so that edges can match something.
source code
 
patch_splitMax(self, patches)
Look through the patches and break up any which are longer than the maximum limit of the match algorithm.
source code
 
patch_toText(self, patches)
Take a list of patches and return a textual representation.
source code
 
patch_fromText(self, textline)
Parse a textual representation of patches and return a list of patch objects.
source code
Class Variables [hide private]
  DIFF_DELETE = -1
  DIFF_INSERT = 1
  DIFF_EQUAL = 0
Method Details [hide private]

__init__(self)
(Constructor)

source code 

Inits a diff_match_patch object with default settings. Redefine these in your program to override the defaults.

diff_main(self, text1, text2, checklines=True)

source code 
Find the differences between two texts.  Simplifies the problem by
  stripping any common prefix or suffix off the texts before diffing.

Args:
  text1: Old string to be diffed.
  text2: New string to be diffed.
  checklines: Optional speedup flag.  If present and false, then don't run
    a line-level diff first to identify the changed areas.
    Defaults to true, which does a faster, slightly less optimal diff.

Returns:
  Array of changes.

diff_compute(self, text1, text2, checklines)

source code 
Find the differences between two texts.  Assumes that the texts do not
  have any common prefix or suffix.

Args:
  text1: Old string to be diffed.
  text2: New string to be diffed.
  checklines: Speedup flag.  If false, then don't run a line-level diff
    first to identify the changed areas.
    If true, then run a faster, slightly less optimal diff.

Returns:
  Array of changes.

diff_linesToChars(self, text1, text2)

source code 
Split two texts into an array of strings.  Reduce the texts to a string
of hashes where each Unicode character represents one line.

Args:
  text1: First string.
  text2: Second string.

Returns:
  Three element tuple, containing the encoded text1, the encoded text2 and
  the array of unique strings.  The zeroth element of the array of unique
  strings is intentionally blank.

diff_charsToLines(self, diffs, lineArray)

source code 
Rehydrate the text in a diff from a string of line hashes to real lines
of text.

Args:
  diffs: Array of diff tuples.
  lineArray: Array of unique strings.

diff_map(self, text1, text2)

source code 
Explore the intersection points between the two texts.

Args:
  text1: Old string to be diffed.
  text2: New string to be diffed.

Returns:
  Array of diff tuples or None if no diff available.

diff_path1(self, v_map, text1, text2)

source code 
Work from the middle back to the start to determine the path.

Args:
  v_map: Array of paths.
  text1: Old string fragment to be diffed.
  text2: New string fragment to be diffed.

Returns:
  Array of diff tuples.

diff_path2(self, v_map, text1, text2)

source code 
Work from the middle back to the end to determine the path.

Args:
  v_map: Array of paths.
  text1: Old string fragment to be diffed.
  text2: New string fragment to be diffed.

Returns:
  Array of diff tuples.

diff_commonPrefix(self, text1, text2)

source code 
Determine the common prefix of two strings.

Args:
  text1: First string.
  text2: Second string.

Returns:
  The number of characters common to the start of each string.

diff_commonSuffix(self, text1, text2)

source code 
Determine the common suffix of two strings.

Args:
  text1: First string.
  text2: Second string.

Returns:
  The number of characters common to the end of each string.

diff_halfMatch(self, text1, text2)

source code 
Do the two texts share a substring which is at least half the length of
the longer text?

Args:
  text1: First string.
  text2: Second string.

Returns:
  Five element Array, containing the prefix of text1, the suffix of text1,
  the prefix of text2, the suffix of text2 and the common middle.  Or None
  if there was no match.

diff_cleanupSemantic(self, diffs)

source code 
Reduce the number of edits by eliminating semantically trivial
equalities.

Args:
  diffs: Array of diff tuples.

diff_cleanupSemanticLossless(self, diffs)

source code 
Look for single edits surrounded on both sides by equalities
which can be shifted sideways to align the edit to a word boundary.
e.g: The c<ins>at c</ins>ame. -> The <ins>cat </ins>came.

Args:
  diffs: Array of diff tuples.

diff_cleanupEfficiency(self, diffs)

source code 
Reduce the number of edits by eliminating operationally trivial
equalities.

Args:
  diffs: Array of diff tuples.

diff_cleanupMerge(self, diffs)

source code 
Reorder and merge like edit sections.  Merge equalities.
Any edit section can move as long as it doesn't cross an equality.

Args:
  diffs: Array of diff tuples.

diff_xIndex(self, diffs, loc)

source code 
loc is a location in text1, compute and return the equivalent location
in text2.  e.g. "The cat" vs "The big cat", 1->1, 5->8

Args:
  diffs: Array of diff tuples.
  loc: Location within text1.

Returns:
  Location within text2.

diff_prettyHtml(self, diffs)

source code 
Convert a diff array into a pretty HTML report.

Args:
  diffs: Array of diff tuples.

Returns:
  HTML representation.

diff_text1(self, diffs)

source code 
Compute and return the source text (all equalities and deletions).

Args:
  diffs: Array of diff tuples.

Returns:
  Source text.

diff_text2(self, diffs)

source code 
Compute and return the destination text (all equalities and insertions).

Args:
  diffs: Array of diff tuples.

Returns:
  Destination text.

diff_levenshtein(self, diffs)

source code 
Compute the Levenshtein distance; the number of inserted, deleted or
substituted characters.

Args:
  diffs: Array of diff tuples.

Returns:
  Number of changes.

diff_toDelta(self, diffs)

source code 
Crush the diff into an encoded string which describes the operations
required to transform text1 into text2.
E.g. =3     -2      +ing  -> Keep 3 chars, delete 2 chars, insert 'ing'.
Operations are tab-separated.  Inserted text is escaped using %xx notation.

Args:
  diffs: Array of diff tuples.

Returns:
  Delta text.

diff_fromDelta(self, text1, delta)

source code 
Given the original text1, and an encoded string which describes the
operations required to transform text1 into text2, compute the full diff.

Args:
  text1: Source string for the diff.
  delta: Delta text.

Returns:
  Array of diff tuples.

Raises:
  ValueError: If invalid input.

match_main(self, text, pattern, loc)

source code 
Locate the best instance of 'pattern' in 'text' near 'loc'.

Args:
  text: The text to search.
  pattern: The pattern to search for.
  loc: The location to search around.

Returns:
  Best match index or -1.

match_bitap(self, text, pattern, loc)

source code 
Locate the best instance of 'pattern' in 'text' near 'loc' using the
Bitap algorithm.

Args:
  text: The text to search.
  pattern: The pattern to search for.
  loc: The location to search around.

Returns:
  Best match index or -1.

match_alphabet(self, pattern)

source code 
Initialise the alphabet for the Bitap algorithm.

Args:
  pattern: The text to encode.

Returns:
  Hash of character locations.

patch_addContext(self, patch, text)

source code 
Increase the context until it is unique,
but don't let the pattern expand beyond Match_MaxBits.

Args:
  patch: The patch to grow.
  text: Source text.

patch_make(self, a, b=None, c=None)

source code 
Compute a list of patches to turn text1 into text2.
Use diffs if provided, otherwise compute it ourselves.
There are four ways to call this function, depending on what data is
available to the caller:
Method 1:
a = text1, b = text2
Method 2:
a = diffs
Method 3 (optimal):
a = text1, b = diffs
Method 4 (deprecated, use method 3):
a = text1, b = text2, c = diffs

Args:
  a: text1 (methods 1,3,4) or Array of diff tuples for text1 to
      text2 (method 2).
  b: text2 (methods 1,4) or Array of diff tuples for text1 to
      text2 (method 3) or undefined (method 2).
  c: Array of diff tuples for text1 to text2 (method 4) or
      undefined (methods 1,2,3).

Returns:
  Array of patch objects.

patch_deepCopy(self, patches)

source code 
Given an array of patches, return another array that is identical.

Args:
  patches: Array of patch objects.

Returns:
  Array of patch objects.

patch_apply(self, patches, text)

source code 
Merge a set of patches onto the text.  Return a patched text, as well
as a list of true/false values indicating which patches were applied.

Args:
  patches: Array of patch objects.
  text: Old text.

Returns:
  Two element Array, containing the new text and an array of boolean values.

patch_addPadding(self, patches)

source code 
Add some padding on text start and end so that edges can match
something.  Intended to be called only from within patch_apply.

Args:
  patches: Array of patch objects.

Returns:
  The padding string added to each side.

patch_splitMax(self, patches)

source code 
Look through the patches and break up any which are longer than the
maximum limit of the match algorithm.

Args:
  patches: Array of patch objects.

patch_toText(self, patches)

source code 
Take a list of patches and return a textual representation.

Args:
  patches: Array of patch objects.

Returns:
  Text representation of patches.

patch_fromText(self, textline)

source code 
Parse a textual representation of patches and return a list of patch
objects.

Args:
  textline: Text representation of patches.

Returns:
  Array of patch objects.

Raises:
  ValueError: If invalid input.