From 5ba32391ba870245c27e94389d537d1640fe3702 Mon Sep 17 00:00:00 2001 From: thadh Date: Mon, 23 Feb 2009 00:30:58 -0800 Subject: [PATCH] addingList --- .../android/dictionary/IndexBuilder.java | 26 ++++++---- .../hughes/android/dictionary/IndexTest.java | 49 +++++++++++++++---- 2 files changed, 57 insertions(+), 18 deletions(-) diff --git a/src/com/hughes/android/dictionary/IndexBuilder.java b/src/com/hughes/android/dictionary/IndexBuilder.java index 9203deb..bc5d72d 100755 --- a/src/com/hughes/android/dictionary/IndexBuilder.java +++ b/src/com/hughes/android/dictionary/IndexBuilder.java @@ -193,18 +193,19 @@ public class IndexBuilder { return count; } - void recursiveSetDescendantOffsetCount() { + void recursiveSetDescendantCounts() { + descendantTokenCount = entryDescriptorsMap.size(); descendantEntryCount = 0; - descendantTokenCount = 0; - for (final List entryDescriptors : entryDescriptorsMap.values()) { - descendantTokenCount += 1; - descendantEntryCount += entryDescriptors.size(); - } + for (final Node child : children.values()) { - child.recursiveSetDescendantOffsetCount(); + child.recursiveSetDescendantCounts(); descendantTokenCount += child.descendantTokenCount; descendantEntryCount += child.descendantEntryCount; } + + for (final List entryDescriptors : entryDescriptorsMap.values()) { + descendantEntryCount += entryDescriptors.size(); + } } @Override @@ -219,7 +220,7 @@ public class IndexBuilder { assert indexFileLocation == file.getFilePointer(); } - // Children. + // Children to location. file.writeInt(children.size()); for (final Map.Entry child : children.entrySet()) { file.writeUTF(child.getKey()); @@ -235,6 +236,10 @@ public class IndexBuilder { file.writeInt(entry.getValue().get(i).offset); } } + + // Dump counts. + file.writeInt(descendantTokenCount); + file.writeInt(descendantEntryCount); // Dump children. for (final Map.Entry child : children.entrySet()) { @@ -275,7 +280,7 @@ public class IndexBuilder { if (token.length() <= 1 || !Character.isLetter(token.charAt(0))) { continue; } - tokenToNormalizedMap.put(token, EntryFactory.entryFactory.normalizeToken(token, lang)); + tokenToNormalizedMap.put(token, EntryFactory.entryFactory.normalizeToken(token)); } for (final Map.Entry tokenToNormalized : tokenToNormalizedMap.entrySet()) { final String normalizedToken = tokenToNormalized.getValue(); @@ -294,6 +299,9 @@ public class IndexBuilder { fileLocation = dictionaryFile.getFilePointer(); } dictionaryFile.close(); + + root.recursiveSetDescendantCounts(); + return root; } diff --git a/src/com/hughes/android/dictionary/IndexTest.java b/src/com/hughes/android/dictionary/IndexTest.java index 5c0b847..2622e30 100755 --- a/src/com/hughes/android/dictionary/IndexTest.java +++ b/src/com/hughes/android/dictionary/IndexTest.java @@ -15,35 +15,66 @@ public class IndexTest extends TestCase { static final String file = "c:\\dict-de-en.txt"; static final String file_index = file + "_index_0"; + public void testRoot() throws IOException { + System.out.println(" testRoot"); + final Index index = new Index(file_index); + final Node node = index.lookup(""); + assertNotNull(node); + + assertEquals(312220, node.descendantTokenCount); + assertEquals(1087063, node.descendantEntryCount); + + for (final String token : node.tokenToOffsets.keySet()) { + System.out.println(token); + assertTrue(token.toLowerCase().contains("handhubwagen")); + } + } + public void testLookup() throws IOException { - System.out.println("testLookup"); + System.out.println(" testLookup"); final Index index = new Index(file_index); final Node node = index.lookup("handhubwagen"); assertNotNull(node); - final RandomAccessFile raf = new RandomAccessFile(file, "r"); - for (int i = 0; i < node.offsets.length; ++i) { - final String entry = FileUtil.readLine(raf, node.offsets[i]); - System.out.println(entry); - assertTrue(entry.toLowerCase().contains("handhubwagen")); + assertEquals(1, node.descendantTokenCount); + assertEquals(2, node.descendantEntryCount); + + for (final String token : node.tokenToOffsets.keySet()) { + System.out.println(token); + assertTrue(token.toLowerCase().contains("handhubwagen")); } } public void testGetDescendantOffsets() throws IOException { - System.out.println("testGetDescendantOffsets"); + System.out.println(" testGetDescendantOffsets"); final Index index = new Index(file_index); final Node node = index.lookup("handhebe"); assertNotNull(node); - assertEquals("handhebel", node.text); + assertEquals("handhebel", node.nodeHandle.normalizedToken); final Set offsets = new LinkedHashSet(); node.getDescendantEntryOffsets(offsets, 10); final RandomAccessFile raf = new RandomAccessFile(file, "r"); for (final Integer offset : offsets) { final String entry = FileUtil.readLine(raf, offset); System.out.println(entry); - assertTrue(entry.toLowerCase().contains(node.text)); + assertTrue(entry.toLowerCase().contains(node.nodeHandle.normalizedToken)); + } + } + + public void testGetDescendants() throws IOException { + System.out.println(" testGetDescendant"); + final Index index = new Index(file_index); + final RandomAccessFile raf = new RandomAccessFile(file, "r"); + for (int i = 1000000; i < 1000050; ++i) { + final Object o = index.root.getDescendant(i); + if (o instanceof Integer) { + System.out.println(" " + FileUtil.readLine(raf, (Integer)o)); + } else { + System.out.println(o); + } } + raf.close(); } } -- 2.43.0