Search in sources :

Example 16 with Trie

use of org.trie4j.Trie in project trie4j by takawitter.

the class LongsConstantTimeSelect0TailLOUDSTrieWithConcatTailArrayTest method test.

@Test
public void test() throws Exception {
    String[] words = { "こんにちは", "さようなら", "おはよう", "おおきなかぶ", "おおやまざき" };
    Trie trie = new PatriciaTrie();
    for (String w : words) trie.insert(w);
    Trie lt = new TailLOUDSTrie(trie);
    //		Algorithms.dump(lt.getRoot(), new OutputStreamWriter(System.out));
    for (String w : words) {
        Assert.assertTrue(w, lt.contains(w));
    }
    Assert.assertFalse(lt.contains("おやすみなさい"));
    StringBuilder b = new StringBuilder();
    Node[] children = lt.getRoot().getChildren();
    for (Node n : children) {
        char[] letters = n.getLetters();
        b.append(letters[0]);
    }
    Assert.assertEquals("おこさ", b.toString());
}
Also used : PatriciaTrie(org.trie4j.patricia.PatriciaTrie) Node(org.trie4j.Node) PatriciaTrie(org.trie4j.patricia.PatriciaTrie) Trie(org.trie4j.Trie) Test(org.junit.Test) AbstractTermIdTrieTest(org.trie4j.AbstractTermIdTrieTest)

Example 17 with Trie

use of org.trie4j.Trie in project trie4j by takawitter.

the class TailLOUDSTrieWithConcatTailArrayTest method test.

@Test
public void test() throws Exception {
    String[] words = { "こんにちは", "さようなら", "おはよう", "おおきなかぶ", "おおやまざき" };
    Trie lt = trieWithWords(words);
    //		Algorithms.dump(lt.getRoot(), new OutputStreamWriter(System.out));
    for (String w : words) {
        Assert.assertTrue(w, lt.contains(w));
    }
    Assert.assertFalse(lt.contains("おやすみなさい"));
    StringBuilder b = new StringBuilder();
    Node[] children = lt.getRoot().getChildren();
    for (Node n : children) {
        char[] letters = n.getLetters();
        b.append(letters[0]);
    }
    Assert.assertEquals("おこさ", b.toString());
}
Also used : Node(org.trie4j.Node) Trie(org.trie4j.Trie) AbstractTermIdTrieTest(org.trie4j.AbstractTermIdTrieTest) Test(org.junit.Test)

Example 18 with Trie

use of org.trie4j.Trie in project trie4j by takawitter.

the class LongsTailLOUDSPPTrieWithSuffixTrieDenseTailArrayWikipediaSerializeTest method secondTrie.

@Override
protected Trie secondTrie(Trie firstTrie) {
    Trie t = new TailLOUDSTrie(firstTrie, new LOUDSPPBvTree(new LongsRank0OnlySuccinctBitVector(), new LongsSuccinctBitVector()), new SuffixTrieDenseTailArrayBuilder());
    t.trimToSize();
    return t;
}
Also used : LOUDSPPBvTree(org.trie4j.louds.bvtree.LOUDSPPBvTree) SuffixTrieDenseTailArrayBuilder(org.trie4j.tail.SuffixTrieDenseTailArrayBuilder) Trie(org.trie4j.Trie) LongsRank0OnlySuccinctBitVector(org.trie4j.bv.LongsRank0OnlySuccinctBitVector) LongsSuccinctBitVector(org.trie4j.bv.LongsSuccinctBitVector)

Example 19 with Trie

use of org.trie4j.Trie in project trie4j by takawitter.

the class TailLOUDSPPTrieWithConcatTailArrayTest method test_save_load.

@Test
public void test_save_load() throws Exception {
    String[] words = { "こんにちは", "さようなら", "おはよう", "おおきなかぶ", "おおやまざき" };
    Trie trie = new PatriciaTrie(words);
    TailLOUDSTrie lt = new TailLOUDSTrie(trie, new LOUDSPPBvTree(trie.nodeSize()));
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos)) {
        lt.writeExternal(oos);
        oos.flush();
        lt = new TailLOUDSTrie(new PatriciaTrie(), new LOUDSPPBvTree());
        lt.readExternal(new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())));
        for (String w : words) {
            Assert.assertTrue(lt.contains(w));
        }
        Assert.assertFalse(lt.contains("おやすみなさい"));
    }
    StringBuilder b = new StringBuilder();
    Node[] children = lt.getRoot().getChildren();
    for (Node n : children) {
        char[] letters = n.getLetters();
        b.append(letters[0]);
    }
    Assert.assertEquals("おこさ", b.toString());
}
Also used : LOUDSPPBvTree(org.trie4j.louds.bvtree.LOUDSPPBvTree) PatriciaTrie(org.trie4j.patricia.PatriciaTrie) Node(org.trie4j.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Trie(org.trie4j.Trie) PatriciaTrie(org.trie4j.patricia.PatriciaTrie) ObjectInputStream(java.io.ObjectInputStream) AbstractTermIdTrieTest(org.trie4j.AbstractTermIdTrieTest) Test(org.junit.Test)

Example 20 with Trie

use of org.trie4j.Trie in project trie4j by takawitter.

the class DoubleArrayTest method test_getNode_traverse.

@Test
public void test_getNode_traverse() throws Exception {
    Trie t = trieWithWords("hello", "helloworld", "hi", "howsgoing", "hell", "helloworld2", "world");
    StringWriter sw = new StringWriter();
    PrintWriter w = new PrintWriter(sw);
    print(t.getRoot(), 0, w);
    String expected = StreamUtil.readAsString(getClass().getResourceAsStream("DoubleArrayTest_dump_expected.txt"), "UTF-8");
    String actual = sw.toString();
    Assert.assertEquals(expected, actual);
}
Also used : StringWriter(java.io.StringWriter) Trie(org.trie4j.Trie) PrintWriter(java.io.PrintWriter) AbstractTermIdTrieTest(org.trie4j.AbstractTermIdTrieTest) Test(org.junit.Test)

Aggregations

Trie (org.trie4j.Trie)21 Test (org.junit.Test)15 PatriciaTrie (org.trie4j.patricia.PatriciaTrie)11 Node (org.trie4j.Node)10 AbstractTermIdTrieTest (org.trie4j.AbstractTermIdTrieTest)7 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 LOUDSPPBvTree (org.trie4j.louds.bvtree.LOUDSPPBvTree)5 WikipediaTitles (org.trie4j.test.WikipediaTitles)5 ObjectInputStream (java.io.ObjectInputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 SuffixTrieDenseTailArrayBuilder (org.trie4j.tail.SuffixTrieDenseTailArrayBuilder)4 LapTimer (org.trie4j.test.LapTimer)4 PrintWriter (java.io.PrintWriter)3 MapTrie (org.trie4j.MapTrie)3 MapPatriciaTrie (org.trie4j.patricia.MapPatriciaTrie)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AbstractImmutableTrieTest (org.trie4j.AbstractImmutableTrieTest)2 NodeVisitor (org.trie4j.NodeVisitor)2 LongsRank0OnlySuccinctBitVector (org.trie4j.bv.LongsRank0OnlySuccinctBitVector)2