zhaih commented on a change in pull request #225:
URL: https://github.com/apache/lucene/pull/225#discussion_r724695583



##########
File path: 
lucene/core/src/test/org/apache/lucene/util/automaton/TestNFARunAutomaton.java
##########
@@ -0,0 +1,188 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.lucene.util.automaton;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.RandomIndexWriter;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.AutomatonQuery;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.IntsRef;
+import org.apache.lucene.util.LuceneTestCase;
+import org.apache.lucene.util.TestUtil;
+
+public class TestNFARunAutomaton extends LuceneTestCase {
+
+  private static final String FIELD = "field";
+
+  public void testWithRandomRegex() {
+    for (int i = 0; i < 100; i++) {
+      RegExp regExp = null;
+      while (regExp == null) {
+        try {
+          regExp = new RegExp(AutomatonTestUtil.randomRegexp(random()));
+        } catch (IllegalArgumentException e) {
+          ignoreException(e);
+        }
+      }
+      Automaton nfa = regExp.toNFA();
+      if (nfa.isDeterministic()) {
+        i--;
+        continue;
+      }
+      Automaton dfa = regExp.toAutomaton();
+      NFARunAutomaton candidate = new NFARunAutomaton(nfa);
+      AutomatonTestUtil.RandomAcceptedStrings randomStringGen;
+      try {
+        randomStringGen = new AutomatonTestUtil.RandomAcceptedStrings(dfa);
+      } catch (IllegalArgumentException e) {
+        ignoreException(e);
+        i--;
+        continue; // sometimes the automaton accept nothing and throw this 
exception
+      }
+
+      for (int round = 0; round < 20; round++) {
+        // test order of accepted strings and random (likely rejected) strings 
alternatively to make
+        // sure caching system works correctly
+        if (random().nextBoolean()) {
+          testAcceptedString(regExp, randomStringGen, candidate, 10);
+          testRandomString(regExp, dfa, candidate, 10);
+        } else {
+          testRandomString(regExp, dfa, candidate, 10);
+          testAcceptedString(regExp, randomStringGen, candidate, 10);
+        }
+      }
+    }
+  }
+
+  public void testWithRandomAutomatonQuery() throws IOException {
+    final int n = 5;
+    for (int i = 0; i < n; i++) {
+      randomAutomatonQueryTest();
+    }
+  }
+
+  private void randomAutomatonQueryTest() throws IOException {
+    final int docNum = 50;
+    final int automatonNum = 50;
+    Directory directory = newDirectory();
+    RandomIndexWriter writer = new RandomIndexWriter(random(), directory);
+
+    Set<String> vocab = new HashSet<>();
+    Set<String> perLoopReuse = new HashSet<>();
+    for (int i = 0; i < docNum; i++) {
+      perLoopReuse.clear();
+      int termNum = random().nextInt(20) + 30;
+      while (perLoopReuse.size() < termNum) {
+        String randomString;
+        while ((randomString = 
TestUtil.randomUnicodeString(random())).length() == 0)
+          ;
+        perLoopReuse.add(randomString);
+        vocab.add(randomString);
+      }
+      Document document = new Document();
+      document.add(
+          newTextField(
+              FIELD, perLoopReuse.stream().reduce("", (s1, s2) -> s1 + " " + 
s2), Field.Store.NO));
+      writer.addDocument(document);
+    }
+    writer.commit();
+    IndexReader reader = DirectoryReader.open(directory);
+    IndexSearcher searcher = new IndexSearcher(reader);
+
+    Set<String> foreignVocab = new HashSet<>();
+    while (foreignVocab.size() < vocab.size()) {
+      String randomString;
+      while ((randomString = TestUtil.randomUnicodeString(random())).length() 
== 0)
+        ;
+      foreignVocab.add(randomString);
+    }
+
+    ArrayList<String> vocabList = new ArrayList<>(vocab);
+    ArrayList<String> foreignVocabList = new ArrayList<>(foreignVocab);
+
+    for (int i = 0; i < automatonNum; i++) {
+      perLoopReuse.clear();
+      int termNum = random().nextInt(40) + 30;
+      while (perLoopReuse.size() < termNum) {
+        if (random().nextBoolean()) {
+          perLoopReuse.add(vocabList.get(random().nextInt(vocabList.size())));
+        } else {
+          
perLoopReuse.add(foreignVocabList.get(random().nextInt(foreignVocabList.size())));
+        }
+      }
+      Automaton a = null;
+      for (String term : perLoopReuse) {
+        if (a == null) {
+          a = Automata.makeString(term);
+        } else {
+          a = Operations.union(a, Automata.makeString(term));
+        }
+      }
+      if (a.isDeterministic()) {
+        i--;
+        continue;
+      }
+      AutomatonQuery dfaQuery = new AutomatonQuery(new Term(FIELD), a);
+      AutomatonQuery nfaQuery = new AutomatonQuery(new Term(FIELD), a, 
ByteRunnable.TYPE.NFA);

Review comment:
       Good idea! Let's delay it a bit to the next PR maybe (I plan to 
introduce NFARegexQuery in next PR probably).




-- 
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: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to