Coder Social home page Coder Social logo

javalc6 / stringdiff Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 34 KB

A java application that highlights text differences between two plain strings based on longest common substrings

License: GNU General Public License v2.0

Java 100.00%
java strings string-differences compare-strings compare-text string-compare string-comparison text-compare text-comparison text-diff

stringdiff's Introduction

StringDiff

This java application highlights text differences between two plain strings by generating html fragment to show changes.

The buildLcsList() method finds longest substrings common to both text1 and text2 recursively producing a list of longest common substrings used in markTextDiff() to markup changes between text1 and text2.

Java 8 or later is required to run the application.

Compile

Run following command to build the application:

javac lcs\StringDiff.java

Run

Just use the following command to run the application:

java lcs.StringDiff

Example

Setting the following strings:

text1 = "Do not change this section. Please check any misqelling! Note that this section is obsolete.";
text2 = "New section added. Do not change this section. Please check any mispelling!";

running the command "java lcs.StringDiff" will generate the following html fragment:

<ins style='background-color:#00ff66'>New section added. </ins>Do not change this section. Please check any mis<del style='background-color:#ff9933'>q</del><ins style='background-color:#00ff66'>p</ins>elling!<del style='background-color:#ff9933'> Note that this section is obsolete.</del>

If you load the html fragment with a browser, you get the following result:

html fragment in a browser

Credits

The longest common substrings are found using the LongestCommonSubstring (LCS) algorithm credited to https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/LongestCommonSubstring.java.html

Credits go to Robert Sedgewick and Kevin Wayne that provide the LCS algorithm with GNU General Public License

Caveat

This algorithm based on LCS works well with ordinary texts. In case of specific domains it may produce sub-optimal results.

As an example, consider the string "abceabceabce". If we remove the second 'b' letter, we get the string "abceaceabce". Using the LCS algorithm on these two strings, we get "ceabce" as LCS, i.e. the result from markTextDiff() is "abceaceabceabce", instead the optimal result should be "abceabceabce".

stringdiff's People

Contributors

javalc6 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.