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