RegexToGramQueryTranslator { public static GramBooleanQuery translate(String regex) throws com.google.re2j.PatternSyntaxException { return translate(regex, TranslatorUtils.DEFAULT_GRAM_LENGTH); }  static GramBooleanQuery translate(String regex); static GramBooleanQuery translate(String regex, int gramLength);  }
@Test public void testEmptyRegex() { String regex = ""; GramBooleanQuery exactQuery = RegexToGramQueryTranslator.translate(regex); GramBooleanQuery expectedQuery = new GramBooleanQuery(GramBooleanQuery.QueryOp.OR); printTranslatorResult(regex); Assert.assertEquals(expectedQuery, exactQuery); Assert.assertEquals(exactQuery, expectedQuery); }
@Test public void testStarRegex() { String regex = "a*"; GramBooleanQuery exactQuery = RegexToGramQueryTranslator.translate(regex); GramBooleanQuery expectedQuery = new GramBooleanQuery(GramBooleanQuery.QueryOp.OR); printTranslatorResult(regex); Assert.assertEquals(expectedQuery, exactQuery); Assert.assertEquals(exactQuery, expectedQuery); }
@Test public void testLiteral1() { String regex = "abc"; GramBooleanQuery exactQuery = RegexToGramQueryTranslator.translate(regex); GramBooleanQuery expectedQuery = GramBooleanQuery.newLeafNode("abc"); printTranslatorResult(regex); Assert.assertEquals(expectedQuery, exactQuery); Assert.assertEquals(exactQuery, expectedQuery); }
@Test public void testLiteral2() { String regex = "ab"; GramBooleanQuery exactQuery = RegexToGramQueryTranslator.translate(regex); GramBooleanQuery expectedQuery = new GramBooleanQuery(GramBooleanQuery.QueryOp.OR); printTranslatorResult(regex); Assert.assertEquals(expectedQuery, exactQuery); Assert.assertEquals(exactQuery, expectedQuery); }
@Test public void testLiteral3() { String regex = "abcd"; GramBooleanQuery exactQuery = RegexToGramQueryTranslator.translate(regex); GramBooleanQuery expectedQuery = new GramBooleanQuery(GramBooleanQuery.QueryOp.AND); expectedQuery.subQuerySet.addAll(getLeafNodeList("abc", "bcd")); printTranslatorResult(regex); Assert.assertEquals(expectedQuery, exactQuery); Assert.assertEquals(exactQuery, expectedQuery); }
@Test public void testLiteral4() { String regex = "ucirvine"; GramBooleanQuery exactQuery = RegexToGramQueryTranslator.translate(regex); GramBooleanQuery expectedQuery = new GramBooleanQuery(GramBooleanQuery.QueryOp.AND); expectedQuery.subQuerySet.addAll(getLeafNodeList("uci", "cir", "irv", "rvi", "vin", "ine")); printTranslatorResult(regex); Assert.assertEquals(expectedQuery, exactQuery); Assert.assertEquals(exactQuery, expectedQuery); }
@Test public void testLiteral5() { String regex = "texera"; GramBooleanQuery exactQuery = RegexToGramQueryTranslator.translate(regex); GramBooleanQuery expectedQuery = new GramBooleanQuery(GramBooleanQuery.QueryOp.AND); expectedQuery.subQuerySet.addAll(getLeafNodeList("tex", "exe", "xer", "era")); printTranslatorResult(regex); Assert.assertEquals(expectedQuery, exactQuery); Assert.assertEquals(exactQuery, expectedQuery); }
@Test public void testCharClass1() { String regex = "[a-b][c-d][e-f]"; GramBooleanQuery exactQuery = RegexToGramQueryTranslator.translate(regex); GramBooleanQuery expectedQuery = new GramBooleanQuery(GramBooleanQuery.QueryOp.OR); expectedQuery.subQuerySet.addAll(getLeafNodeList("ace", "acf", "bce", "bcf", "ade", "adf", "bde", "bdf")); printTranslatorResult(regex); Assert.assertEquals(expectedQuery, exactQuery); Assert.assertEquals(exactQuery, expectedQuery); }
@Test public void testAlternate1() { String regex = "uci|ics"; GramBooleanQuery exactQuery = RegexToGramQueryTranslator.translate(regex); GramBooleanQuery expectedQuery = new GramBooleanQuery(GramBooleanQuery.QueryOp.OR); expectedQuery.subQuerySet.addAll(getLeafNodeList("uci", "ics")); printTranslatorResult(regex); Assert.assertEquals(expectedQuery, exactQuery); Assert.assertEquals(exactQuery, expectedQuery); }
@Test public void testAlternate2() { String regex = "data*(bcd|pqr)"; GramBooleanQuery exactQuery = RegexToGramQueryTranslator.translate(regex); GramBooleanQuery expectedQuery = new GramBooleanQuery(GramBooleanQuery.QueryOp.OR); GramBooleanQuery expectedAnd1 = new GramBooleanQuery(GramBooleanQuery.QueryOp.AND); expectedQuery.subQuerySet.add(expectedAnd1); expectedAnd1.subQuerySet.addAll(getLeafNodeList("dat", "pqr")); GramBooleanQuery expectedAnd2 = new GramBooleanQuery(GramBooleanQuery.QueryOp.AND); expectedQuery.subQuerySet.add(expectedAnd2); expectedAnd2.subQuerySet.addAll(getLeafNodeList("dat", "bcd")); printTranslatorResult(regex); Assert.assertEquals(expectedQuery, exactQuery); Assert.assertEquals(exactQuery, expectedQuery); }
@Test public void testPlus1() { String regex = "abc+"; GramBooleanQuery exactQuery = RegexToGramQueryTranslator.translate(regex); GramBooleanQuery expectedQuery = GramBooleanQuery.newLeafNode("abc"); printTranslatorResult(regex); Assert.assertEquals(expectedQuery, exactQuery); Assert.assertEquals(exactQuery, expectedQuery); }
@Test public void testPlus2() { String regex = "abc+pqr+"; GramBooleanQuery exactQuery = RegexToGramQueryTranslator.translate(regex); GramBooleanQuery expectedQuery = new GramBooleanQuery(GramBooleanQuery.QueryOp.AND); expectedQuery.subQuerySet.addAll(getLeafNodeList("abc", "cpq", "pqr")); printTranslatorResult(regex); Assert.assertEquals(expectedQuery, exactQuery); Assert.assertEquals(exactQuery, expectedQuery); }
@Test public void testQuest1() { String regex = "abc?"; GramBooleanQuery exactQuery = RegexToGramQueryTranslator.translate(regex); GramBooleanQuery expectedQuery = new GramBooleanQuery(GramBooleanQuery.QueryOp.OR); printTranslatorResult(regex); Assert.assertEquals(expectedQuery, exactQuery); Assert.assertEquals(exactQuery, expectedQuery); }
@Test public void testQuest2() { String regex = "abc?pqr?"; GramBooleanQuery exactQuery = RegexToGramQueryTranslator.translate(regex); GramBooleanQuery expectedQuery = new GramBooleanQuery(GramBooleanQuery.QueryOp.OR); GramBooleanQuery expectedFirstAnd = new GramBooleanQuery(GramBooleanQuery.QueryOp.AND); expectedQuery.subQuerySet.add(expectedFirstAnd); expectedFirstAnd.subQuerySet.addAll(getLeafNodeList("abp", "bpq")); GramBooleanQuery expectedSecondAnd = new GramBooleanQuery(GramBooleanQuery.QueryOp.AND); expectedQuery.subQuerySet.add(expectedSecondAnd); expectedSecondAnd.subQuerySet.addAll(getLeafNodeList("abc", "bcp", "cpq")); printTranslatorResult(regex); Assert.assertEquals(expectedQuery, exactQuery); Assert.assertEquals(exactQuery, expectedQuery); }
@Test public void testRepeat1() { String regex = "abc{1,3}"; GramBooleanQuery exactQuery = RegexToGramQueryTranslator.translate(regex); GramBooleanQuery expectedQuery = GramBooleanQuery.newLeafNode("abc"); printTranslatorResult(regex); Assert.assertEquals(expectedQuery, exactQuery); Assert.assertEquals(exactQuery, expectedQuery); }
@Test public void testCapture1() { String regex = "(abc)(qwer)"; GramBooleanQuery exactQuery = RegexToGramQueryTranslator.translate(regex); GramBooleanQuery expectedQuery = new GramBooleanQuery(GramBooleanQuery.QueryOp.AND); expectedQuery.subQuerySet.addAll(getLeafNodeList("abc", "bcq", "cqw", "qwe", "wer")); Assert.assertEquals(expectedQuery, exactQuery); Assert.assertEquals(exactQuery, expectedQuery); }
@Test public void testRegexCropUrl() { String regex = "^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$"; GramBooleanQuery exactQuery = RegexToGramQueryTranslator.translate(regex); GramBooleanQuery expectedQuery = new GramBooleanQuery(GramBooleanQuery.QueryOp.OR); printTranslatorResult(regex); Assert.assertEquals(expectedQuery, exactQuery); Assert.assertEquals(exactQuery, expectedQuery); }