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.hughes.util.raf.RAFSerializable;
29 import com.ibm.icu.text.Transliterator;
31 public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntry>,
32 Comparable<PairEntry> {
34 public final List<Pair> pairs;
36 public PairEntry(final EntrySource entrySource) {
38 pairs = new ArrayList<>(1);
41 public PairEntry(final EntrySource entrySource, final String lang1, final String lang2) {
43 this.pairs.add(new Pair(lang1, lang2));
46 public PairEntry(final Dictionary dictionary, final DataInput raf, final int index)
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()));
54 pairs = new ArrayList<>(size);
55 for (int i = 0; i < size; ++i) {
56 pairs.add(new Pair(raf.readUTF(), raf.readUTF()));
62 public void write(DataOutput raf) throws IOException {
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);
72 static final class Serializer implements RAFListSerializerSkippable<PairEntry> {
74 final Dictionary dictionary;
76 Serializer(Dictionary dictionary) {
77 this.dictionary = dictionary;
81 public PairEntry read(DataInput raf, int index) throws IOException {
82 return new PairEntry(dictionary, raf, index);
86 public void skip(DataInput raf, int index) throws IOException {
88 if (dictionary.dictFileVersion >= 7)
90 StringUtil.readVarInt(raf);
91 size = StringUtil.readVarInt(raf);
98 for (int i = 0; i < 2*size; ++i) {
99 int l = raf.readUnsignedShort();
105 public void write(DataOutput raf, PairEntry t) throws IOException {
111 public void addToDictionary(final Dictionary dictionary) {
113 dictionary.pairEntries.add(this);
114 index = dictionary.pairEntries.size() - 1;
118 public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
119 return new Row(this.index, rowIndex, dictionaryIndex);
122 // --------------------------------------------------------------------
124 public static class Row extends RowBase {
126 Row(final DataInput raf, final int thisRowIndex,
127 final Index index, int extra) throws IOException {
128 super(raf, thisRowIndex, index, extra);
131 Row(final int referenceIndex, final int thisRowIndex,
133 super(referenceIndex, thisRowIndex, index);
137 public String toString() {
138 return getRawText(false);
141 public PairEntry getEntry() {
142 return index.dict.pairEntries.get(referenceIndex);
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));
155 public String getRawText(boolean compact) {
156 final PairEntry pairEntry = getEntry();
157 return pairEntry.getRawText(compact);
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));
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);
177 return RowMatchType.NO_MATCH;
180 for (final String pairSide : pairSides) {
181 if (orderedMatchPattern.matcher(pairSide).find()) {
182 return RowMatchType.ORDERED_MATCH;
185 return RowMatchType.BAG_OF_WORDS_MATCH;
189 public int getSideLength(boolean swapPairEntries) {
191 final int side = swapPairEntries ? 1 : 0;
192 for (final Pair pair : getEntry().pairs) {
193 result += pair.get(side).length();
200 private String getRawText(final boolean compact) {
202 return this.pairs.get(0).toStringTab();
204 final StringBuilder builder = new StringBuilder();
205 for (int i = 0; i < this.pairs.size(); ++i) {
207 builder.append(" | ");
209 builder.append(this.pairs.get(i).lang1);
211 builder.append("\t");
212 for (int i = 0; i < this.pairs.size(); ++i) {
214 builder.append(" | ");
216 builder.append(this.pairs.get(i).lang2);
218 return builder.toString();
222 public int compareTo(/*@NonNull*/ final PairEntry that) {
223 return this.getRawText(false).compareTo(that.getRawText(false));
227 public String toString() {
228 return getRawText(false);
231 // -----------------------------------------------------------------------
233 public static final class Pair {
235 public final String lang1;
236 public final String lang2;
238 @SuppressWarnings("WeakerAccess")
239 public Pair(final String lang1, final String lang2) {
242 assert lang1.trim().length() > 0 && lang2.trim().length() > 0 : "Empty pair!!!";
245 public Pair(final String lang1, final String lang2, final boolean swap) {
246 this(swap ? lang2 : lang1, swap ? lang1 : lang2);
249 public String toString() {
250 return lang1 + " :: " + lang2;
253 String toStringTab() {
254 return lang1 + "\t" + lang2;
257 public String get(int i) {
263 throw new IllegalArgumentException();
267 public boolean equals(Object o)
269 if (o == this) return true;
270 if (!(o instanceof Pair)) return false;
272 return p.lang1.equals(lang1) && p.lang2.equals(lang2);
276 public int hashCode()
278 return (lang1 + "|" + lang2).hashCode();