]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/engine/Index.java
Major refactor of down dictionary list is stored by app.
[Dictionary.git] / src / com / hughes / android / dictionary / engine / Index.java
index e2c2fee96e145fff7ec15caff0b71a17be031bd9..19e0ecc8ed63d8285710596482950a26af45655d 100644 (file)
@@ -1,3 +1,17 @@
+// Copyright 2011 Google Inc. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
 /**
  * 
  */
@@ -11,12 +25,15 @@ import java.util.Collection;
 import java.util.List;
 import java.util.concurrent.atomic.AtomicBoolean;
 
+import com.hughes.android.dictionary.DictionaryInfo;
+import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
 import com.hughes.util.CachingList;
 import com.hughes.util.raf.RAFList;
 import com.hughes.util.raf.RAFSerializable;
 import com.hughes.util.raf.RAFSerializer;
 import com.hughes.util.raf.UniformRAFList;
 import com.ibm.icu.text.Collator;
+import com.ibm.icu.text.Transliterator;
 
 public final class Index implements RAFSerializable<Index> {
   
@@ -24,29 +41,48 @@ public final class Index implements RAFSerializable<Index> {
   
   final Dictionary dict;
   
-  final String shortName;
-  final String longName;
+  public final String shortName;  // Typically the ISO code for the language.
+  public final String longName;
   
   // persisted: tells how the entries are sorted.
-  final Language sortLanguage;
+  public final Language sortLanguage;
+  final String normalizerRules;
+  
+  // Built from the two above.
+  private Transliterator normalizer;
     
   // persisted
-  final List<IndexEntry> sortedIndexEntries;
+  public final List<IndexEntry> sortedIndexEntries;
 
   // One big list!
   // Various sub-types.
   // persisted
-  final List<RowBase> rows;
+  public final List<RowBase> rows;
+  public final boolean swapPairEntries;
+  
+  // Version 2:
+  int mainTokenCount = -1;
   
   // --------------------------------------------------------------------------
   
-  public Index(final Dictionary dict, final String shortName, final String longName, final Language sortLanguage) {
+  public Index(final Dictionary dict, final String shortName, final String longName, final Language sortLanguage, final String normalizerRules, final boolean swapPairEntries) {
     this.dict = dict;
     this.shortName = shortName;
     this.longName = longName;
     this.sortLanguage = sortLanguage;
+    this.normalizerRules = normalizerRules;
+    this.swapPairEntries = swapPairEntries;
     sortedIndexEntries = new ArrayList<IndexEntry>();
     rows = new ArrayList<RowBase>();
+    
+    normalizer = null;
+  }
+  
+  public synchronized Transliterator normalizer() {
+    if (normalizer == null) {
+      normalizer = Transliterator.createFromRules("", normalizerRules, Transliterator.FORWARD);
+    }
+    return normalizer;
   }
   
   public Index(final Dictionary dict, final RandomAccessFile raf) throws IOException {
@@ -55,32 +91,44 @@ public final class Index implements RAFSerializable<Index> {
     longName = raf.readUTF();
     final String languageCode = raf.readUTF();
     sortLanguage = Language.lookup(languageCode);
+    normalizerRules = raf.readUTF();
+    swapPairEntries = raf.readBoolean();
     if (sortLanguage == null) {
       throw new IOException("Unsupported language: " + languageCode);
     }
+    if (dict.dictFileVersion >= 2) {
+      mainTokenCount = raf.readInt();
+    }
     sortedIndexEntries = CachingList.create(RAFList.create(raf, IndexEntry.SERIALIZER, raf.getFilePointer()), CACHE_SIZE);
     rows = CachingList.create(UniformRAFList.create(raf, new RowBase.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
   }
   
-  public void print(final PrintStream out) {
-    for (final RowBase row : rows) {
-      row.print(out);
-    }
-  }
-  
   @Override
   public void write(final RandomAccessFile raf) throws IOException {
     raf.writeUTF(shortName);
     raf.writeUTF(longName);
-    raf.writeUTF(sortLanguage.getSymbol());
+    raf.writeUTF(sortLanguage.getIsoCode());
+    raf.writeUTF(normalizerRules);
+    raf.writeBoolean(swapPairEntries);
+    if (dict.dictFileVersion >= 2) {
+      raf.writeInt(mainTokenCount);
+    }
     RAFList.write(raf, sortedIndexEntries, IndexEntry.SERIALIZER);
     UniformRAFList.write(raf, (Collection<RowBase>) rows, new RowBase.Serializer(this), 5);
   }
 
+  public void print(final PrintStream out) {
+    for (final RowBase row : rows) {
+      row.print(out);
+    }
+  }
   
-  static final class IndexEntry implements RAFSerializable<Index.IndexEntry> {
-    String token;
-    int startRow;
+  public static final class IndexEntry implements RAFSerializable<Index.IndexEntry> {
+    public final String token;
+    private final String normalizedToken;
+    public final int startRow;
+    public final int numRows;
+    
     
     static final RAFSerializer<IndexEntry> SERIALIZER = new RAFSerializer<IndexEntry> () {
       @Override
@@ -92,70 +140,84 @@ public final class Index implements RAFSerializable<Index> {
         t.write(raf);
       }};
       
-    public IndexEntry(final String token, final int startRow) {
+    public IndexEntry(final String token, final String normalizedToken, final int startRow, final int numRows) {
       assert token.equals(token.trim());
       assert token.length() > 0;
       this.token = token;
+      this.normalizedToken = normalizedToken;
       this.startRow = startRow;
+      this.numRows = numRows;
     }
     
     public IndexEntry(final RandomAccessFile raf) throws IOException {
       token = raf.readUTF();
       startRow = raf.readInt();
+      numRows = raf.readInt();
+      final boolean hasNormalizedForm = raf.readBoolean();
+      normalizedToken = hasNormalizedForm ? raf.readUTF() : token;
     }
     
     public void write(RandomAccessFile raf) throws IOException {
       raf.writeUTF(token);
       raf.writeInt(startRow);
+      raf.writeInt(numRows);
+      final boolean hasNormalizedForm = !token.equals(normalizedToken);
+      raf.writeBoolean(hasNormalizedForm);
+      if (hasNormalizedForm) {
+        raf.writeUTF(normalizedToken);
+      }
     }
 
     public String toString() {
-      return token + "@" + startRow;
+      return String.format("%s@%d(%d)", token, startRow, numRows);
     }
-}
-  
 
-  private TokenRow sortedIndexToToken(final int sortedIndex) {
-    final IndexEntry indexEntry = sortedIndexEntries.get(sortedIndex);
-    return (TokenRow) rows.get(indexEntry.startRow);
+    public String normalizedToken() {
+      return normalizedToken;
+    }
   }
-
-  public TokenRow find(String token, final AtomicBoolean interrupted) {
-    token = sortLanguage.textNorm(token, true);
+  
+  public IndexEntry findInsertionPoint(String token, final AtomicBoolean interrupted) {
+    if (TransliteratorManager.init(null)) {
+      final Transliterator normalizer = normalizer();
+      token = normalizer.transliterate(token);
+    } else {
+      // Do our best since the Transliterators aren't up yet.
+      token = token.toLowerCase();
+    }
 
     int start = 0;
     int end = sortedIndexEntries.size();
     
-    final Collator sortCollator = sortLanguage.getSortCollator();
+    final Collator sortCollator = sortLanguage.getCollator();
     while (start < end) {
       final int mid = (start + end) / 2;
       if (interrupted.get()) {
-        return sortedIndexToToken(mid);
+        return null;
       }
       final IndexEntry midEntry = sortedIndexEntries.get(mid);
 
-      final int comp = sortCollator.compare(token, sortLanguage.textNorm(midEntry.token, true));
+      final int comp = sortCollator.compare(token, midEntry.normalizedToken());
       if (comp == 0) {
-        final int result = windBack(token, mid, sortCollator, interrupted);
-        return sortedIndexToToken(result);
+        final int result = windBackCase(token, mid, interrupted);
+        return sortedIndexEntries.get(result);
       } else if (comp < 0) {
-//        Log.d("THAD", "Upper bound: " + midEntry);
+        //System.out.println("Upper bound: " + midEntry + ", norm=" + midEntry.normalizedToken() + ", mid=" + mid);
         end = mid;
       } else {
-//        Log.d("THAD", "Lower bound: " + midEntry);
+        //System.out.println("Lower bound: " + midEntry + ", norm=" + midEntry.normalizedToken() + ", mid=" + mid);
         start = mid + 1;
       }
     }
+
+    // If we search for a substring of a string that's in there, return that.
     int result = Math.min(start, sortedIndexEntries.size() - 1);
-    result = windBack(token, result, sortCollator, interrupted);
-    if (result > 0 && sortCollator.compare(sortLanguage.textNorm(sortedIndexEntries.get(result).token, true), token) > 0) {
-      result = windBack(sortLanguage.textNorm(sortedIndexEntries.get(result - 1).token, true), result, sortCollator, interrupted);
-    }
-    return sortedIndexToToken(result);
+    result = windBackCase(sortedIndexEntries.get(result).normalizedToken(), result, interrupted);
+    return sortedIndexEntries.get(result);
   }
-  
-  private final int windBack(final String token, int result, final Collator sortCollator, final AtomicBoolean interrupted) {
-    while (result > 0 && sortCollator.compare(sortLanguage.textNorm(sortedIndexEntries.get(result - 1).token, true), token) >= 0) {
+    
+  private final int windBackCase(final String token, int result, final AtomicBoolean interrupted) {
+    while (result > 0 && sortedIndexEntries.get(result - 1).normalizedToken().equals(token)) {
       --result;
       if (interrupted.get()) {
         return result;
@@ -164,4 +226,8 @@ public final class Index implements RAFSerializable<Index> {
     return result;
   }
 
+  public IndexInfo getIndexInfo() {
+    return new DictionaryInfo.IndexInfo(shortName, sortedIndexEntries.size(), mainTokenCount);
+  }
+
 }
\ No newline at end of file