1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 package com.hughes.android.dictionary.engine;
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;
25 import java.util.SortedMap;
26 import java.util.TreeMap;
28 import com.hughes.android.dictionary.engine.Index.IndexEntry;
29 import com.hughes.android.dictionary.parser.DictFileParser;
31 public class IndexBuilder {
33 final DictionaryBuilder dictionaryBuilder;
34 public final Index index;
35 final Set<String> stoplist;
37 final SortedMap<String, TokenData> tokenToData;
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;
47 final Set<IndexedEntry> tokenEntryDatas = new HashSet<IndexedEntry>();
48 final List<RowBase> rows = index.rows;
49 for (final TokenData tokenData : tokenToData.values()) {
50 tokenEntryDatas.clear();
51 final int indexIndex = index.sortedIndexEntries.size();
52 final int startRow = rows.size();
54 rows.add(new TokenRow(indexIndex, rows.size(), index, tokenData.hasMainEntry));
55 // System.out.println("Added TokenRow: " + rows.get(rows.size() - 1));
57 // System.out.println("TOKEN: " + tokenData.token);
58 for (final Map.Entry<EntryTypeName, List<IndexedEntry>> typeToEntry : tokenData.typeToEntries.entrySet()) {
59 for (final IndexedEntry entryData : typeToEntry.getValue()) {
60 if (entryData.index() == -1) {
61 entryData.addToDictionary(dictionaryBuilder.dictionary);
62 assert entryData.index() >= 0;
64 if (tokenEntryDatas.add(entryData)) {
65 rows.add(new PairEntry.Row(entryData.index(), rows.size(), index));
68 // System.out.print(" " + typeToEntry.getKey() + ": ");
69 // rows.get(rows.size() - 1).print(System.out);
70 // System.out.println();
74 index.sortedIndexEntries.add(new Index.IndexEntry(tokenData.token, index
75 .normalizer().transliterate(tokenData.token), startRow, numRows));
78 final List<IndexEntry> entriesSortedByNumRows = new ArrayList<IndexEntry>(index.sortedIndexEntries);
79 Collections.sort(entriesSortedByNumRows, new Comparator<IndexEntry>() {
81 public int compare(IndexEntry object1, IndexEntry object2) {
82 return object2.numRows - object1.numRows;
84 System.out.println("Most common tokens:");
85 for (int i = 0; i < 50 && i < entriesSortedByNumRows.size(); ++i) {
86 System.out.println(" " + entriesSortedByNumRows.get(i));
90 static class TokenData {
93 final Map<EntryTypeName, List<IndexedEntry>> typeToEntries = new EnumMap<EntryTypeName, List<IndexedEntry>>(EntryTypeName.class);
94 boolean hasMainEntry = false;
96 TokenData(final String token) {
97 assert token.equals(token.trim());
98 assert token.length() > 0;
103 private TokenData getOrCreateTokenData(final String token) {
104 TokenData tokenData = tokenToData.get(token);
105 if (tokenData == null) {
106 tokenData = new TokenData(token);
107 tokenToData.put(token, tokenData);
112 private List<IndexedEntry> getOrCreateEntries(final String token, final EntryTypeName entryTypeName) {
113 final TokenData tokenData = getOrCreateTokenData(token);
114 List<IndexedEntry> entries = tokenData.typeToEntries.get(entryTypeName);
115 if (entryTypeName.overridesStopList) {
116 tokenData.hasMainEntry = true;
118 if (entries == null) {
119 entries = new ArrayList<IndexedEntry>();
120 tokenData.typeToEntries.put(entryTypeName, entries);
125 public void addEntryWithTokens(final IndexedEntry indexedEntry, final Set<String> tokens,
126 final EntryTypeName entryTypeName) {
127 if (indexedEntry == null) {
128 System.out.println("asdfasdf");
130 assert indexedEntry != null;
131 for (final String token : tokens) {
132 if (entryTypeName.overridesStopList || !stoplist.contains(token)) {
133 getOrCreateEntries(token, entryTypeName).add(indexedEntry);
138 public void addEntryWithString(final IndexedEntry indexedEntry, final String untokenizedString,
139 final EntryTypeName entryTypeName) {
140 final Set<String> tokens = DictFileParser.tokenize(untokenizedString, DictFileParser.NON_CHAR);
141 addEntryWithTokens(indexedEntry, tokens, tokens.size() == 1 ? entryTypeName.singleWordInstance : entryTypeName);
144 public void addEntryWithStringNoSingle(final IndexedEntry indexedEntry, final String untokenizedString,
145 final EntryTypeName entryTypeName) {
146 final Set<String> tokens = DictFileParser.tokenize(untokenizedString, DictFileParser.NON_CHAR);
147 addEntryWithTokens(indexedEntry, tokens, entryTypeName);