Search in sources :

Example 11 with PatriciaTrie

use of org.trie4j.patricia.PatriciaTrie 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)

Aggregations

PatriciaTrie (org.trie4j.patricia.PatriciaTrie)11 Trie (org.trie4j.Trie)10 Test (org.junit.Test)8 Node (org.trie4j.Node)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 AbstractTermIdTrieTest (org.trie4j.AbstractTermIdTrieTest)4 ObjectInputStream (java.io.ObjectInputStream)3 ObjectOutputStream (java.io.ObjectOutputStream)3 MapTrie (org.trie4j.MapTrie)3 MapPatriciaTrie (org.trie4j.patricia.MapPatriciaTrie)3 LapTimer (org.trie4j.test.LapTimer)3 WikipediaTitles (org.trie4j.test.WikipediaTitles)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 NodeVisitor (org.trie4j.NodeVisitor)2 MapTailDoubleArray (org.trie4j.doublearray.MapTailDoubleArray)2 TailDoubleArray (org.trie4j.doublearray.TailDoubleArray)2 LOUDSPPBvTree (org.trie4j.louds.bvtree.LOUDSPPBvTree)2 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1