Package dev.demeng.pluginbase.text
Class StringSimilarity
java.lang.Object
dev.demeng.pluginbase.text.StringSimilarity
Utility class for calculating string similarity using various algorithms. Useful for implementing
fuzzy matching, command suggestions, and typo correction.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanChecks if two strings are similar enough based on a threshold.static intlevenshteinDistance(@NotNull String s1, @NotNull String s2) Calculates the Levenshtein distance between two strings.static doublesimilarity(@NotNull String s1, @NotNull String s2) Calculates the similarity percentage between two strings based on Levenshtein distance.
-
Constructor Details
-
StringSimilarity
public StringSimilarity()
-
-
Method Details
-
levenshteinDistance
Calculates the Levenshtein distance between two strings. The Levenshtein distance is the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one string into the other.- Parameters:
s1- The first strings2- The second string- Returns:
- The Levenshtein distance between the two strings
-
similarity
Calculates the similarity percentage between two strings based on Levenshtein distance. Returns a value between 0.0 (completely different) and 1.0 (identical).- Parameters:
s1- The first strings2- The second string- Returns:
- A similarity score between 0.0 and 1.0
-
isSimilar
public static boolean isSimilar(@NotNull @NotNull String s1, @NotNull @NotNull String s2, double threshold) Checks if two strings are similar enough based on a threshold. Useful for determining if a string is "close enough" to another for fuzzy matching purposes.- Parameters:
s1- The first strings2- The second stringthreshold- The minimum similarity score (0.0 to 1.0) required to consider strings similar- Returns:
- true if the similarity score is greater than or equal to the threshold
-