]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/PairEntry.java
d3fb060e1dc0570a3d02a326e0283ac8a58382cd
[Dictionary.git] / src / com / hughes / android / dictionary / engine / PairEntry.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 com.hughes.util.StringUtil;
18 import com.hughes.util.raf.RAFListSerializer;
19 import com.hughes.util.raf.RAFListSerializerSkippable;
20 import com.hughes.util.raf.RAFSerializable;
21 import com.ibm.icu.text.Transliterator;
22
23 import java.io.DataInput;
24 import java.io.DataOutput;
25 import java.io.IOException;
26 import java.io.PrintStream;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.regex.Pattern;
30
31 public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntry>,
32     Comparable<PairEntry> {
33
34     public final List<Pair> pairs;
35
36     public PairEntry(final EntrySource entrySource) {
37         super(entrySource);
38         pairs = new ArrayList<Pair>(1);
39     }
40
41     public PairEntry(final EntrySource entrySource, final String lang1, final String lang2) {
42         this(entrySource);
43         this.pairs.add(new Pair(lang1, lang2));
44     }
45
46     public PairEntry(final Dictionary dictionary, final DataInput raf, final int index)
47     throws IOException {
48         super(dictionary, raf, index);
49         final int size = dictionary.dictFileVersion >= 7 ? StringUtil.readVarInt(raf) : raf.readInt();
50         pairs = new ArrayList<PairEntry.Pair>(size);
51         for (int i = 0; i < size; ++i) {
52             pairs.add(new Pair(raf.readUTF(), raf.readUTF()));
53         }
54     }
55
56     @Override
57     public void write(DataOutput raf) throws IOException {
58         super.write(raf);
59         StringUtil.writeVarInt(raf, pairs.size());
60         for (int i = 0; i < pairs.size(); ++i) {
61             assert pairs.get(i).lang1.length() > 0;
62             raf.writeUTF(pairs.get(i).lang1);
63             raf.writeUTF(pairs.get(i).lang2);
64         }
65     }
66
67     static final class Serializer implements RAFListSerializerSkippable<PairEntry> {
68
69         final Dictionary dictionary;
70
71         Serializer(Dictionary dictionary) {
72             this.dictionary = dictionary;
73         }
74
75         @Override
76         public PairEntry read(DataInput raf, int index) throws IOException {
77             return new PairEntry(dictionary, raf, index);
78         }
79
80         @Override
81         public void skip(DataInput raf, int index) throws IOException {
82             final int size;
83             if (dictionary.dictFileVersion >= 7)
84             {
85                 StringUtil.readVarInt(raf);
86                 size = StringUtil.readVarInt(raf);
87             }
88             else
89             {
90                 raf.skipBytes(2);
91                 size = raf.readInt();
92             }
93             for (int i = 0; i < 2*size; ++i) {
94                 int l = raf.readUnsignedShort();
95                 raf.skipBytes(l);
96             }
97         }
98
99         @Override
100         public void write(DataOutput raf, PairEntry t) throws IOException {
101             t.write(raf);
102         }
103     }
104
105     @Override
106     public void addToDictionary(final Dictionary dictionary) {
107         assert index == -1;
108         dictionary.pairEntries.add(this);
109         index = dictionary.pairEntries.size() - 1;
110     }
111
112     @Override
113     public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
114         return new Row(this.index, rowIndex, dictionaryIndex);
115     }
116
117     // --------------------------------------------------------------------
118
119     public static class Row extends RowBase {
120
121         Row(final DataInput raf, final int thisRowIndex,
122             final Index index, int extra) throws IOException {
123             super(raf, thisRowIndex, index, extra);
124         }
125
126         Row(final int referenceIndex, final int thisRowIndex,
127             final Index index) {
128             super(referenceIndex, thisRowIndex, index);
129         }
130
131         @Override
132         public String toString() {
133             return getRawText(false);
134         }
135
136         public PairEntry getEntry() {
137             return index.dict.pairEntries.get(referenceIndex);
138         }
139
140         @Override
141         public void print(PrintStream out) {
142             final PairEntry pairEntry = getEntry();
143             for (int i = 0; i < pairEntry.pairs.size(); ++i) {
144                 out.print((i == 0 ? "  " : "    ") + pairEntry.pairs.get(i));
145                 out.println();
146             }
147         }
148
149         @Override
150         public String getRawText(boolean compact) {
151             final PairEntry pairEntry = getEntry();
152             return pairEntry.getRawText(compact);
153         }
154
155         @Override
156         public RowMatchType matches(final List<String> searchTokens,
157                                     final Pattern orderedMatchPattern, final Transliterator normalizer,
158                                     final boolean swapPairEntries) {
159             final int side = swapPairEntries ? 1 : 0;
160             final List<Pair> pairs = getEntry().pairs;
161             final String[] pairSides = new String[pairs.size()];
162             for (int i = 0; i < pairs.size(); ++i) {
163                 pairSides[i] = normalizer.transform(pairs.get(i).get(side));
164             }
165             for (int i = searchTokens.size() - 1; i >= 0; --i) {
166                 final String searchToken = searchTokens.get(i);
167                 boolean found = false;
168                 for (final String pairSide : pairSides) {
169                     found |= pairSide.contains(searchToken);
170                 }
171                 if (!found) {
172                     return RowMatchType.NO_MATCH;
173                 }
174             }
175             for (final String pairSide : pairSides) {
176                 if (orderedMatchPattern.matcher(pairSide).find()) {
177                     return RowMatchType.ORDERED_MATCH;
178                 }
179             }
180             return RowMatchType.BAG_OF_WORDS_MATCH;
181         }
182
183         @Override
184         public int getSideLength(boolean swapPairEntries) {
185             int result = 0;
186             final int side = swapPairEntries ? 1 : 0;
187             for (final Pair pair : getEntry().pairs) {
188                 result += pair.get(side).length();
189             }
190             return result;
191         }
192
193     }
194
195     public String getRawText(final boolean compact) {
196         if (compact) {
197             return this.pairs.get(0).toStringTab();
198         }
199         final StringBuilder builder = new StringBuilder();
200         for (int i = 0; i < this.pairs.size(); ++i) {
201             if (i > 0) {
202                 builder.append(" | ");
203             }
204             builder.append(this.pairs.get(i).lang1);
205         }
206         builder.append("\t");
207         for (int i = 0; i < this.pairs.size(); ++i) {
208             if (i > 0) {
209                 builder.append(" | ");
210             }
211             builder.append(this.pairs.get(i).lang2);
212         }
213         return builder.toString();
214     }
215
216     @Override
217     public int compareTo(final PairEntry that) {
218         return this.getRawText(false).compareTo(that.getRawText(false));
219     }
220
221     @Override
222     public String toString() {
223         return getRawText(false);
224     }
225
226     // -----------------------------------------------------------------------
227
228     public static final class Pair {
229
230         public final String lang1;
231         public final String lang2;
232
233         public Pair(final String lang1, final String lang2) {
234             this.lang1 = lang1;
235             this.lang2 = lang2;
236             assert lang1.trim().length() > 0 && lang2.trim().length() > 0 : "Empty pair!!!";
237         }
238
239         public Pair(final String lang1, final String lang2, final boolean swap) {
240             this(swap ? lang2 : lang1, swap ? lang1 : lang2);
241         }
242
243         public String toString() {
244             return lang1 + " :: " + lang2;
245         }
246
247         public String toStringTab() {
248             return lang1 + "\t" + lang2;
249         }
250
251         public String get(int i) {
252             if (i == 0) {
253                 return lang1;
254             } else if (i == 1) {
255                 return lang2;
256             }
257             throw new IllegalArgumentException();
258         }
259
260         @Override
261         public boolean equals(Object o)
262         {
263             if (o == this) return true;
264             if (!(o instanceof Pair)) return false;
265             Pair p = (Pair)o;
266             return p.lang1.equals(lang1) && p.lang2.equals(lang2);
267         }
268
269         @Override
270         public int hashCode()
271         {
272             return (lang1 + "|" + lang2).hashCode();
273         }
274     }
275 }