]> gitweb.fperrin.net Git - DictionaryPC.git/blob - src/com/hughes/android/dictionary/engine/IndexBuilder.java
Moved around testdata.
[DictionaryPC.git] / src / com / hughes / android / dictionary / engine / IndexBuilder.java
1 package com.hughes.android.dictionary.engine;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.EnumMap;
7 import java.util.HashSet;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Set;
11 import java.util.SortedMap;
12 import java.util.TreeMap;
13
14 import com.hughes.android.dictionary.engine.Index.IndexEntry;
15
16
17 public class IndexBuilder {
18   
19   final DictionaryBuilder dictionaryBuilder;
20   final Index index;
21
22   final SortedMap<String, TokenData> tokenToData;
23
24   @SuppressWarnings("unchecked")
25   IndexBuilder(final DictionaryBuilder dictionaryBuilder, final String shortName, final String longName, final Language language, final boolean swapPairEntries) {
26     this.dictionaryBuilder = dictionaryBuilder;
27     index = new Index(dictionaryBuilder.dictionary, shortName, longName, language, swapPairEntries);
28     tokenToData = new TreeMap<String, TokenData>(language.getSortCollator());
29   }
30   
31   public void build() {
32     final Set<EntryData> tokenEntryDatas = new HashSet<EntryData>();
33     final List<RowBase> rows = index.rows;
34     for (final TokenData tokenData : tokenToData.values()) {
35       tokenEntryDatas.clear();
36       final int indexIndex = index.sortedIndexEntries.size();
37       final int startRow = rows.size();
38       rows.add(new TokenRow(indexIndex, rows.size(), index));
39 //      System.out.println("Added TokenRow: " + rows.get(rows.size() - 1));
40       int numRows = 0;
41 //      System.out.println("TOKEN: " + tokenData.token);
42       for (final Map.Entry<EntryTypeName, List<EntryData>> typeToEntry : tokenData.typeToEntries.entrySet()) {
43         for (final EntryData entryData : typeToEntry.getValue()) {
44           if (tokenEntryDatas.add(entryData)) {
45             rows.add(new PairEntry.Row(entryData.index(), rows.size(), index));
46             ++numRows;
47             
48 //            System.out.print("  " + typeToEntry.getKey() + ": ");
49   //          rows.get(rows.size() - 1).print(System.out);
50 //            System.out.println();
51           }
52         }
53       }
54       index.sortedIndexEntries.add(new Index.IndexEntry(tokenData.token, startRow, numRows));
55     }
56     
57     final List<IndexEntry> sortedEntries = new ArrayList<IndexEntry>(index.sortedIndexEntries);
58     Collections.sort(sortedEntries, new Comparator<IndexEntry>() {
59       @Override
60       public int compare(IndexEntry object1, IndexEntry object2) {
61         return object2.numRows - object1.numRows;
62       }});
63     System.out.println("Most common tokens:");
64     for (int i = 0; i < 50 && i < sortedEntries.size(); ++i) {
65       System.out.println("  " + sortedEntries.get(i));
66     }
67   }
68   
69   static class TokenData {
70     final String token;
71         
72     final Map<EntryTypeName, List<EntryData>> typeToEntries = new EnumMap<EntryTypeName, List<EntryData>>(EntryTypeName.class);
73     
74     TokenData(final String token) {
75       assert token.equals(token.trim());
76       assert token.length() > 0;
77       this.token = token;
78     }
79   }
80
81   public TokenData getOrCreateTokenData(final String token) {
82     TokenData tokenData = tokenToData.get(token);
83     if (tokenData == null) {
84       tokenData = new TokenData(token);
85       tokenToData.put(token, tokenData);
86     }
87     return tokenData;
88   }
89
90   public List<EntryData> getOrCreateEntries(final String token, final EntryTypeName entryTypeName) {
91     final TokenData tokenData = getOrCreateTokenData(token);
92     List<EntryData> entries = tokenData.typeToEntries.get(entryTypeName);
93     if (entries == null) {
94       entries = new ArrayList<EntryData>();
95       tokenData.typeToEntries.put(entryTypeName, entries);
96     }
97     return entries;
98   }
99   
100
101 }