donnerpeter commented on a change in pull request #2416:
URL: https://github.com/apache/lucene-solr/pull/2416#discussion_r580529040



##########
File path: 
lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/GeneratingSuggester.java
##########
@@ -175,67 +175,85 @@ private static int calcThreshold(String word) {
     }
 
     // suffixes
-    processFST(
-        dictionary.suffixes,
-        (key, ids) -> {
-          String suffix = new 
StringBuilder(toString(key)).reverse().toString();
-          if (misspelled.length() <= suffix.length() || 
!misspelled.endsWith(suffix)) return;
-
-          for (int i = 0; i < ids.length; i++) {
-            int suffixId = ids.ints[ids.offset + i];
-            if (!hasCompatibleFlags(root, suffixId) || 
!checkAffixCondition(suffixId, root.word)) {
-              continue;
-            }
+    processAffixes(
+        false,
+        misspelled,
+        (suffixLength, suffixId) -> {
+          if (!hasCompatibleFlags(root, suffixId) || 
!checkAffixCondition(suffixId, root.word)) {
+            return;
+          }
 
-            String withSuffix =
-                root.word.substring(0, root.word.length() - 
affixStripLength(suffixId)) + suffix;
-            result.add(withSuffix);
-            if (dictionary.isCrossProduct(suffixId)) {
-              crossProducts.add(withSuffix);
-            }
+          String suffix = misspelled.substring(misspelled.length() - 
suffixLength);
+          String withSuffix =
+              root.word.substring(0, root.word.length() - 
affixStripLength(suffixId)) + suffix;
+          result.add(withSuffix);
+          if (dictionary.isCrossProduct(suffixId)) {
+            crossProducts.add(withSuffix);
           }
         });
 
     // cross-product prefixes
-    processFST(
-        dictionary.prefixes,
-        (key, ids) -> {
-          String prefix = toString(key);
-          if (misspelled.length() <= prefix.length() || 
!misspelled.startsWith(prefix)) return;
-
-          for (int i = 0; i < ids.length; i++) {
-            int prefixId = ids.ints[ids.offset + i];
-            if (!dictionary.hasFlag(root.entryId, 
dictionary.affixData(prefixId, AFFIX_FLAG))
-                || !dictionary.isCrossProduct(prefixId)) {
-              continue;
-            }
+    processAffixes(
+        true,
+        misspelled,
+        (prefixLength, prefixId) -> {
+          if (!dictionary.hasFlag(root.entryId, dictionary.affixData(prefixId, 
AFFIX_FLAG))
+              || !dictionary.isCrossProduct(prefixId)) {
+            return;
+          }
 
-            for (String suffixed : crossProducts) {
-              if (checkAffixCondition(prefixId, suffixed)) {
-                result.add(prefix + 
suffixed.substring(affixStripLength(prefixId)));
-              }
+          String prefix = misspelled.substring(0, prefixLength);
+          for (String suffixed : crossProducts) {
+            if (checkAffixCondition(prefixId, suffixed)) {
+              result.add(prefix + 
suffixed.substring(affixStripLength(prefixId)));
             }
           }
         });
 
     // pure prefixes
-    processFST(
-        dictionary.prefixes,
-        (key, ids) -> {
-          String prefix = toString(key);
-          if (misspelled.length() <= prefix.length() || 
!misspelled.startsWith(prefix)) return;
-
-          for (int i = 0; i < ids.length; i++) {
-            int prefixId = ids.ints[ids.offset + i];
-            if (hasCompatibleFlags(root, prefixId) && 
checkAffixCondition(prefixId, root.word)) {
-              result.add(prefix + 
root.word.substring(affixStripLength(prefixId)));
-            }
+    processAffixes(
+        true,
+        misspelled,
+        (prefixLength, prefixId) -> {
+          if (hasCompatibleFlags(root, prefixId) && 
checkAffixCondition(prefixId, root.word)) {
+            String prefix = misspelled.substring(0, prefixLength);
+            result.add(prefix + 
root.word.substring(affixStripLength(prefixId)));
           }
         });
 
     return result.stream().limit(MAX_WORDS).collect(Collectors.toList());
   }
 
+  private void processAffixes(boolean prefixes, String word, AffixProcessor 
processor) {

Review comment:
       This code is quite similar to `Stemmer.stem`, but reusing it isn't 
trivial. My best attempts gave a slowdown of 5%, probably due to lambda 
allocation.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to