]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/IndexBuilder.java
Apache license.
[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
36   final SortedMap<String, TokenData> tokenToData;
37
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()));
42   }
43   
44   public void build() {
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));
53       int numRows = 0;
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;
60           }
61           if (tokenEntryDatas.add(entryData)) {
62             rows.add(new PairEntry.Row(entryData.index(), rows.size(), index));
63             ++numRows;
64             
65 //            System.out.print("  " + typeToEntry.getKey() + ": ");
66   //          rows.get(rows.size() - 1).print(System.out);
67 //            System.out.println();
68           }
69         }
70       }
71       index.sortedIndexEntries.add(new Index.IndexEntry(tokenData.token, index
72           .normalizer().transliterate(tokenData.token), startRow, numRows));
73     }
74     
75     final List<IndexEntry> sortedEntries = new ArrayList<IndexEntry>(index.sortedIndexEntries);
76     Collections.sort(sortedEntries, new Comparator<IndexEntry>() {
77       @Override
78       public int compare(IndexEntry object1, IndexEntry object2) {
79         return object2.numRows - object1.numRows;
80       }});
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));
84     }
85   }
86   
87   static class TokenData {
88     final String token;
89         
90     final Map<EntryTypeName, List<IndexedEntry>> typeToEntries = new EnumMap<EntryTypeName, List<IndexedEntry>>(EntryTypeName.class);
91     
92     TokenData(final String token) {
93       assert token.equals(token.trim());
94       assert token.length() > 0;
95       this.token = token;
96     }
97   }
98
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);
104     }
105     return tokenData;
106   }
107
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);
114     }
115     return entries;
116   }
117
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);
122     }    
123   }
124
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);
129   }
130
131   public void addEntryWithString(final IndexedEntry indexedEntry, final String untokenizedString,
132       final EntryTypeName entryTypeName) {
133     addEntryWithString(indexedEntry, untokenizedString, entryTypeName, entryTypeName);
134   }
135 }