donnerpeter commented on code in PR #975:
URL: https://github.com/apache/lucene/pull/975#discussion_r906637155


##########
lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/WordFormGenerator.java:
##########
@@ -245,6 +240,31 @@ private LinkedHashSet<Character> appendFlags(AffixEntry 
affix) {
     return appendId <= 0 ? new LinkedHashSet<>() : 
toSet(dictionary.flagLookup.getFlags(appendId));
   }
 
+  /**
+   * Given a list of words, try to produce a smaller set of dictionary entries 
(with some flags)

Review Comment:
   And quite likely NP-complete as well :) I've come up with some approximate 
greedy algorithm that seems to work for my cases, but isn't ideal.



##########
lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/WordFormGenerator.java:
##########
@@ -280,4 +300,188 @@ private boolean 
isCompatibleWithPreviousAffixes(AffixedWord stem, Dictionary dic
       return true;
     }
   }
+
+  private class WordCompressor {
+    private final Set<String> forbidden;
+    private final Runnable checkCanceled;
+    private final Set<String> wordSet;
+    private final Set<String> existingStems;
+    private final Map<String, Set<FlagSet>> stemToPossibleFlags = new 
HashMap<>();
+    private final Map<String, Integer> stemCounts = new LinkedHashMap<>();
+
+    WordCompressor(List<String> words, Set<String> forbidden, Runnable 
checkCanceled) {
+      this.forbidden = forbidden;
+      this.checkCanceled = checkCanceled;
+      wordSet = new HashSet<>(words);
+
+      Stemmer.StemCandidateProcessor processor =
+          new Stemmer.StemCandidateProcessor(WordContext.SIMPLE_WORD) {
+            @Override
+            boolean processStemCandidate(
+                char[] word,
+                int offset,
+                int length,
+                int lastAffix,
+                int outerPrefix,
+                int innerPrefix,
+                int outerSuffix,
+                int innerSuffix) {
+              String candidate = new String(word, offset, length);
+              stemCounts.merge(candidate, 1, Integer::sum);
+              Set<Character> flags = new LinkedHashSet<>();
+              if (outerPrefix >= 0) 
flags.add(dictionary.affixData(outerPrefix, AFFIX_FLAG));
+              if (innerPrefix >= 0) 
flags.add(dictionary.affixData(innerPrefix, AFFIX_FLAG));
+              if (outerSuffix >= 0) 
flags.add(dictionary.affixData(outerSuffix, AFFIX_FLAG));
+              if (innerSuffix >= 0) 
flags.add(dictionary.affixData(innerSuffix, AFFIX_FLAG));
+              stemToPossibleFlags
+                  .computeIfAbsent(candidate, __ -> new LinkedHashSet<>())
+                  .add(new FlagSet(flags, dictionary));
+              return true;
+            }
+          };
+
+      for (String word : words) {
+        checkCanceled.run();
+        stemCounts.merge(word, 1, Integer::sum);
+        stemToPossibleFlags.computeIfAbsent(word, __ -> new LinkedHashSet<>());

Review Comment:
   Same as above



-- 
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.

To unsubscribe, e-mail: [email protected]

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