Search in sources :

Example 6 with PatriciaTrie

use of org.trie4j.patricia.PatriciaTrie in project trie4j by takawitter.

the class TailLOUDSTrieWithSuffixTrieTailArrayTest method test_save_load2.

@Test
public void test_save_load2() throws Exception {
    String[] words = { "こんにちは", "さようなら", "おはよう", "おおきなかぶ", "おおやまざき" };
    Trie trie = new PatriciaTrie();
    for (String w : words) trie.insert(w);
    TailLOUDSTrie lt = new TailLOUDSTrie(trie);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(lt);
    oos.flush();
    lt = (TailLOUDSTrie) new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())).readObject();
    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 : 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 7 with PatriciaTrie

use of org.trie4j.patricia.PatriciaTrie in project trie4j by takawitter.

the class BitVectorExp method main.

public static void main(String[] args) throws Exception {
    Trie trie = new PatriciaTrie();
    int c = 0;
    // You can download archive from http://dumps.wikimedia.org/jawiki/latest/
    LapTimer t = new LapTimer();
    for (String word : new WikipediaTitles()) {
        trie.insert(word);
        c++;
        if (c == maxCount)
            break;
    }
    t.lapMillis("trie building done. %d words.", c);
    final BytesSuccinctBitVector bv = new BytesSuccinctBitVector(5000000);
    final AtomicInteger nodeCount = new AtomicInteger();
    Algorithms.traverseByDepth(trie.getRoot(), new NodeVisitor() {

        @Override
        public boolean visit(Node node, int nest) {
            Node[] children = node.getChildren();
            if (children != null) {
                int n = node.getChildren().length;
                for (int i = 0; i < n; i++) {
                    bv.append(true);
                }
            }
            bv.append(false);
            nodeCount.incrementAndGet();
            return true;
        }
    });
    trie = null;
    t.lapMillis("done. %d nodes inserted. do rank and select", nodeCount.intValue());
    for (int i = 0; i < c; i += 100) {
        int count = bv.rank(i, true);
        bv.select(count, true);
    }
    t.lapMillis("done.");
    Thread.sleep(10000);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PatriciaTrie(org.trie4j.patricia.PatriciaTrie) Node(org.trie4j.Node) BytesSuccinctBitVector(org.trie4j.bv.BytesSuccinctBitVector) WikipediaTitles(org.trie4j.test.WikipediaTitles) Trie(org.trie4j.Trie) PatriciaTrie(org.trie4j.patricia.PatriciaTrie) LapTimer(org.trie4j.test.LapTimer) NodeVisitor(org.trie4j.NodeVisitor)

Example 8 with PatriciaTrie

use of org.trie4j.patricia.PatriciaTrie in project trie4j by takawitter.

the class Issue_031 method test_UnsafeDoubleArray.

@Test
public void test_UnsafeDoubleArray() throws Throwable {
    Trie trie = new PatriciaTrie();
    insertLines(trie, FILE_NAME);
    @SuppressWarnings("deprecation") Trie da = new org.trie4j.doublearray.UnsafeDoubleArray(trie);
    Assert.assertFalse(trie.contains("you"));
    Assert.assertFalse(da.contains("you"));
}
Also used : PatriciaTrie(org.trie4j.patricia.PatriciaTrie) MapPatriciaTrie(org.trie4j.patricia.MapPatriciaTrie) MapTrie(org.trie4j.MapTrie) PatriciaTrie(org.trie4j.patricia.PatriciaTrie) Trie(org.trie4j.Trie) MapPatriciaTrie(org.trie4j.patricia.MapPatriciaTrie) Test(org.junit.Test)

Example 9 with PatriciaTrie

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

use of org.trie4j.patricia.PatriciaTrie in project trie4j by takawitter.

the class TestWikipediaCPS method main.

public static void main(String[] args) throws Exception {
    System.out.println("--- recursive patricia trie ---");
    Trie trie = new PatriciaTrie();
    int c = 0;
    BufferedReader r = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream("jawiki-20120220-all-titles-in-ns0.gz")), CharsetUtil.newUTF8Decoder()));
    String word = null;
    long sum = 0;
    long lap = System.currentTimeMillis();
    while ((word = r.readLine()) != null) {
        long d = System.currentTimeMillis();
        trie.insert(word);
        sum += System.currentTimeMillis() - d;
        if (c % 100000 == 0) {
            d = System.currentTimeMillis() - lap;
            long free = Runtime.getRuntime().freeMemory();
            System.out.println(c + "," + free + "," + Runtime.getRuntime().maxMemory() + "," + d);
            lap = System.currentTimeMillis();
        }
        c++;
        if (c == maxCount)
            break;
    }
    System.out.println(c + "entries in ja wikipedia titles.");
    System.out.println("insert time: " + sum + " millis.");
    System.out.println("-- insert done.");
    System.out.println(Runtime.getRuntime().freeMemory() + " bytes free.");
    doSearches(trie);
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) InputStreamReader(java.io.InputStreamReader) PatriciaTrie(org.trie4j.patricia.PatriciaTrie) BufferedReader(java.io.BufferedReader) PatriciaTrie(org.trie4j.patricia.PatriciaTrie) FileInputStream(java.io.FileInputStream)

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