Search in sources :

Example 1 with NodeVisitor

use of org.trie4j.NodeVisitor 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 NodeVisitor

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

the class LabelNodeTest method main.

public static void main(String[] args) throws Exception {
    String[] words = { /*
				"apple", "appear", "a", "orange"
				, "applejuice", "appletea", "appleshower"
				, "orangejuice"
/*/
    "page_title", "!", "!!", "!!!", "!!!Fuck_You!!!", "!?", "!LOUD!", "!SHOUT!", "!_-attention-", "!wagero!", "\"", "\"74ers\"_LIVE_IN_OSAKA-JO_HALL_2003" };
    Map<String, LabelNode> nodes = new HashMap<String, LabelNode>();
    LabelNode root = new LabelNode(new char[] {});
    for (String w : words) {
        System.out.println("--insert [" + w + "]--");
        nodes.put(w, root.insertChild(0, CharsUtil.revert(w.toCharArray()), 0));
        System.out.println("--dump--");
        root.visit(new NodeVisitor() {

            @Override
            public boolean visit(Node node, int nest) {
                for (int i = 0; i < nest; i++) {
                    System.out.print(" ");
                }
                if (node.getLetters().length > 0) {
                    System.out.print(node.getLetters());
                } else {
                    System.out.print("<empty>");
                }
                System.out.println();
                return true;
            }
        }, 0);
    }
    for (String w : words) {
        System.out.print(w + ": ");
        LabelNode node = nodes.get(w);
        while (node != null) {
            System.out.print(CharsUtil.revert(node.getLetters()));
            System.out.print(" ");
            node = node.getParent();
        }
        System.out.println();
    }
    char[][] charss = { { '!', '!', (char) -1 }, { '!', (char) -1 }, { 'p', 'a', 'g', 'e', '_', 't', 'i', 't', 'l', 'e', (char) -1 } };
    for (char[] c : charss) {
        System.out.println("--insert [" + new String(c) + "]--");
        LabelNode n = root.insertChild(0, CharsUtil.revert(c), 0);
        Algorithms.dump(root, new OutputStreamWriter(System.out));
        System.out.println("--containsBottomup: " + n.containsBottomup(Arrays.copyOf(c, c.length - 1), 0));
    }
}
Also used : HashMap(java.util.HashMap) Node(org.trie4j.Node) OutputStreamWriter(java.io.OutputStreamWriter) NodeVisitor(org.trie4j.NodeVisitor)

Example 3 with NodeVisitor

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

the class TailLOUDSTrieWithSuffixTrieTailArrayWikipediaTest method afterVerification.

@Override
protected void afterVerification(Trie trie) throws Exception {
    TailLOUDSTrie t = (TailLOUDSTrie) trie;
    final Map<Integer, List<Integer>> childrenCounts = new TreeMap<Integer, List<Integer>>(new Comparator<Integer>() {

        @Override
        public int compare(Integer o1, Integer o2) {
            return o2 - o1;
        }
    });
    Algorithms.traverseByBreadth(t.getRoot(), new NodeVisitor() {

        @Override
        public boolean visit(Node node, int nest) {
            int n = node.getChildren().length;
            List<Integer> nodes = childrenCounts.get(n);
            if (nodes == null) {
                nodes = new ArrayList<Integer>();
                childrenCounts.put(n, nodes);
            }
            nodes.add(c++);
            return c < 6189;
        }

        int c = 0;
    });
    for (Map.Entry<Integer, List<Integer>> entry : childrenCounts.entrySet()) {
        System.out.println(entry.getKey() + ": " + entry.getValue());
    }
}
Also used : Node(org.trie4j.Node) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) NodeVisitor(org.trie4j.NodeVisitor) ArrayList(java.util.ArrayList) List(java.util.List) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 4 with NodeVisitor

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

the class MapPatriciaTrieWikipediaTest method afterVerification.

@Override
protected void afterVerification(Trie trie) throws Exception {
    final AtomicInteger nodes = new AtomicInteger();
    final AtomicInteger leaves = new AtomicInteger();
    Algorithms.traverseByDepth(trie.getRoot(), new NodeVisitor() {

        @Override
        public boolean visit(Node node, int nest) {
            if (node.isTerminate())
                leaves.incrementAndGet();
            else
                nodes.incrementAndGet();
            return true;
        }
    });
    System.out.println(String.format("%d nodes and %d leaves", nodes.intValue(), leaves.intValue()));
    super.afterVerification(trie);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Node(org.trie4j.Node) NodeVisitor(org.trie4j.NodeVisitor)

Example 5 with NodeVisitor

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

the class MapTailPatriciaTrieWithSuffixTrieTailBuilderWikipediaTest method afterVerification.

@Override
protected void afterVerification(Trie trie) throws Exception {
    final AtomicInteger nodes = new AtomicInteger();
    final AtomicInteger leaves = new AtomicInteger();
    Algorithms.traverseByDepth(trie.getRoot(), new NodeVisitor() {

        @Override
        public boolean visit(Node node, int nest) {
            if (node.isTerminate())
                leaves.incrementAndGet();
            else
                nodes.incrementAndGet();
            return true;
        }
    });
    System.out.println(String.format("%d nodes and %d leaves", nodes.intValue(), leaves.intValue()));
    super.afterVerification(trie);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Node(org.trie4j.Node) NodeVisitor(org.trie4j.NodeVisitor)

Aggregations

Node (org.trie4j.Node)8 NodeVisitor (org.trie4j.NodeVisitor)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Trie (org.trie4j.Trie)2 PatriciaTrie (org.trie4j.patricia.PatriciaTrie)2 LapTimer (org.trie4j.test.LapTimer)2 WikipediaTitles (org.trie4j.test.WikipediaTitles)2 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 BytesSuccinctBitVector (org.trie4j.bv.BytesSuccinctBitVector)1