You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2022/06/30 11:51:07 UTC

[camel] branch main updated: (chores) camel-catalog-lucene / camel-kamelet-main: remove duplicated code for suggestions

This is an automated email from the ASF dual-hosted git repository.

orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 6871aa31240 (chores) camel-catalog-lucene / camel-kamelet-main: remove duplicated code for suggestions
6871aa31240 is described below

commit 6871aa3124060cc9cd933590e832abdd19525424
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Thu Jun 30 12:45:05 2022 +0200

    (chores) camel-catalog-lucene / camel-kamelet-main: remove duplicated code for suggestions
---
 .../catalog/lucene/LuceneSuggestionStrategy.java   |  5 ++++
 .../camel/main/util/SuggestSimilarHelper.java      | 31 +++-------------------
 2 files changed, 9 insertions(+), 27 deletions(-)

diff --git a/catalog/camel-catalog-lucene/src/main/java/org/apache/camel/catalog/lucene/LuceneSuggestionStrategy.java b/catalog/camel-catalog-lucene/src/main/java/org/apache/camel/catalog/lucene/LuceneSuggestionStrategy.java
index 284ed21be16..55ffd816c1f 100644
--- a/catalog/camel-catalog-lucene/src/main/java/org/apache/camel/catalog/lucene/LuceneSuggestionStrategy.java
+++ b/catalog/camel-catalog-lucene/src/main/java/org/apache/camel/catalog/lucene/LuceneSuggestionStrategy.java
@@ -17,6 +17,7 @@
 package org.apache.camel.catalog.lucene;
 
 import java.io.StringReader;
+import java.util.Collection;
 import java.util.Set;
 
 import org.apache.camel.catalog.SuggestionStrategy;
@@ -37,6 +38,10 @@ public class LuceneSuggestionStrategy implements SuggestionStrategy {
 
     @Override
     public String[] suggestEndpointOptions(Set<String> names, String unknownOption) {
+        return suggestEndpointOptions(names, unknownOption, maxSuggestions);
+    }
+
+    public static String[] suggestEndpointOptions(Collection<String> names, String unknownOption, int maxSuggestions) {
         // each option must be on a separate line in a String
         StringBuilder sb = new StringBuilder();
         for (String name : names) {
diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/util/SuggestSimilarHelper.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/util/SuggestSimilarHelper.java
index 921ef8c0975..f4f12d77fb2 100644
--- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/util/SuggestSimilarHelper.java
+++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/util/SuggestSimilarHelper.java
@@ -16,15 +16,10 @@
  */
 package org.apache.camel.main.util;
 
-import java.io.StringReader;
 import java.util.Arrays;
 import java.util.List;
 
-import org.apache.lucene.analysis.core.KeywordAnalyzer;
-import org.apache.lucene.index.IndexWriterConfig;
-import org.apache.lucene.search.spell.PlainTextDictionary;
-import org.apache.lucene.search.spell.SpellChecker;
-import org.apache.lucene.store.ByteBuffersDirectory;
+import org.apache.camel.catalog.lucene.LuceneSuggestionStrategy;
 
 public final class SuggestSimilarHelper {
 
@@ -34,27 +29,9 @@ public final class SuggestSimilarHelper {
     }
 
     public static List<String> didYouMean(List<String> names, String unknown) {
-        // each option must be on a separate line in a String
-        StringBuilder sb = new StringBuilder();
-        for (String name : names) {
-            sb.append(name);
-            sb.append("\n");
-        }
-        StringReader reader = new StringReader(sb.toString());
-
-        try {
-            PlainTextDictionary words = new PlainTextDictionary(reader);
-
-            // use in-memory lucene spell checker to make the suggestions
-            try (ByteBuffersDirectory dir = new ByteBuffersDirectory()) {
-                SpellChecker checker = new SpellChecker(dir);
-                checker.indexDictionary(words, new IndexWriterConfig(new KeywordAnalyzer()), false);
-
-                String[] suggestions = checker.suggestSimilar(unknown, MAX_SUGGESTIONS);
-                return Arrays.asList(suggestions);
-            }
-        } catch (Exception e) {
-            // ignore
+        String[] suggestions = LuceneSuggestionStrategy.suggestEndpointOptions(names, unknown, MAX_SUGGESTIONS);
+        if (suggestions != null) {
+            return Arrays.asList(suggestions);
         }
 
         return null;