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 com.hughes.util.StringUtil;
18 import com.hughes.util.raf.RAFListSerializer;
19 import com.hughes.util.raf.RAFListSerializerSkippable;
20 import com.hughes.util.raf.RAFSerializable;
21 import com.ibm.icu.text.Transliterator;
23 import java.io.DataInput;
24 import java.io.DataOutput;
25 import java.io.IOException;
26 import java.io.PrintStream;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.regex.Pattern;
32 public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntry>,
33 Comparable<PairEntry> {
35 public final List<Pair> pairs;
37 public PairEntry(final EntrySource entrySource) {
39 pairs = new ArrayList<Pair>(1);
42 public PairEntry(final EntrySource entrySource, final String lang1, final String lang2) {
44 this.pairs.add(new Pair(lang1, lang2));
47 public PairEntry(final Dictionary dictionary, final DataInput raf, final int index)
49 super(dictionary, raf, index);
50 final int size = dictionary.dictFileVersion >= 7 ? StringUtil.readVarInt(raf) : raf.readInt();
51 // Use singletonList for better performance in common case
52 if (size == 1) pairs = Collections.singletonList(new Pair(raf.readUTF(), raf.readUTF()));
55 pairs = new ArrayList<PairEntry.Pair>(size);
56 for (int i = 0; i < size; ++i) {
57 pairs.add(new Pair(raf.readUTF(), raf.readUTF()));
63 public void write(DataOutput raf) throws IOException {
65 StringUtil.writeVarInt(raf, pairs.size());
66 for (int i = 0; i < pairs.size(); ++i) {
67 assert pairs.get(i).lang1.length() > 0;
68 raf.writeUTF(pairs.get(i).lang1);
69 raf.writeUTF(pairs.get(i).lang2);
73 static final class Serializer implements RAFListSerializerSkippable<PairEntry> {
75 final Dictionary dictionary;
77 Serializer(Dictionary dictionary) {
78 this.dictionary = dictionary;
82 public PairEntry read(DataInput raf, int index) throws IOException {
83 return new PairEntry(dictionary, raf, index);
87 public void skip(DataInput raf, int index) throws IOException {
89 if (dictionary.dictFileVersion >= 7)
91 StringUtil.readVarInt(raf);
92 size = StringUtil.readVarInt(raf);
99 for (int i = 0; i < 2*size; ++i) {
100 int l = raf.readUnsignedShort();
106 public void write(DataOutput raf, PairEntry t) throws IOException {
112 public void addToDictionary(final Dictionary dictionary) {
114 dictionary.pairEntries.add(this);
115 index = dictionary.pairEntries.size() - 1;
119 public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
120 return new Row(this.index, rowIndex, dictionaryIndex);
123 // --------------------------------------------------------------------
125 public static class Row extends RowBase {
127 Row(final DataInput raf, final int thisRowIndex,
128 final Index index, int extra) throws IOException {
129 super(raf, thisRowIndex, index, extra);
132 Row(final int referenceIndex, final int thisRowIndex,
134 super(referenceIndex, thisRowIndex, index);
138 public String toString() {
139 return getRawText(false);
142 public PairEntry getEntry() {
143 return index.dict.pairEntries.get(referenceIndex);
147 public void print(PrintStream out) {
148 final PairEntry pairEntry = getEntry();
149 for (int i = 0; i < pairEntry.pairs.size(); ++i) {
150 out.print((i == 0 ? " " : " ") + pairEntry.pairs.get(i));
156 public String getRawText(boolean compact) {
157 final PairEntry pairEntry = getEntry();
158 return pairEntry.getRawText(compact);
162 public RowMatchType matches(final List<String> searchTokens,
163 final Pattern orderedMatchPattern, final Transliterator normalizer,
164 final boolean swapPairEntries) {
165 final int side = swapPairEntries ? 1 : 0;
166 final List<Pair> pairs = getEntry().pairs;
167 final String[] pairSides = new String[pairs.size()];
168 for (int i = 0; i < pairs.size(); ++i) {
169 pairSides[i] = normalizer.transform(pairs.get(i).get(side));
171 for (int i = searchTokens.size() - 1; i >= 0; --i) {
172 final String searchToken = searchTokens.get(i);
173 boolean found = false;
174 for (final String pairSide : pairSides) {
175 found |= pairSide.contains(searchToken);
178 return RowMatchType.NO_MATCH;
181 for (final String pairSide : pairSides) {
182 if (orderedMatchPattern.matcher(pairSide).find()) {
183 return RowMatchType.ORDERED_MATCH;
186 return RowMatchType.BAG_OF_WORDS_MATCH;
190 public int getSideLength(boolean swapPairEntries) {
192 final int side = swapPairEntries ? 1 : 0;
193 for (final Pair pair : getEntry().pairs) {
194 result += pair.get(side).length();
201 public String getRawText(final boolean compact) {
203 return this.pairs.get(0).toStringTab();
205 final StringBuilder builder = new StringBuilder();
206 for (int i = 0; i < this.pairs.size(); ++i) {
208 builder.append(" | ");
210 builder.append(this.pairs.get(i).lang1);
212 builder.append("\t");
213 for (int i = 0; i < this.pairs.size(); ++i) {
215 builder.append(" | ");
217 builder.append(this.pairs.get(i).lang2);
219 return builder.toString();
223 public int compareTo(final PairEntry that) {
224 return this.getRawText(false).compareTo(that.getRawText(false));
228 public String toString() {
229 return getRawText(false);
232 // -----------------------------------------------------------------------
234 public static final class Pair {
236 public final String lang1;
237 public final String lang2;
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 public 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();