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 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;
26 import com.hughes.util.StringUtil;
27 import com.hughes.util.raf.RAFListSerializerSkippable;
28 import com.ibm.icu.text.Transliterator;
30 public class PairEntry extends AbstractEntry implements Comparable<PairEntry> {
32 public final List<Pair> pairs;
34 public PairEntry(final EntrySource entrySource) {
36 pairs = new ArrayList<>(1);
39 public PairEntry(final EntrySource entrySource, final String lang1, final String lang2) {
41 this.pairs.add(new Pair(lang1, lang2));
44 public PairEntry(final Dictionary dictionary, final DataInput raf, final int index)
46 super(dictionary, raf, index);
47 final int size = dictionary.dictFileVersion >= 7 ? StringUtil.readVarInt(raf) : raf.readInt();
48 // Use singletonList for better performance in common case
49 if (size == 1) pairs = Collections.singletonList(new Pair(raf.readUTF(), raf.readUTF()));
52 pairs = new ArrayList<>(size);
53 for (int i = 0; i < size; ++i) {
54 pairs.add(new Pair(raf.readUTF(), raf.readUTF()));
60 public void write(DataOutput raf) throws IOException {
62 StringUtil.writeVarInt(raf, pairs.size());
63 for (Pair p : pairs) {
64 assert p.lang1.length() > 0;
65 raf.writeUTF(p.lang1);
66 raf.writeUTF(p.lang2);
70 static final class Serializer implements RAFListSerializerSkippable<PairEntry> {
72 final Dictionary dictionary;
74 Serializer(Dictionary dictionary) {
75 this.dictionary = dictionary;
79 public PairEntry read(DataInput raf, int index) throws IOException {
80 return new PairEntry(dictionary, raf, index);
84 public void skip(DataInput raf, int index) throws IOException {
86 if (dictionary.dictFileVersion >= 7)
88 StringUtil.readVarInt(raf);
89 size = StringUtil.readVarInt(raf);
96 for (int i = 0; i < 2*size; ++i) {
97 int l = raf.readUnsignedShort();
103 public void write(DataOutput raf, PairEntry t) throws IOException {
109 public void addToDictionary(final Dictionary dictionary) {
111 dictionary.pairEntries.add(this);
112 index = dictionary.pairEntries.size() - 1;
116 public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
117 return new Row(this.index, rowIndex, dictionaryIndex);
120 // --------------------------------------------------------------------
122 public static class Row extends RowBase {
124 Row(final DataInput raf, final int thisRowIndex,
125 final Index index, int extra) throws IOException {
126 super(raf, thisRowIndex, index, extra);
129 Row(final int referenceIndex, final int thisRowIndex,
131 super(referenceIndex, thisRowIndex, index);
135 public String toString() {
136 return getRawText(false);
139 public PairEntry getEntry() {
140 return index.dict.pairEntries.get(referenceIndex);
144 public void print(PrintStream out) {
145 final PairEntry pairEntry = getEntry();
146 for (int i = 0; i < pairEntry.pairs.size(); ++i) {
147 out.print((i == 0 ? " " : " ") + pairEntry.pairs.get(i));
153 public String getRawText(boolean compact) {
154 final PairEntry pairEntry = getEntry();
155 return pairEntry.getRawText(compact);
159 public RowMatchType matches(final List<String> searchTokens,
160 final Pattern orderedMatchPattern, final Transliterator normalizer,
161 final boolean swapPairEntries) {
162 final int side = swapPairEntries ? 1 : 0;
163 final List<Pair> pairs = getEntry().pairs;
164 final String[] pairSides = new String[pairs.size()];
165 for (int i = 0; i < pairs.size(); ++i) {
166 pairSides[i] = normalizer.transform(pairs.get(i).get(side));
168 for (int i = searchTokens.size() - 1; i >= 0; --i) {
169 final String searchToken = searchTokens.get(i);
170 boolean found = false;
171 for (final String pairSide : pairSides) {
172 found |= pairSide.contains(searchToken);
175 return RowMatchType.NO_MATCH;
178 for (final String pairSide : pairSides) {
179 if (orderedMatchPattern.matcher(pairSide).find()) {
180 return RowMatchType.ORDERED_MATCH;
183 return RowMatchType.BAG_OF_WORDS_MATCH;
187 public int getSideLength(boolean swapPairEntries) {
189 final int side = swapPairEntries ? 1 : 0;
190 for (final Pair pair : getEntry().pairs) {
191 result += pair.get(side).length();
198 private String getRawText(final boolean compact) {
200 return this.pairs.get(0).toStringTab();
202 final StringBuilder builder = new StringBuilder();
203 for (int i = 0; i < this.pairs.size(); ++i) {
205 builder.append(" | ");
207 builder.append(this.pairs.get(i).lang1);
209 builder.append("\t");
210 for (int i = 0; i < this.pairs.size(); ++i) {
212 builder.append(" | ");
214 builder.append(this.pairs.get(i).lang2);
216 return builder.toString();
220 public int compareTo(/*@NonNull*/ final PairEntry that) {
221 return this.getRawText(false).compareTo(that.getRawText(false));
225 public String toString() {
226 return getRawText(false);
229 // -----------------------------------------------------------------------
231 public static final class Pair {
233 public final String lang1;
234 public final String lang2;
236 @SuppressWarnings("WeakerAccess")
237 public Pair(final String lang1, final String lang2) {
240 assert lang1.trim().length() > 0 && lang2.trim().length() > 0 : "Empty pair!!!";
243 public Pair(final String lang1, final String lang2, final boolean swap) {
244 this(swap ? lang2 : lang1, swap ? lang1 : lang2);
247 public String toString() {
248 return lang1 + " :: " + lang2;
251 String toStringTab() {
252 return lang1 + "\t" + lang2;
255 public String get(int i) {
261 throw new IllegalArgumentException();
265 public boolean equals(Object o)
267 if (o == this) return true;
268 if (!(o instanceof Pair)) return false;
270 return p.lang1.equals(lang1) && p.lang2.equals(lang2);
274 public int hashCode()
276 return (lang1 + "|" + lang2).hashCode();