]> gitweb.fperrin.net Git - DictionaryPC.git/commitdiff
addingList
authorthadh <thadh@THADH-MTV.ad.corp.google.com>
Mon, 23 Feb 2009 08:30:58 +0000 (00:30 -0800)
committerthadh <thadh@THADH-MTV.ad.corp.google.com>
Mon, 23 Feb 2009 08:30:58 +0000 (00:30 -0800)
src/com/hughes/android/dictionary/IndexBuilder.java
src/com/hughes/android/dictionary/IndexTest.java

index 9203deba66f26f41da46de1525ecc0c030d6575d..bc5d72d303eb4d95b6600af86fb542e28e05b976 100755 (executable)
@@ -193,18 +193,19 @@ public class IndexBuilder {
       return count;\r
     }\r
 \r
-    void recursiveSetDescendantOffsetCount() {\r
+    void recursiveSetDescendantCounts() {\r
+      descendantTokenCount = entryDescriptorsMap.size();\r
       descendantEntryCount = 0;\r
-      descendantTokenCount = 0;\r
-      for (final List<EntryDescriptor> entryDescriptors : entryDescriptorsMap.values()) {\r
-        descendantTokenCount += 1;\r
-        descendantEntryCount += entryDescriptors.size();\r
-      }\r
+\r
       for (final Node child : children.values()) {\r
-        child.recursiveSetDescendantOffsetCount();\r
+        child.recursiveSetDescendantCounts();\r
         descendantTokenCount += child.descendantTokenCount;\r
         descendantEntryCount += child.descendantEntryCount;\r
       }\r
+\r
+      for (final List<EntryDescriptor> entryDescriptors : entryDescriptorsMap.values()) {\r
+        descendantEntryCount += entryDescriptors.size();\r
+      }\r
     }\r
 \r
     @Override\r
@@ -219,7 +220,7 @@ public class IndexBuilder {
         assert indexFileLocation == file.getFilePointer();\r
       }\r
       \r
-      // Children.\r
+      // Children to location.\r
       file.writeInt(children.size());\r
       for (final Map.Entry<String, Node> child : children.entrySet()) {\r
         file.writeUTF(child.getKey());\r
@@ -235,6 +236,10 @@ public class IndexBuilder {
           file.writeInt(entry.getValue().get(i).offset);\r
         }\r
       }\r
+\r
+      // Dump counts.\r
+      file.writeInt(descendantTokenCount);\r
+      file.writeInt(descendantEntryCount);\r
       \r
       // Dump children.\r
       for (final Map.Entry<String, Node> child : children.entrySet()) {\r
@@ -275,7 +280,7 @@ public class IndexBuilder {
         if (token.length() <= 1 || !Character.isLetter(token.charAt(0))) {\r
           continue;\r
         }\r
-        tokenToNormalizedMap.put(token, EntryFactory.entryFactory.normalizeToken(token, lang));\r
+        tokenToNormalizedMap.put(token, EntryFactory.entryFactory.normalizeToken(token));\r
       }\r
       for (final Map.Entry<String, String> tokenToNormalized : tokenToNormalizedMap.entrySet()) {\r
         final String normalizedToken = tokenToNormalized.getValue();\r
@@ -294,6 +299,9 @@ public class IndexBuilder {
       fileLocation = dictionaryFile.getFilePointer();\r
     }\r
     dictionaryFile.close();\r
+    \r
+    root.recursiveSetDescendantCounts();\r
+    \r
     return root;\r
   }\r
 \r
index 5c0b8473831447f4f9773cf5166583deb65c89a7..2622e30963ab27bcda7bb72cdd70b4a82d4bf948 100755 (executable)
@@ -15,35 +15,66 @@ public class IndexTest extends TestCase {
   static final String file = "c:\\dict-de-en.txt";\r
   static final String file_index = file + "_index_0";\r
   \r
+  public void testRoot() throws IOException {\r
+    System.out.println("  testRoot");\r
+    final Index index = new Index(file_index);\r
+    final Node node = index.lookup("");\r
+    assertNotNull(node);\r
+    \r
+    assertEquals(312220, node.descendantTokenCount);\r
+    assertEquals(1087063, node.descendantEntryCount);\r
+    \r
+    for (final String token : node.tokenToOffsets.keySet()) {\r
+      System.out.println(token);\r
+      assertTrue(token.toLowerCase().contains("handhubwagen"));\r
+    }\r
+  }\r
+  \r
   public void testLookup() throws IOException {\r
-    System.out.println("testLookup");\r
+    System.out.println("  testLookup");\r
     final Index index = new Index(file_index);\r
     final Node node = index.lookup("handhubwagen");\r
     assertNotNull(node);\r
     \r
-    final RandomAccessFile raf = new RandomAccessFile(file, "r");\r
-    for (int i = 0; i < node.offsets.length; ++i) {\r
-      final String entry = FileUtil.readLine(raf, node.offsets[i]);\r
-      System.out.println(entry);\r
-      assertTrue(entry.toLowerCase().contains("handhubwagen"));\r
+    assertEquals(1, node.descendantTokenCount);\r
+    assertEquals(2, node.descendantEntryCount);\r
+    \r
+    for (final String token : node.tokenToOffsets.keySet()) {\r
+      System.out.println(token);\r
+      assertTrue(token.toLowerCase().contains("handhubwagen"));\r
     }\r
   }\r
 \r
   public void testGetDescendantOffsets() throws IOException {\r
-    System.out.println("testGetDescendantOffsets");\r
+    System.out.println("  testGetDescendantOffsets");\r
     final Index index = new Index(file_index);\r
     \r
     final Node node = index.lookup("handhebe");\r
     assertNotNull(node);\r
-    assertEquals("handhebel", node.text);\r
+    assertEquals("handhebel", node.nodeHandle.normalizedToken);\r
     final Set<Integer> offsets = new LinkedHashSet<Integer>();\r
     node.getDescendantEntryOffsets(offsets, 10);\r
     final RandomAccessFile raf = new RandomAccessFile(file, "r");\r
     for (final Integer offset : offsets) {\r
       final String entry = FileUtil.readLine(raf, offset);\r
       System.out.println(entry);\r
-      assertTrue(entry.toLowerCase().contains(node.text));\r
+      assertTrue(entry.toLowerCase().contains(node.nodeHandle.normalizedToken));\r
+    }\r
+  }\r
+\r
+  public void testGetDescendants() throws IOException {\r
+    System.out.println("  testGetDescendant");\r
+    final Index index = new Index(file_index);\r
+    final RandomAccessFile raf = new RandomAccessFile(file, "r");\r
+    for (int i = 1000000; i < 1000050; ++i) {\r
+      final Object o = index.root.getDescendant(i);\r
+      if (o instanceof Integer) {\r
+        System.out.println("  " + FileUtil.readLine(raf, (Integer)o));\r
+      } else {\r
+        System.out.println(o);\r
+      }\r
     }\r
+    raf.close();\r
   }\r
 \r
 }\r