Search in sources :

Example 11 with LapTimer

use of org.trie4j.test.LapTimer in project trie4j by takawitter.

the class AbstractMapTrieWikipediaTest method test.

@Test
public void test() throws Exception {
    MapTrie<Integer> trie = createFirstTrie();
    System.out.println("building first trie: " + trie.getClass().getName());
    int c = 0, chars = 0;
    long b = 0;
    LapTimer t = new LapTimer();
    for (String word : new WikipediaTitles(wikipediaFilename)) {
        try {
            t.reset();
            trie.insert(word, c);
            b += t.lapNanos();
        } catch (Exception e) {
            System.out.println("exception at " + c + "th word: " + word);
            trie.dump(new PrintWriter(System.out));
            throw e;
        }
        c++;
        chars += word.length();
    }
    System.out.println(String.format("done in %d millis with %d words and %d chars.", (b / 1000000), c, chars));
    MapTrie<Integer> second = buildSecondTrie(trie);
    try {
        getClass().getDeclaredMethod("buildSecondTrie", Trie.class);
        System.out.print("building second trie: ");
        t.reset();
        second = buildSecondTrie(trie);
        System.out.println("done in " + t.lapMillis() + "millis.");
    } catch (NoSuchMethodException e) {
    }
    System.out.println("verifying trie.");
    long sum = 0;
    c = 0;
    for (String word : new WikipediaTitles(wikipediaFilename)) {
        t.reset();
        boolean found = (int) second.get(word) == c;
        sum += t.lapNanos();
        c++;
        if (!found) {
            System.out.println(String.format("verification failed.  trie not contains %d th word: [%s]" + " with id: [%d] actual: [%d].", c, word, c, second.get(word)));
            break;
        }
    }
    System.out.println("done in " + (sum / 1000000) + " millis with " + c + " words.");
    afterVerification(second);
}
Also used : WikipediaTitles(org.trie4j.test.WikipediaTitles) LapTimer(org.trie4j.test.LapTimer) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 12 with LapTimer

use of org.trie4j.test.LapTimer in project trie4j by takawitter.

the class LZSS method main.

public static void main(String[] args) throws Exception {
    LapTimer lt = new LapTimer();
    String src = "abcabdrz";
    src = read("data/jawiki-20120220-tail");
    int windowSize = 8192;
    lt.reset();
    LZSSData ret = compress(src, windowSize);
    lt.lapMillis("compress done. %d elements, %d chars", ret.match.length(), ret.dest.length());
    dump(ret);
    StringBuilder b = new StringBuilder();
    lt.reset();
    decompress(ret, b);
    lt.lapMillis("decompress done.");
    StringBuilder dest = ret.dest;
    int sz = dest.length();
    int bsz = ret.size / 8 + (((ret.size) % 8 == 0) ? 0 : 1);
    boolean eq = src.equals(b.toString());
    System.out.println(String.format("src: %d, comp: %d(%02.1f%%) + %dbytes, decomp: %d, %b", src.length(), sz, 1.0 * sz / src.length() * 100, bsz, b.length(), eq));
    for (int i = 0; i < src.length(); i++) {
        if (src.charAt(i) != b.charAt(i)) {
            System.out.println(String.format("%dth char different [%c:%c]", i, src.charAt(i), b.charAt(i)));
            int s = Math.max(i - 5, 0);
            int e = Math.min(i + 5, src.length());
            System.out.println("src: " + src.substring(s, e));
            System.out.println("dec: " + b.substring(s, e));
            break;
        }
    }
}
Also used : LapTimer(org.trie4j.test.LapTimer)

Example 13 with LapTimer

use of org.trie4j.test.LapTimer in project trie4j by takawitter.

the class TestIO method testLoad.

@Test
public void testLoad() throws Exception {
    TailDoubleArray da = new TailDoubleArray();
    LapTimer t = new LapTimer();
    System.out.println("-- loading double array.");
    da.load(new GZIPInputStream(new FileInputStream("da.dat")));
    System.out.println("done in " + t.lapMillis() + " millis.");
    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);
    }
    Thread.sleep(10000);
    da.contains("hello");
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) LapTimer(org.trie4j.test.LapTimer) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 14 with LapTimer

use of org.trie4j.test.LapTimer in project trie4j by takawitter.

the class TestIO method verify.

private static void verify(Trie da) throws Exception {
    System.out.println("verifying double array...");
    int c = 0;
    int sum = 0;
    LapTimer t1 = new LapTimer();
    LapTimer t = new LapTimer();
    for (String word : new WikipediaTitles()) {
        if (c == maxCount)
            break;
        t.reset();
        boolean found = da.contains(word);
        sum += t.lapMillis();
        c++;
        if (!found) {
            System.out.println("verification failed.  trie not contains " + c + " th word: [" + word + "]");
            break;
        }
    }
    System.out.println("done " + c + "words in " + t1.lapMillis() + " millis.");
    System.out.println("contains time: " + sum + " millis.");
}
Also used : WikipediaTitles(org.trie4j.test.WikipediaTitles) LapTimer(org.trie4j.test.LapTimer)

Example 15 with LapTimer

use of org.trie4j.test.LapTimer in project trie4j by takawitter.

the class AbstractWikipediaSerializeTest method test.

@Test
public void test() throws Exception {
    WikipediaTitles wt = new WikipediaTitles();
    Trie trie = wt.insertTo(firstTrie());
    trie = secondTrie(trie);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    LapTimer lt = new LapTimer();
    oos.writeObject(trie);
    oos.flush();
    long wd = lt.lapMillis();
    byte[] serialized = baos.toByteArray();
    lt.reset();
    Trie t = (Trie) new ObjectInputStream(new ByteArrayInputStream(serialized)).readObject();
    long rd = lt.lapMillis();
    long vd = wt.assertAllContains(t);
    System.out.println(String.format("%s%s%s, size: %d, write(ms): %d, read(ms): %d, verify(ms): %d.", trie.getClass().getSimpleName(), getBvTreeClassName(trie), getTailClassName(trie), serialized.length, wd, rd, vd));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) WikipediaTitles(org.trie4j.test.WikipediaTitles) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) TailPatriciaTrie(org.trie4j.patricia.TailPatriciaTrie) TailLOUDSTrie(org.trie4j.louds.TailLOUDSTrie) LapTimer(org.trie4j.test.LapTimer) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Aggregations

LapTimer (org.trie4j.test.LapTimer)16 WikipediaTitles (org.trie4j.test.WikipediaTitles)12 Test (org.junit.Test)8 PrintWriter (java.io.PrintWriter)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Trie (org.trie4j.Trie)4 ObjectInputStream (java.io.ObjectInputStream)3 ObjectOutputStream (java.io.ObjectOutputStream)3 PatriciaTrie (org.trie4j.patricia.PatriciaTrie)3 TailPatriciaTrie (org.trie4j.patricia.TailPatriciaTrie)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Node (org.trie4j.Node)2 NodeVisitor (org.trie4j.NodeVisitor)2 TailLOUDSTrie (org.trie4j.louds.TailLOUDSTrie)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 Set (java.util.Set)1 GZIPInputStream (java.util.zip.GZIPInputStream)1