use of org.trie4j.patricia.PatriciaTrie in project trie4j by takawitter.
the class TailLOUDSTrieWithSuffixTrieTailArrayTest method test_save_load2.
@Test
public void test_save_load2() 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);
oos.writeObject(lt);
oos.flush();
lt = (TailLOUDSTrie) new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())).readObject();
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 BitVectorExp method main.
public static void main(String[] args) throws Exception {
Trie trie = new PatriciaTrie();
int c = 0;
// You can download archive from http://dumps.wikimedia.org/jawiki/latest/
LapTimer t = new LapTimer();
for (String word : new WikipediaTitles()) {
trie.insert(word);
c++;
if (c == maxCount)
break;
}
t.lapMillis("trie building done. %d words.", c);
final BytesSuccinctBitVector bv = new BytesSuccinctBitVector(5000000);
final AtomicInteger nodeCount = new AtomicInteger();
Algorithms.traverseByDepth(trie.getRoot(), new NodeVisitor() {
@Override
public boolean visit(Node node, int nest) {
Node[] children = node.getChildren();
if (children != null) {
int n = node.getChildren().length;
for (int i = 0; i < n; i++) {
bv.append(true);
}
}
bv.append(false);
nodeCount.incrementAndGet();
return true;
}
});
trie = null;
t.lapMillis("done. %d nodes inserted. do rank and select", nodeCount.intValue());
for (int i = 0; i < c; i += 100) {
int count = bv.rank(i, true);
bv.select(count, true);
}
t.lapMillis("done.");
Thread.sleep(10000);
}
use of org.trie4j.patricia.PatriciaTrie in project trie4j by takawitter.
the class Issue_031 method test_UnsafeDoubleArray.
@Test
public void test_UnsafeDoubleArray() throws Throwable {
Trie trie = new PatriciaTrie();
insertLines(trie, FILE_NAME);
@SuppressWarnings("deprecation") Trie da = new org.trie4j.doublearray.UnsafeDoubleArray(trie);
Assert.assertFalse(trie.contains("you"));
Assert.assertFalse(da.contains("you"));
}
use of org.trie4j.patricia.PatriciaTrie in project trie4j by takawitter.
the class LongsConstantTimeSelect0TailLOUDSTrieWithConcatTailArrayTest method test.
@Test
public void test() throws Exception {
String[] words = { "こんにちは", "さようなら", "おはよう", "おおきなかぶ", "おおやまざき" };
Trie trie = new PatriciaTrie();
for (String w : words) trie.insert(w);
Trie lt = new TailLOUDSTrie(trie);
// 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());
}
use of org.trie4j.patricia.PatriciaTrie in project trie4j by takawitter.
the class TestWikipediaCPS method main.
public static void main(String[] args) throws Exception {
System.out.println("--- recursive patricia trie ---");
Trie trie = new PatriciaTrie();
int c = 0;
BufferedReader r = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream("jawiki-20120220-all-titles-in-ns0.gz")), CharsetUtil.newUTF8Decoder()));
String word = null;
long sum = 0;
long lap = System.currentTimeMillis();
while ((word = r.readLine()) != null) {
long d = System.currentTimeMillis();
trie.insert(word);
sum += System.currentTimeMillis() - d;
if (c % 100000 == 0) {
d = System.currentTimeMillis() - lap;
long free = Runtime.getRuntime().freeMemory();
System.out.println(c + "," + free + "," + Runtime.getRuntime().maxMemory() + "," + d);
lap = System.currentTimeMillis();
}
c++;
if (c == maxCount)
break;
}
System.out.println(c + "entries in ja wikipedia titles.");
System.out.println("insert time: " + sum + " millis.");
System.out.println("-- insert done.");
System.out.println(Runtime.getRuntime().freeMemory() + " bytes free.");
doSearches(trie);
}
Aggregations