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