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