Search in sources :

Example 1 with PatriciaTrie

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

the class TestWikipedia method main.

public static void main(String[] args) throws Exception {
    System.out.println("--- building patricia trie ---");
    Trie trie = new PatriciaTrie();
    //		Trie trie = new TailPatriciaTrie(new ConcatTailBuilder());
    int c = 0;
    LapTimer t1 = new LapTimer();
    for (String word : new WikipediaTitles()) {
        trie.insert(word);
        c++;
        if (c == maxCount)
            break;
    }
    System.out.println("done in " + t1.lapMillis() + " millis.");
    System.out.println(c + "entries in ja wikipedia titles.");
    System.out.println("-- building double array.");
    t1.reset();
    //		Trie da = new TailDoubleArray(trie, 65536, new ConcatTailBuilder());
    //		Trie da = new DoubleArray(trie, 65536);
    Trie da = trie;
    trie = null;
    System.out.println("done in " + t1.lapMillis() + " millis.");
    final AtomicInteger count = new AtomicInteger();
    Algorithms.traverseByBreadth(da.getRoot(), new NodeVisitor() {

        @Override
        public boolean visit(Node node, int nest) {
            count.incrementAndGet();
            return true;
        }
    });
    System.out.println(count + " nodes in trie.");
    da.dump(new PrintWriter(System.out));
    verify(da);
    System.out.println("---- common prefix search ----");
    System.out.println("-- for 東京国際フォーラム");
    for (String s : da.commonPrefixSearch("東京国際フォーラム")) {
        System.out.println(s);
    }
    System.out.println("-- for 大阪城ホール");
    for (String s : da.commonPrefixSearch("大阪城ホール")) {
        System.out.println(s);
    }
    System.out.println("---- predictive search ----");
    System.out.println("-- for 大阪城");
    for (String s : da.predictiveSearch("大阪城")) {
        System.out.println(s);
    }
    System.out.println("---- done ----");
    Thread.sleep(10000);
    da.contains("hello");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PatriciaTrie(org.trie4j.patricia.PatriciaTrie) Node(org.trie4j.Node) WikipediaTitles(org.trie4j.test.WikipediaTitles) Trie(org.trie4j.Trie) PatriciaTrie(org.trie4j.patricia.PatriciaTrie) LapTimer(org.trie4j.test.LapTimer) NodeVisitor(org.trie4j.NodeVisitor) PrintWriter(java.io.PrintWriter)

Example 2 with PatriciaTrie

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

the class TrieWriterTest method test.

@Test
public void test() throws Exception {
    LapTimer lt = new LapTimer();
    PatriciaTrie origTrie = new PatriciaTrie();
    new WikipediaTitles().insertTo(origTrie);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    TrieWriter tw = new TrieWriter(baos);
    Trie trie = new TailLOUDSTrie(origTrie, new LOUDSPPBvTree(origTrie.nodeSize()), new SuffixTrieDenseTailArrayBuilder());
    lt.reset();
    tw.write(trie);
    tw.flush();
    lt.lapMillis("trie saved.");
    System.out.println(baos.size() + " bytes");
    TrieReader tr = new TrieReader(new ByteArrayInputStream(baos.toByteArray()));
    lt.reset();
    Trie trie2 = tr.read();
    lt.lapMillis("trie loaded.");
    long d = new WikipediaTitles().assertAllContains(trie2);
    System.out.println("[" + d + "ms]: verified");
}
Also used : TailLOUDSTrie(org.trie4j.louds.TailLOUDSTrie) LOUDSPPBvTree(org.trie4j.louds.bvtree.LOUDSPPBvTree) ByteArrayInputStream(java.io.ByteArrayInputStream) PatriciaTrie(org.trie4j.patricia.PatriciaTrie) SuffixTrieDenseTailArrayBuilder(org.trie4j.tail.SuffixTrieDenseTailArrayBuilder) WikipediaTitles(org.trie4j.test.WikipediaTitles) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Trie(org.trie4j.Trie) PatriciaTrie(org.trie4j.patricia.PatriciaTrie) TailLOUDSTrie(org.trie4j.louds.TailLOUDSTrie) LapTimer(org.trie4j.test.LapTimer) Test(org.junit.Test)

Example 3 with PatriciaTrie

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

the class LongsConstantTimeSelect0TailLOUDSTrieWithConcatTailArrayTest method test_save_load.

@Test
public void test_save_load() 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);
    lt.writeExternal(oos);
    oos.flush();
    lt = new TailLOUDSTrie();
    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 : PatriciaTrie(org.trie4j.patricia.PatriciaTrie) Node(org.trie4j.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) PatriciaTrie(org.trie4j.patricia.PatriciaTrie) Trie(org.trie4j.Trie) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) AbstractTermIdTrieTest(org.trie4j.AbstractTermIdTrieTest)

Example 4 with PatriciaTrie

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

the class Issue_031 method test_TailDoubleArray.

@Test
public void test_TailDoubleArray() throws Throwable {
    Trie trie = new PatriciaTrie();
    insertLines(trie, FILE_NAME);
    Trie da = new TailDoubleArray(trie);
    Assert.assertFalse(trie.contains("you"));
    Assert.assertFalse(da.contains("you"));
}
Also used : MapTailDoubleArray(org.trie4j.doublearray.MapTailDoubleArray) TailDoubleArray(org.trie4j.doublearray.TailDoubleArray) 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 5 with PatriciaTrie

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

the class Issue_031 method test_DoubleArray.

@Test
public void test_DoubleArray() throws Throwable {
    Trie trie = new PatriciaTrie();
    insertLines(trie, FILE_NAME);
    Trie da = new DoubleArray(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) MapTailDoubleArray(org.trie4j.doublearray.MapTailDoubleArray) TailDoubleArray(org.trie4j.doublearray.TailDoubleArray) MapDoubleArray(org.trie4j.doublearray.MapDoubleArray) DoubleArray(org.trie4j.doublearray.DoubleArray) 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