Repository navigation
Add suggestions when features are incorrectly specified #48963
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/configuration/SimilarityUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package org.keycloak.quarkus.runtime.configuration; | ||
|
|
||
| import java.util.Comparator; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Bigram-based cosine similarity for suggesting corrections to misspelled CLI values. | ||
| * Used by option-name matching ({@link KcUnmatchedArgumentException}) and | ||
| * feature-name validation ({@link org.keycloak.quarkus.runtime.configuration.mappers.FeaturePropertyMappers}). | ||
| */ | ||
| public final class SimilarityUtil { | ||
|
|
||
| public static final int DEFAULT_MAX_SUGGESTIONS = 5; | ||
| public static final double DEFAULT_MIN_SIMILARITY = 0.4; | ||
|
|
||
| private SimilarityUtil() { | ||
| } | ||
|
|
||
| public static List<String> findSimilar(String input, List<String> candidates) { | ||
| return findSimilar(input, candidates, DEFAULT_MAX_SUGGESTIONS, DEFAULT_MIN_SIMILARITY); | ||
| } | ||
|
|
||
| public static double cosineSimilarity(String a, String b) { | ||
| Map<String, Integer> aFreq = bigramFrequency(a); | ||
| Map<String, Integer> bFreq = bigramFrequency(b); | ||
| double dot = dotProduct(aFreq, bFreq); | ||
| double normA = dotProduct(aFreq, aFreq); | ||
| double normB = dotProduct(bFreq, bFreq); | ||
| double denominator = Math.sqrt(normA * normB); | ||
| return denominator == 0 ? 0 : dot / denominator; | ||
| } | ||
|
|
||
| public static List<String> findSimilar(String input, List<String> candidates, int maxSuggestions, double minSimilarity) { | ||
| String lowerInput = input.toLowerCase(); | ||
| return candidates.stream() | ||
| .map(candidate -> Map.entry(cosineSimilarity(lowerInput, candidate.toLowerCase()), candidate)) | ||
| .filter(e -> e.getKey() >= minSimilarity) | ||
| .sorted(Comparator.<Map.Entry<Double, String>, Double>comparing(Map.Entry::getKey).reversed()) | ||
| .limit(maxSuggestions) | ||
| .map(Map.Entry::getValue) | ||
| .toList(); | ||
| } | ||
|
|
||
| private static Map<String, Integer> bigramFrequency(String s) { | ||
| Map<String, Integer> freq = new HashMap<>(); | ||
| for (int i = 0; i < s.length() - 1; i++) { | ||
| freq.merge(s.substring(i, i + 2), 1, Integer::sum); | ||
| } | ||
| return freq; | ||
| } | ||
|
|
||
| private static double dotProduct(Map<String, Integer> m1, Map<String, Integer> m2) { | ||
| return m1.entrySet().stream() | ||
| .collect(Collectors.summingDouble(e -> e.getValue() * (m2.getOrDefault(e.getKey(), 0)))); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
.../runtime/src/test/java/org/keycloak/quarkus/runtime/configuration/SimilarityUtilTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package org.keycloak.quarkus.runtime.configuration; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| public class SimilarityUtilTest { | ||
|
|
||
| C288 td> | @Test | |
| public void exactMatch() { | ||
| assertEquals(1.0, SimilarityUtil.cosineSimilarity("token-exchange", "token-exchange"), 0.001); | ||
| } | ||
|
|
||
| @Test | ||
| public void similarStrings() { | ||
| double similarity = SimilarityUtil.cosineSimilarity("tokn-exchang", "token-exchange"); | ||
| assertTrue("Expected high similarity for near-match, got " + similarity, similarity > 0.5); | ||
| } | ||
|
|
||
| @Test | ||
| public void dissimilarStrings() { | ||
| double similarity = SimilarityUtil.cosineSimilarity("xyz", "token-exchange"); | ||
| assertTrue("Expected low similarity for unrelated strings, got " + similarity, similarity < 0.3); | ||
| } | ||
|
|
||
| @Test | ||
| public void emptyString() { | ||
| assertEquals(0.0, SimilarityUtil.cosineSimilarity("", "test"), 0.001); | ||
| } | ||
|
|
||
| @Test | ||
| public void singleChar() { | ||
| assertEquals(0.0, SimilarityUtil.cosineSimilarity("a", "b"), 0.001); | ||
| } | ||
|
|
||
| @Test | ||
| public void findSimilarReturnsBestMatches() { | ||
| List<String> candidates = List.of("token-exchange", "admin-api", "account-api", | ||
| "docker", "impersonation", "token-introspection", "passkeys"); | ||
|
|
||
| List<String> suggestions = SimilarityUtil.findSimilar("tokn-exchang", candidates, 3, SimilarityUtil.DEFAULT_MIN_SIMILARITY); | ||
|
|
||
| assertTrue("Should suggest token-exchange", suggestions.contains("token-exchange")); | ||
| assertTrue("Should return at most 3 suggestions", suggestions.size() <= 3); | ||
| assertEquals("Best match should be first", "token-exchange", suggestions.get(0)); | ||
| } | ||
|
|
||
| @Test | ||
| public void findSimilarFiltersLowSimilarity() { | ||
| List<String> candidates = List.of("token-exchange", "admin-api", "docker"); | ||
|
|
||
| List<String> suggestions = SimilarityUtil.findSimilar("zzzzzzzzz", candidates, 5, SimilarityUtil.DEFAULT_MIN_SIMILARITY); | ||
|
|
||
| assertTrue("Should return no suggestions for unrelated input", suggestions.isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| public void findSimilarCaseInsensitive() { | ||
| List<String> candidates = List.of("Token-Exchange", "admin-api"); | ||
|
|
||
| List<String> suggestions = SimilarityUtil.findSimilar("TOKEN-EXCHANGE", candidates, 3, SimilarityUtil.DEFAULT_MIN_SIMILARITY); | ||
|
|
||
| assertTrue("Should match case-insensitively", suggestions.contains("Token-Exchange")); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.