]> gitweb.fperrin.net Git - DictionaryPC.git/blobdiff - src/com/hughes/android/dictionary/engine/IndexBuilder.java
First decent implementation of HtmlEntry attached to TokenRow.
[DictionaryPC.git] / src / com / hughes / android / dictionary / engine / IndexBuilder.java
index cab33187d57f1e9bd6ae60ba6f50c77486d5bfc6..9fe234b7c7723c80ec11c02446a3bbdde84cd3a3 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.
+
 package com.hughes.android.dictionary.engine;
 
 import java.util.ArrayList;
@@ -14,39 +28,58 @@ import java.util.TreeMap;
 import com.hughes.android.dictionary.engine.Index.IndexEntry;
 import com.hughes.android.dictionary.parser.DictFileParser;
 
-
 public class IndexBuilder {
   
   final DictionaryBuilder dictionaryBuilder;
   public final Index index;
+  final Set<String> stoplist;
 
   final SortedMap<String, TokenData> tokenToData;
 
-  IndexBuilder(final DictionaryBuilder dictionaryBuilder, final String shortName, final String longName, final Language language, final String normalizerRules, final boolean swapPairEntries) {
+  IndexBuilder(final DictionaryBuilder dictionaryBuilder, final String shortName, final String longName, final Language language, final String normalizerRules, final Set<String> stoplist, final boolean swapPairEntries) {
     this.dictionaryBuilder = dictionaryBuilder;
-    index = new Index(dictionaryBuilder.dictionary, shortName, longName, language, normalizerRules, swapPairEntries);
-    tokenToData = new TreeMap<String, TokenData>(new NormalizeComparator(index.normalizer(), language.getCollator()));
+    index = new Index(dictionaryBuilder.dictionary, shortName, longName, language, normalizerRules, swapPairEntries, stoplist);
+    tokenToData = new TreeMap<String, TokenData>(index.getSortComparator());
+    this.stoplist = stoplist;
   }
   
   public void build() {
-    final Set<IndexedEntry> tokenEntryDatas = new HashSet<IndexedEntry>();
+    final Set<IndexedEntry> tokenIndexedEntries = new HashSet<IndexedEntry>();
     final List<RowBase> rows = index.rows;
+    index.mainTokenCount = 0;
     for (final TokenData tokenData : tokenToData.values()) {
-      tokenEntryDatas.clear();
+      tokenIndexedEntries.clear();
       final int indexIndex = index.sortedIndexEntries.size();
       final int startRow = rows.size();
-      rows.add(new TokenRow(indexIndex, rows.size(), index));
-//      System.out.println("Added TokenRow: " + rows.get(rows.size() - 1));
-      int numRows = 0;
+      
+      TokenRow tokenRow = null;
+      if (!tokenData.htmlEntries.isEmpty()) {
+          tokenRow = new TokenRow(indexIndex, rows.size(), index, /* hasMainEntry */ true);
+          rows.add(tokenRow);
+      }
+      
+//    System.out.println("Added TokenRow: " + rows.get(rows.size() - 1));
+      
+      int numRows = 0;  // off by one--doesn't count the token row!
 //      System.out.println("TOKEN: " + tokenData.token);
-      for (final Map.Entry<EntryTypeName, List<IndexedEntry>> typeToEntry : tokenData.typeToEntries.entrySet()) {
-        for (final IndexedEntry entryData : typeToEntry.getValue()) {
-          if (entryData.index() == -1) {
-            entryData.addToDictionary(dictionaryBuilder.dictionary);
-            assert entryData.index() >= 0;
+      for (final Map.Entry<EntryTypeName, List<IndexedEntry>> typeToIndexedEntries : tokenData.typeToEntries.entrySet()) {
+        for (final IndexedEntry indexedEntry : typeToIndexedEntries.getValue()) {
+          if (!indexedEntry.isValid) {
+            continue;
+          }
+          
+          if (tokenRow == null) {
+              tokenRow = new TokenRow(indexIndex, rows.size(), index, tokenData.hasMainEntry);
+              rows.add(tokenRow);
           }
-          if (tokenEntryDatas.add(entryData)) {
-            rows.add(new PairEntry.Row(entryData.index(), rows.size(), index));
+          
+          if (indexedEntry.entry.index() == -1) {
+            indexedEntry.entry.addToDictionary(dictionaryBuilder.dictionary);
+            assert indexedEntry.entry.index() >= 0;
+          }
+          if (tokenIndexedEntries.add(indexedEntry)) {
+            rows.add(indexedEntry.entry.CreateRow(rows.size(), index));
+            ++indexedEntry.entry.entrySource.numEntries;
             ++numRows;
             
 //            System.out.print("  " + typeToEntry.getKey() + ": ");
@@ -55,26 +88,38 @@ public class IndexBuilder {
           }
         }
       }
-      index.sortedIndexEntries.add(new Index.IndexEntry(tokenData.token, index
-          .normalizer().transliterate(tokenData.token), startRow, numRows));
+      
+      if (tokenRow != null) {
+          if (tokenRow.hasMainEntry) {
+              index.mainTokenCount++;
+          }
+          
+          final Index.IndexEntry indexEntry = new Index.IndexEntry(index, tokenData.token, index
+                  .normalizer().transliterate(tokenData.token), startRow, numRows);
+          indexEntry.htmlEntries.addAll(tokenData.htmlEntries);
+          index.sortedIndexEntries.add(indexEntry);
+      }
     }
     
-    final List<IndexEntry> sortedEntries = new ArrayList<IndexEntry>(index.sortedIndexEntries);
-    Collections.sort(sortedEntries, new Comparator<IndexEntry>() {
+    final List<IndexEntry> entriesSortedByNumRows = new ArrayList<IndexEntry>(index.sortedIndexEntries);
+    Collections.sort(entriesSortedByNumRows, new Comparator<IndexEntry>() {
       @Override
       public int compare(IndexEntry object1, IndexEntry object2) {
         return object2.numRows - object1.numRows;
       }});
     System.out.println("Most common tokens:");
-    for (int i = 0; i < 50 && i < sortedEntries.size(); ++i) {
-      System.out.println("  " + sortedEntries.get(i));
+    for (int i = 0; i < 50 && i < entriesSortedByNumRows.size(); ++i) {
+      System.out.println("  " + entriesSortedByNumRows.get(i));
     }
   }
   
-  static class TokenData {
+  public static class TokenData {
     final String token;
         
     final Map<EntryTypeName, List<IndexedEntry>> typeToEntries = new EnumMap<EntryTypeName, List<IndexedEntry>>(EntryTypeName.class);
+    boolean hasMainEntry = false;
+    
+    public List<HtmlEntry> htmlEntries = new ArrayList<HtmlEntry>();
     
     TokenData(final String token) {
       assert token.equals(token.trim());
@@ -92,9 +137,12 @@ public class IndexBuilder {
     return tokenData;
   }
 
-  public List<IndexedEntry> getOrCreateEntries(final String token, final EntryTypeName entryTypeName) {
+  private List<IndexedEntry> getOrCreateEntries(final String token, final EntryTypeName entryTypeName) {
     final TokenData tokenData = getOrCreateTokenData(token);
     List<IndexedEntry> entries = tokenData.typeToEntries.get(entryTypeName);
+    if (entryTypeName.mainWord) {
+      tokenData.hasMainEntry = true;
+    }
     if (entries == null) {
       entries = new ArrayList<IndexedEntry>();
       tokenData.typeToEntries.put(entryTypeName, entries);
@@ -104,19 +152,26 @@ public class IndexBuilder {
 
   public void addEntryWithTokens(final IndexedEntry indexedEntry, final Set<String> tokens,
       final EntryTypeName entryTypeName) {
+    if (indexedEntry == null) {
+      System.out.println("asdfasdf");
+    }
+    assert indexedEntry != null;
     for (final String token : tokens) {
-      getOrCreateEntries(token, entryTypeName).add(indexedEntry);
+      if (entryTypeName.overridesStopList || !stoplist.contains(token)) {
+        getOrCreateEntries(token, entryTypeName).add(indexedEntry);
+      }
     }    
   }
 
   public void addEntryWithString(final IndexedEntry indexedEntry, final String untokenizedString,
-      final EntryTypeName singleTokenEntryTypeName, final EntryTypeName multiTokenEntryTypeName) {
+      final EntryTypeName entryTypeName) {
     final Set<String> tokens = DictFileParser.tokenize(untokenizedString, DictFileParser.NON_CHAR);
-    addEntryWithTokens(indexedEntry, tokens, tokens.size() == 1 ? singleTokenEntryTypeName : multiTokenEntryTypeName);
+    addEntryWithTokens(indexedEntry, tokens, tokens.size() == 1 ? entryTypeName.singleWordInstance : entryTypeName);
   }
 
-  public void addEntryWithString(final IndexedEntry indexedEntry, final String untokenizedString,
+  public void addEntryWithStringNoSingle(final IndexedEntry indexedEntry, final String untokenizedString,
       final EntryTypeName entryTypeName) {
-    addEntryWithString(indexedEntry, untokenizedString, entryTypeName, entryTypeName);
+    final Set<String> tokens = DictFileParser.tokenize(untokenizedString, DictFileParser.NON_CHAR);
+    addEntryWithTokens(indexedEntry, tokens, entryTypeName);
   }
 }