1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 package com.hughes.android.dictionary.engine;
17 import android.support.annotation.NonNull;
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;
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;
33 public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntry>,
34 Comparable<PairEntry> {
36 public final List<Pair> pairs;
38 public PairEntry(final EntrySource entrySource) {
40 pairs = new ArrayList<>(1);
43 public PairEntry(final EntrySource entrySource, final String lang1, final String lang2) {
45 this.pairs.add(new Pair(lang1, lang2));
48 public PairEntry(final Dictionary dictionary, final DataInput raf, final int index)
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()));
56 pairs = new ArrayList<>(size);
57 for (int i = 0; i < size; ++i) {
58 pairs.add(new Pair(raf.readUTF(), raf.readUTF()));
64 public void write(DataOutput raf) throws IOException {
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);
74 static final class Serializer implements RAFListSerializerSkippable<PairEntry> {
76 final Dictionary dictionary;
78 Serializer(Dictionary dictionary) {
79 this.dictionary = dictionary;
83 public PairEntry read(DataInput raf, int index) throws IOException {
84 return new PairEntry(dictionary, raf, index);
88 public void skip(DataInput raf, int index) throws IOException {
90 if (dictionary.dictFileVersion >= 7)
92 StringUtil.readVarInt(raf);
93 size = StringUtil.readVarInt(raf);
100 for (int i = 0; i < 2*size; ++i) {
101 int l = raf.readUnsignedShort();
107 public void write(DataOutput raf, PairEntry t) throws IOException {
113 public void addToDictionary(final Dictionary dictionary) {
115 dictionary.pairEntries.add(this);
116 index = dictionary.pairEntries.size() - 1;
120 public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
121 return new Row(this.index, rowIndex, dictionaryIndex);
124 // --------------------------------------------------------------------
126 public static class Row extends RowBase {
128 Row(final DataInput raf, final int thisRowIndex,
129 final Index index, int extra) throws IOException {
130 super(raf, thisRowIndex, index, extra);
133 Row(final int referenceIndex, final int thisRowIndex,
135 super(referenceIndex, thisRowIndex, index);
139 public String toString() {
140 return getRawText(false);
143 public PairEntry getEntry() {
144 return index.dict.pairEntries.get(referenceIndex);
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));
157 public String getRawText(boolean compact) {
158 final PairEntry pairEntry = getEntry();
159 return pairEntry.getRawText(compact);
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));
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);
179 return RowMatchType.NO_MATCH;
182 for (final String pairSide : pairSides) {
183 if (orderedMatchPattern.matcher(pairSide).find()) {
184 return RowMatchType.ORDERED_MATCH;
187 return RowMatchType.BAG_OF_WORDS_MATCH;
191 public int getSideLength(boolean swapPairEntries) {
193 final int side = swapPairEntries ? 1 : 0;
194 for (final Pair pair : getEntry().pairs) {
195 result += pair.get(side).length();
202 private String getRawText(final boolean compact) {
204 return this.pairs.get(0).toStringTab();
206 final StringBuilder builder = new StringBuilder();
207 for (int i = 0; i < this.pairs.size(); ++i) {
209 builder.append(" | ");
211 builder.append(this.pairs.get(i).lang1);
213 builder.append("\t");
214 for (int i = 0; i < this.pairs.size(); ++i) {
216 builder.append(" | ");
218 builder.append(this.pairs.get(i).lang2);
220 return builder.toString();
224 public int compareTo(@NonNull final PairEntry that) {
225 return this.getRawText(false).compareTo(that.getRawText(false));
229 public String toString() {
230 return getRawText(false);
233 // -----------------------------------------------------------------------
235 public static final class Pair {
237 public final String lang1;
238 public final String lang2;
240 @SuppressWarnings("WeakerAccess")
241 public Pair(final String lang1, final String lang2) {
244 assert lang1.trim().length() > 0 && lang2.trim().length() > 0 : "Empty pair!!!";
247 public Pair(final String lang1, final String lang2, final boolean swap) {
248 this(swap ? lang2 : lang1, swap ? lang1 : lang2);
251 public String toString() {
252 return lang1 + " :: " + lang2;
255 String toStringTab() {
256 return lang1 + "\t" + lang2;
259 public String get(int i) {
265 throw new IllegalArgumentException();
269 public boolean equals(Object o)
271 if (o == this) return true;
272 if (!(o instanceof Pair)) return false;
274 return p.lang1.equals(lang1) && p.lang2.equals(lang2);
278 public int hashCode()
280 return (lang1 + "|" + lang2).hashCode();