Search in sources :

Example 6 with Node

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));
    }
}
Also used : HashMap(java.util.HashMap) Node(org.trie4j.Node) OutputStreamWriter(java.io.OutputStreamWriter) NodeVisitor(org.trie4j.NodeVisitor)

Example 7 with Node

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);
    }
}
Also used : Node(org.trie4j.Node)

Example 8 with Node

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());
}
Also used : Node(org.trie4j.Node) Trie(org.trie4j.Trie) AbstractTermIdTrieTest(org.trie4j.AbstractTermIdTrieTest) Test(org.junit.Test)

Example 9 with Node

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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Node(org.trie4j.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) AbstractTermIdTrieTest(org.trie4j.AbstractTermIdTrieTest) Test(org.junit.Test)

Example 10 with Node

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());
}
Also used : Node(org.trie4j.Node) AbstractTermIdTrieTest(org.trie4j.AbstractTermIdTrieTest) Test(org.junit.Test)

Aggregations

Node (org.trie4j.Node)31 Test (org.junit.Test)15 AbstractTermIdTrieTest (org.trie4j.AbstractTermIdTrieTest)13 Trie (org.trie4j.Trie)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 ObjectInputStream (java.io.ObjectInputStream)8 ObjectOutputStream (java.io.ObjectOutputStream)8 NodeVisitor (org.trie4j.NodeVisitor)8 PatriciaTrie (org.trie4j.patricia.PatriciaTrie)6 LinkedList (java.util.LinkedList)3 PrintWriter (java.io.PrintWriter)2 ArrayList (java.util.ArrayList)2 AbstractImmutableTrieTest (org.trie4j.AbstractImmutableTrieTest)2 LapTimer (org.trie4j.test.LapTimer)2 WikipediaTitles (org.trie4j.test.WikipediaTitles)2 Pair (org.trie4j.util.Pair)2 OutputStreamWriter (java.io.OutputStreamWriter)1 HashMap (java.util.HashMap)1