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;
36 final SortedMap<String, TokenData> tokenToData;
38 IndexBuilder(final DictionaryBuilder dictionaryBuilder, final String shortName, final String longName, final Language language, final String normalizerRules, final boolean swapPairEntries) {
39 this.dictionaryBuilder = dictionaryBuilder;
40 index = new Index(dictionaryBuilder.dictionary, shortName, longName, language, normalizerRules, swapPairEntries);
41 tokenToData = new TreeMap<String, TokenData>(new NormalizeComparator(index.normalizer(), language.getCollator()));
45 final Set<IndexedEntry> tokenEntryDatas = new HashSet<IndexedEntry>();
46 final List<RowBase> rows = index.rows;
47 for (final TokenData tokenData : tokenToData.values()) {
48 tokenEntryDatas.clear();
49 final int indexIndex = index.sortedIndexEntries.size();
50 final int startRow = rows.size();
51 rows.add(new TokenRow(indexIndex, rows.size(), index));
52 // System.out.println("Added TokenRow: " + rows.get(rows.size() - 1));
54 // System.out.println("TOKEN: " + tokenData.token);
55 for (final Map.Entry<EntryTypeName, List<IndexedEntry>> typeToEntry : tokenData.typeToEntries.entrySet()) {
56 for (final IndexedEntry entryData : typeToEntry.getValue()) {
57 if (entryData.index() == -1) {
58 entryData.addToDictionary(dictionaryBuilder.dictionary);
59 assert entryData.index() >= 0;
61 if (tokenEntryDatas.add(entryData)) {
62 rows.add(new PairEntry.Row(entryData.index(), rows.size(), index));
65 // System.out.print(" " + typeToEntry.getKey() + ": ");
66 // rows.get(rows.size() - 1).print(System.out);
67 // System.out.println();
71 index.sortedIndexEntries.add(new Index.IndexEntry(tokenData.token, index
72 .normalizer().transliterate(tokenData.token), startRow, numRows));
75 final List<IndexEntry> sortedEntries = new ArrayList<IndexEntry>(index.sortedIndexEntries);
76 Collections.sort(sortedEntries, new Comparator<IndexEntry>() {
78 public int compare(IndexEntry object1, IndexEntry object2) {
79 return object2.numRows - object1.numRows;
81 System.out.println("Most common tokens:");
82 for (int i = 0; i < 50 && i < sortedEntries.size(); ++i) {
83 System.out.println(" " + sortedEntries.get(i));
87 static class TokenData {
90 final Map<EntryTypeName, List<IndexedEntry>> typeToEntries = new EnumMap<EntryTypeName, List<IndexedEntry>>(EntryTypeName.class);
92 TokenData(final String token) {
93 assert token.equals(token.trim());
94 assert token.length() > 0;
99 public TokenData getOrCreateTokenData(final String token) {
100 TokenData tokenData = tokenToData.get(token);
101 if (tokenData == null) {
102 tokenData = new TokenData(token);
103 tokenToData.put(token, tokenData);
108 public List<IndexedEntry> getOrCreateEntries(final String token, final EntryTypeName entryTypeName) {
109 final TokenData tokenData = getOrCreateTokenData(token);
110 List<IndexedEntry> entries = tokenData.typeToEntries.get(entryTypeName);
111 if (entries == null) {
112 entries = new ArrayList<IndexedEntry>();
113 tokenData.typeToEntries.put(entryTypeName, entries);
118 public void addEntryWithTokens(final IndexedEntry indexedEntry, final Set<String> tokens,
119 final EntryTypeName entryTypeName) {
120 for (final String token : tokens) {
121 getOrCreateEntries(token, entryTypeName).add(indexedEntry);
125 public void addEntryWithString(final IndexedEntry indexedEntry, final String untokenizedString,
126 final EntryTypeName singleTokenEntryTypeName, final EntryTypeName multiTokenEntryTypeName) {
127 final Set<String> tokens = DictFileParser.tokenize(untokenizedString, DictFileParser.NON_CHAR);
128 addEntryWithTokens(indexedEntry, tokens, tokens.size() == 1 ? singleTokenEntryTypeName : multiTokenEntryTypeName);
131 public void addEntryWithString(final IndexedEntry indexedEntry, final String untokenizedString,
132 final EntryTypeName entryTypeName) {
133 addEntryWithString(indexedEntry, untokenizedString, entryTypeName, entryTypeName);