• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java String Text Difference Comparison

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been given a task to either create or find logic that allows for the comparison of the contents of two Java strings, noting the differences. This should work similar to MS Word's track changes functionality.
I have been searching for open source logic that accomplishes this but to no avail.
This is my assignment. Create a method to compare two strings and output the differences as HTML styles. A possible method sigature could be:
public static String compareString(String currentText, String editedText, String currentTextHtmlStyle, String editedTextHtmlStyle){}

Here is an example:
Current Text - I am some reference text.
Edited Text - I am some edited text.
Current Text Style - currentHtmlStyle
Edited Text Style - editedHtmlStyle
The output should look like the following:
I am some <span class="currentHtmlStyle">reference</span><span class="editedHtmlStyle">edited</span> text.
Of course it gets more complicated if the Current Text were to read something like this: I am some reference text, an example of edited text.
If any one knows how to accomplish this or knows where to find open source, I would greatly appreciate any and all assistance.
Thanks!!
[ March 12, 2004: Message edited by: geoffrey baltzer ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found some free code for doing text file compares. The code was, um, not appealing, but the algorithm was simple enough I could write my own version. I suppose you could adapt it to compare words instead of lines. It would fall down at comparing letters, tho, with too many matches. Here's some info from my package.html. This gives the broadest of hints. It would be a rather advanced task to go from here to code. But if you want to give it a shot, see how far you get and come back with code samples of your own.
TextDiff compares two text files or arrays of strings and generates a report of edit commands that would transform Old to New.
The algorithm (but no code) was taken from Java code by Ian F. Darwin, ian@darwinsys.com, January, 1997. Darwin's code was a translation of a C program by D. C. Lindsay, C (1982-1987)
The algorithm is:
For each each unique line of text create a symbol. The symbol state is: OldOnly, NewOnly, UniqueMatch (both files exactly once), or Other.
For each line, create a LineInfo object. Set state = symbol state and establish bidirectional links between UniqueMatch lines in the two files.
For each UniqueMatch in old create a "match block". Stretch match blocks forward and backward to include matching lines with any state, including other match blocks.
Build a Report of edit commands that can be used to tranform Old into New.
Matching blocks generate match or move commands. Non-matching blocks generate insert, append, delete or change commands.
Iterate the commands to generate a report.
The DefaultReportWriter prints a human-friendly report to a PrintStream such as System.out. One could implement custom report writers to create machine-readable reports such as concrete editor commands.
 
reply
    Bookmark Topic Watch Topic
  • New Topic