]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/IndexBuilder.java
Better DictionaryInfo, IndexBuilder counts main TokenRows.
[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);
42     tokenToData = new TreeMap<String, TokenData>(new NormalizeComparator(index.normalizer(), language.getCollator()));
43     this.stoplist = stoplist;
44   }
45   
46   public void build() {
47     final Set<IndexedEntry> tokenEntryDatas = new HashSet<IndexedEntry>();
48     final List<RowBase> rows = index.rows;
49     index.mainTokenCount = 0;
50     for (final TokenData tokenData : tokenToData.values()) {
51       tokenEntryDatas.clear();
52       final int indexIndex = index.sortedIndexEntries.size();
53       final int startRow = rows.size();
54       
55       final TokenRow tokenRow = new TokenRow(indexIndex, rows.size(), index, tokenData.hasMainEntry);
56       rows.add(tokenRow);
57       if (tokenRow.hasMainEntry) {
58         index.mainTokenCount++;
59       }
60 //      System.out.println("Added TokenRow: " + rows.get(rows.size() - 1));
61       int numRows = 0;
62 //      System.out.println("TOKEN: " + tokenData.token);
63       for (final Map.Entry<EntryTypeName, List<IndexedEntry>> typeToEntry : tokenData.typeToEntries.entrySet()) {
64         for (final IndexedEntry entryData : typeToEntry.getValue()) {
65           if (entryData.index() == -1) {
66             entryData.addToDictionary(dictionaryBuilder.dictionary);
67             assert entryData.index() >= 0;
68           }
69           if (tokenEntryDatas.add(entryData)) {
70             rows.add(new PairEntry.Row(entryData.index(), rows.size(), index));
71             ++numRows;
72             
73 //            System.out.print("  " + typeToEntry.getKey() + ": ");
74   //          rows.get(rows.size() - 1).print(System.out);
75 //            System.out.println();
76           }
77         }
78       }
79       index.sortedIndexEntries.add(new Index.IndexEntry(tokenData.token, index
80           .normalizer().transliterate(tokenData.token), startRow, numRows));
81     }
82     
83     final List<IndexEntry> entriesSortedByNumRows = new ArrayList<IndexEntry>(index.sortedIndexEntries);
84     Collections.sort(entriesSortedByNumRows, new Comparator<IndexEntry>() {
85       @Override
86       public int compare(IndexEntry object1, IndexEntry object2) {
87         return object2.numRows - object1.numRows;
88       }});
89     System.out.println("Most common tokens:");
90     for (int i = 0; i < 50 && i < entriesSortedByNumRows.size(); ++i) {
91       System.out.println("  " + entriesSortedByNumRows.get(i));
92     }
93   }
94   
95   static class TokenData {
96     final String token;
97         
98     final Map<EntryTypeName, List<IndexedEntry>> typeToEntries = new EnumMap<EntryTypeName, List<IndexedEntry>>(EntryTypeName.class);
99     boolean hasMainEntry = false;
100     
101     TokenData(final String token) {
102       assert token.equals(token.trim());
103       assert token.length() > 0;
104       this.token = token;
105     }
106   }
107
108   private TokenData getOrCreateTokenData(final String token) {
109     TokenData tokenData = tokenToData.get(token);
110     if (tokenData == null) {
111       tokenData = new TokenData(token);
112       tokenToData.put(token, tokenData);
113     }
114     return tokenData;
115   }
116
117   private List<IndexedEntry> getOrCreateEntries(final String token, final EntryTypeName entryTypeName) {
118     final TokenData tokenData = getOrCreateTokenData(token);
119     List<IndexedEntry> entries = tokenData.typeToEntries.get(entryTypeName);
120     if (entryTypeName.overridesStopList) {
121       tokenData.hasMainEntry = true;
122     }
123     if (entries == null) {
124       entries = new ArrayList<IndexedEntry>();
125       tokenData.typeToEntries.put(entryTypeName, entries);
126     }
127     return entries;
128   }
129
130   public void addEntryWithTokens(final IndexedEntry indexedEntry, final Set<String> tokens,
131       final EntryTypeName entryTypeName) {
132     if (indexedEntry == null) {
133       System.out.println("asdfasdf");
134     }
135     assert indexedEntry != null;
136     for (final String token : tokens) {
137       if (entryTypeName.overridesStopList || !stoplist.contains(token)) {
138         getOrCreateEntries(token, entryTypeName).add(indexedEntry);
139       }
140     }    
141   }
142
143   public void addEntryWithString(final IndexedEntry indexedEntry, final String untokenizedString,
144       final EntryTypeName entryTypeName) {
145     final Set<String> tokens = DictFileParser.tokenize(untokenizedString, DictFileParser.NON_CHAR);
146     addEntryWithTokens(indexedEntry, tokens, tokens.size() == 1 ? entryTypeName.singleWordInstance : entryTypeName);
147   }
148
149   public void addEntryWithStringNoSingle(final IndexedEntry indexedEntry, final String untokenizedString,
150       final EntryTypeName entryTypeName) {
151     final Set<String> tokens = DictFileParser.tokenize(untokenizedString, DictFileParser.NON_CHAR);
152     addEntryWithTokens(indexedEntry, tokens, entryTypeName);
153   }
154 }