Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.xdrop:fuzzywuzzy:1.3.0'
}
dependencies {
implementation("com.github.xdrop:fuzzywuzzy:1.3.0")
}
<dependency>
<groupId>com.github.xdrop</groupId>
<artifactId>fuzzywuzzy</artifactId>
<version>1.3.0</version>
</dependency>
libraryDependencies += "com.github.xdrop" % "fuzzywuzzy" % "1.3.0"
:dependencies [[com.github.xdrop/fuzzywuzzy "1.3.0"]]
Fuzzy string matching for java based on the FuzzyWuzzy Python algorithm. The algorithm uses Levenshtein distance to calculate similarity between strings.
I've personally needed to use this but all of the other Java implementations out there either had a crazy amount of dependencies, or simply did not output the correct results as the python one, so I've decided to properly re-implement this in Java. Enjoy!
In Maven and Gradle examples, remember to replace "VERSION
" with the
latest release of this
library.
<dependency>
<groupId>me.xdrop</groupId>
<artifactId>fuzzywuzzy</artifactId>
<version>VERSION</version>
</dependency>
repositories {
mavenCentral()
}
dependencies {
implementation 'me.xdrop:fuzzywuzzy:VERSION'
}
If you use Java 9 or newer, and use the Java Platform Module System (JPMS), you will need to add
the following declarations to your module-info.java
file:
module my.modulename.here {
requires java.base;
requires me.xdrop.fuzzywuzzy;
}
Download the latest release here and add to your classpath.
FuzzySearch.ratio("mysmilarstring","myawfullysimilarstirng")
72
FuzzySearch.ratio("mysmilarstring","mysimilarstring")
97
FuzzySearch.partialRatio("similar", "somewhresimlrbetweenthisstring")
71
FuzzySearch.tokenSortPartialRatio("order words out of"," words out of order")
100
FuzzySearch.tokenSortRatio("order words out of"," words out of order")
100
FuzzySearch.tokenSetRatio("fuzzy was a bear", "fuzzy fuzzy fuzzy bear")
100
FuzzySearch.tokenSetPartialRatio("fuzzy was a bear", "fuzzy fuzzy fuzzy bear")
100
FuzzySearch.weightedRatio("The quick brown fox jimps ofver the small lazy dog", "the quick brown fox jumps over the small lazy dog")
97
// groovy
FuzzySearch.extractOne("cowboys", ["Atlanta Falcons", "New York Jets", "New York Giants", "Dallas Cowboys"])
(string: Dallas Cowboys, score: 90, index: 3)
FuzzySearch.extractTop("goolge", ["google", "bing", "facebook", "linkedin", "twitter", "googleplus", "bingnews", "plexoogl"], 3)
[(string: google, score: 83, index: 0), (string: googleplus, score: 63, index:5), (string: plexoogl, score: 43, index: 7)]
FuzzySearch.extractAll("goolge", ["google", "bing", "facebook", "linkedin", "twitter", "googleplus", "bingnews", "plexoogl"]);
[(string: google, score: 83, index: 0), (string: bing, score: 20, index: 1), (string: facebook, score: 29, index: 2), (string: linkedin, score: 29, index: 3), (string: twitter, score: 15, index: 4), (string: googleplus, score: 63, index: 5), (string: bingnews, score: 29, index: 6), (string: plexoogl, score: 43, index: 7)]
// score cutoff
FuzzySearch.extractAll("goolge", ["google", "bing", "facebook", "linkedin", "twitter", "googleplus", "bingnews", "plexoogl"], 40)
[(string: google, score: 83, index: 0), (string: googleplus, score: 63, index: 5), (string: plexoogl, score: 43, index: 7)]
FuzzySearch.extractSorted("goolge", ["google", "bing", "facebook", "linkedin", "twitter", "googleplus", "bingnews", "plexoogl"]);
[(string: google, score: 83, index: 0), (string: googleplus, score: 63, index: 5), (string: plexoogl, score: 43, index: 7), (string: facebook, score: 29, index: 2), (string: linkedin, score: 29, index: 3), (string: bingnews, score: 29, index: 6), (string: bing, score: 20, index: 1), (string: twitter, score: 15, index: 4)]
// score cutoff
FuzzySearch.extractSorted("goolge", ["google", "bing", "facebook", "linkedin", "twitter", "googleplus", "bingnews", "plexoogl"], 3);
[(string: google, score: 83, index: 0), (string: googleplus, score: 63, index: 5), (string: plexoogl, score: 43, index: 7)]
extractOne
and related methods can receive Collection<T>
and produce BoundExtractedResult<T>
List<Foo> foo = ...;
BoundExtractedResult<Foo> match = FuzzySearch.extractOne("cowboys", foo, x -> x.toString());
Foo matchFoo = match.getReferent();