#! /usr/bin/python import sys import cgi import cdiff form = cgi.FieldStorage() a = form.getvalue( "a" ) b = form.getvalue( "b" ) n = int( form.getvalue( "n", "0" ) ) #a = "The Private War of Private Jacob" #b = "The Private War of Pvt. Jackob" #n = 1 if (n): a = cdiff.normalise( a ) b = cdiff.normalise( b ) diff = cdiff.Difference( a, b ) d = diff.distance() trace = diff.trace() s = diff.similarity() out = sys.stdout out.write( "Content-type: text/html\n" ) out.write( "\n" ) out.write( """ Edit Distance

Edit Distance

""" ) out.write( ("

String A: " + str( a ) + "

\n") ) out.write( ("

String B: " + str( b ) + "

\n") ) out.write( ("

Edit Distance: " + str( d ) + "

\n") ) out.write( ("

Similarity: " + str( s ) + "

\n") ) out.write( ("

Trace: " + trace.replace( "<", "<" ) + "

\n") ) out.write( ("

Encoding: " + (" ".join( diff.encoding() )) + "

\n") ) out.write( """

The trace is a representation of how the strings compare; read from the beginning, | means 'there is the same character in each input', . means 'there is a different character in each input', > means 'there is a character in A but not B', and < means 'there is a character in B but not A'.

The encoding is a representation of the differences between the strings, as a set of coded modifications to string A to produce string B; Xn indicates that character X at position n should be removed, nY indicates that character Y should be inserted at position n (pushing subsequent characters along) and XnY indicates that character X and position n should be replaced with character Y.

""" ) out.flush()