]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/IndexBuilder.java
0c3fa13da2147e30c0bd9e9f0ab3c0ed2b925bb0
[DictionaryPC.git] / src / com / hughes / android / dictionary / engine / IndexBuilder.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package com.hughes.android.dictionary.engine;
16
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.Comparator;
20 import java.util.EnumMap;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.SortedMap;
26 import java.util.TreeMap;
27
28 import com.hughes.android.dictionary.engine.Index.IndexEntry;
29 import com.hughes.android.dictionary.parser.DictFileParser;
30
31 public class IndexBuilder {
32   
33   final DictionaryBuilder dictionaryBuilder;
34   public final Index index;
35   final Set<String> stoplist;
36
37   final SortedMap<String, TokenData> tokenToData;
38
39   IndexBuilder(final DictionaryBuilder dictionaryBuilder, final String shortName, final String longName, final Language language, final String normalizerRules, final Set<String> stoplist, final boolean swapPairEntries) {
40     this.dictionaryBuilder = dictionaryBuilder;
41     index = new Index(dictionaryBuilder.dictionary, shortName, longName, language, normalizerRules, swapPairEntries, stoplist);
42     tokenToData = new TreeMap<String, TokenData>(index.getSortComparator());
43     this.stoplist = stoplist;
44   }
45   
46   public void build() {
47     final Set<IndexedEntry> tokenIndexedEntries = new HashSet<IndexedEntry>();
48     final List<RowBase> rows = index.rows;
49     index.mainTokenCount = 0;
50     for (final TokenData tokenData : tokenToData.values()) {
51       tokenIndexedEntries.clear();
52       final int indexIndex = index.sortedIndexEntries.size();
53       final int startRow = rows.size();
54       
55       TokenRow tokenRow = null;
56       if (!tokenData.htmlEntries.isEmpty()) {
57           tokenRow = new TokenRow(indexIndex, rows.size(), index, tokenData.hasMainEntry);
58           rows.add(tokenRow);
59       }
60       
61 //    System.out.println("Added TokenRow: " + rows.get(rows.size() - 1));
62       
63       int numRows = 0;  // off by one--doesn't count the token row!
64 //      System.out.println("TOKEN: " + tokenData.token);
65       for (final Map.Entry<EntryTypeName, List<IndexedEntry>> typeToIndexedEntries : tokenData.typeToEntries.entrySet()) {
66         for (final IndexedEntry indexedEntry : typeToIndexedEntries.getValue()) {
67           if (!indexedEntry.isValid) {
68             continue;
69           }
70           
71           if (tokenRow == null) {
72               tokenRow = new TokenRow(indexIndex, rows.size(), index, tokenData.hasMainEntry);
73               rows.add(tokenRow);
74           }
75           
76           if (indexedEntry.entry.index() == -1) {
77             indexedEntry.entry.addToDictionary(dictionaryBuilder.dictionary);
78             assert indexedEntry.entry.index() >= 0;
79           }
80           if (tokenIndexedEntries.add(indexedEntry) && !tokenData.htmlEntries.contains(indexedEntry.entry)) {
81             rows.add(indexedEntry.entry.CreateRow(rows.size(), index));
82             ++indexedEntry.entry.entrySource.numEntries;
83             ++numRows;
84             
85 //            System.out.print("  " + typeToEntry.getKey() + ": ");
86   //          rows.get(rows.size() - 1).print(System.out);
87 //            System.out.println();
88           }
89         }
90       }
91       
92       if (tokenRow != null) {
93           if (tokenRow.hasMainEntry) {
94               index.mainTokenCount++;
95           }
96           
97           final Index.IndexEntry indexEntry = new Index.IndexEntry(index, tokenData.token, index
98                   .normalizer().transliterate(tokenData.token), startRow, numRows);
99           indexEntry.htmlEntries.addAll(tokenData.htmlEntries);
100           index.sortedIndexEntries.add(indexEntry);
101       }
102     }
103     
104     final List<IndexEntry> entriesSortedByNumRows = new ArrayList<IndexEntry>(index.sortedIndexEntries);
105     Collections.sort(entriesSortedByNumRows, new Comparator<IndexEntry>() {
106       @Override
107       public int compare(IndexEntry object1, IndexEntry object2) {
108         return object2.numRows - object1.numRows;
109       }});
110     System.out.println("Most common tokens:");
111     for (int i = 0; i < 50 && i < entriesSortedByNumRows.size(); ++i) {
112       System.out.println("  " + entriesSortedByNumRows.get(i));
113     }
114   }
115   
116   public static class TokenData {
117     final String token;
118         
119     final Map<EntryTypeName, List<IndexedEntry>> typeToEntries = new EnumMap<EntryTypeName, List<IndexedEntry>>(EntryTypeName.class);
120     public boolean hasMainEntry = false;
121     
122     public List<HtmlEntry> htmlEntries = new ArrayList<HtmlEntry>();
123     
124     TokenData(final String token) {
125       assert token.equals(token.trim());
126       assert token.length() > 0;
127       this.token = token;
128     }
129   }
130
131   public TokenData getOrCreateTokenData(final String token) {
132     TokenData tokenData = tokenToData.get(token);
133     if (tokenData == null) {
134       tokenData = new TokenData(token);
135       tokenToData.put(token, tokenData);
136     }
137     return tokenData;
138   }
139
140   private List<IndexedEntry> getOrCreateEntries(final String token, final EntryTypeName entryTypeName) {
141     final TokenData tokenData = getOrCreateTokenData(token);
142     List<IndexedEntry> entries = tokenData.typeToEntries.get(entryTypeName);
143     if (entryTypeName.mainWord) {
144       tokenData.hasMainEntry = true;
145     }
146     if (entries == null) {
147       entries = new ArrayList<IndexedEntry>();
148       tokenData.typeToEntries.put(entryTypeName, entries);
149     }
150     return entries;
151   }
152
153   public void addEntryWithTokens(final IndexedEntry indexedEntry, final Set<String> tokens,
154       final EntryTypeName entryTypeName) {
155     if (indexedEntry == null) {
156       System.out.println("asdfasdf");
157     }
158     assert indexedEntry != null;
159     for (final String token : tokens) {
160       if (entryTypeName.overridesStopList || !stoplist.contains(token)) {
161         getOrCreateEntries(token, entryTypeName).add(indexedEntry);
162       }
163     }    
164   }
165
166   public void addEntryWithString(final IndexedEntry indexedEntry, final String untokenizedString,
167       final EntryTypeName entryTypeName) {
168     final Set<String> tokens = DictFileParser.tokenize(untokenizedString, DictFileParser.NON_CHAR);
169     addEntryWithTokens(indexedEntry, tokens, tokens.size() == 1 ? entryTypeName.singleWordInstance : entryTypeName);
170   }
171
172   public void addEntryWithStringNoSingle(final IndexedEntry indexedEntry, final String untokenizedString,
173       final EntryTypeName entryTypeName) {
174     final Set<String> tokens = DictFileParser.tokenize(untokenizedString, DictFileParser.NON_CHAR);
175     addEntryWithTokens(indexedEntry, tokens, entryTypeName);
176   }
177 }