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");
}
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));
}
}
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());
}
}
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);
}
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);
}
Aggregations