A Java library for aligning two generic lists using the Damerau Levenshtein algorithm.
Inspired by Errant by Chris Bryant and diff_match_patch by Neil Fraser.
Before you begin, ensure you have met the following requirements:
- You have Java 11 installed.
Add this to the dependencies section in your pom.xml
:
<dependency>
<groupId>io.github.manzurola</groupId>
<artifactId>aligner</artifactId>
<version>0.3.0</version>
</dependency>
To use Aligner in code, follow these steps:
// Prepare source and target lists to be aligned
List<Integer> source = List.of(1, 3, 3);
List<Integer> target = List.of(1, 2, 3);
// Create a new aligner via one of the available static factory methods
Aligner<String> aligner = Aligner.damerauLevenshtein();
// Align source and target
Alignment<String> alignment = aligner.align(source,target);
// Get the cost and distance of the alignment
double cost = alignment.cost();
double distance = alignment.distance();
System.out.printf("Cost: %s, distance: %s%n", cost, distance);
// Inspect individual edits
for (Edit<Integer> edit : alignment.edits()) {
Operation op = edit.operation();
Segment<Integer> s = edit.source();
Segment<Integer> t = edit.target();
System.out.printf("Operation: %s, source: %s, target: %s%n", op, s, t);
}
You can also customise an Aligner with your own metrics:
// The equality operation is used to determine whether two elements are equal
Equalizer<Integer> equalizer = Integer::equals;
// The comparator is used to sort and compare two candidate lists for transposition
Comparator<Integer> comparator = Integer::compareTo;
// This cost function disables substitution for elements with values (3,2) by returning a Double.MAX_VALUE
// when matched
SubstituteCost<Integer> substituteCost = (s, t) -> s == 3 && t == 2 ? Double.MAX_VALUE : 1.0;
// A custom damerau levenshtein aligner
Aligner<Integer> aligner = Aligner.damerauLevenshtein(equalizer, comparator, substituteCost);
To contribute to Aligner, follow these steps:
- Fork this repository.
- Create a branch:
git checkout -b <branch_name>
. - Make your changes and commit them:
git commit -m '<commit_message>'
- Push to the original branch:
git push origin <project_name>/<location>
- Create the pull request.
Alternatively see the GitHub documentation on creating a pull request.
Thanks to the following people who have contributed to this project:
If you want to contact me you can reach me at [email protected].
This project uses the following license: MIT.