use of org.trie4j.Node 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.Node in project trie4j by takawitter.
the class DoubleArrayTest method print.
private void print(Node n, int nest, PrintWriter w) {
w.println(StringUtil.repeted(" ", nest) + new String(n.getLetters()) + (n.isTerminate() ? "*" : ""));
nest++;
for (Node c : n.getChildren()) {
print(c, nest, w);
}
}
use of org.trie4j.Node in project trie4j by takawitter.
the class LongsTailLOUDSTrieWithConcatTailArrayTest method test.
@Test
public void test() throws Exception {
String[] words = { "こんにちは", "さようなら", "おはよう", "おおきなかぶ", "おおやまざき" };
Trie lt = trieWithWords(words);
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());
}
use of org.trie4j.Node in project trie4j by takawitter.
the class LongsTailLOUDSTrieWithConcatTailArrayTest method test_save_load.
@Test
public void test_save_load() throws Exception {
String[] words = { "こんにちは", "さようなら", "おはよう", "おおきなかぶ", "おおやまざき" };
TailLOUDSTrie lt = trieWithWords(words);
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());
}
use of org.trie4j.Node in project trie4j by takawitter.
the class TailLOUDSPPTrieWithConcatTailArrayTest method test.
@Test
public void test() throws Exception {
String[] words = { "こんにちは", "さようなら", "おはよう", "おおきなかぶ", "おおやまざき" };
// final SBVTailIndex ti = new SBVTailIndex();
TailLOUDSTrie lt = trieWithWords(words);
// 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());
}
Aggregations