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.List;
29 import java.util.regex.Pattern;
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<Pair>(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 pairs = new ArrayList<PairEntry.Pair>(size);
51 for (int i = 0; i < size; ++i) {
52 pairs.add(new Pair(raf.readUTF(), raf.readUTF()));
57 public void write(DataOutput raf) throws IOException {
59 StringUtil.writeVarInt(raf, pairs.size());
60 for (int i = 0; i < pairs.size(); ++i) {
61 assert pairs.get(i).lang1.length() > 0;
62 raf.writeUTF(pairs.get(i).lang1);
63 raf.writeUTF(pairs.get(i).lang2);
67 static final class Serializer implements RAFListSerializerSkippable<PairEntry> {
69 final Dictionary dictionary;
71 Serializer(Dictionary dictionary) {
72 this.dictionary = dictionary;
76 public PairEntry read(DataInput raf, int index) throws IOException {
77 return new PairEntry(dictionary, raf, index);
81 public void skip(DataInput raf, int index) throws IOException {
83 if (dictionary.dictFileVersion >= 7)
85 StringUtil.readVarInt(raf);
86 size = StringUtil.readVarInt(raf);
93 for (int i = 0; i < 2*size; ++i) {
94 int l = raf.readUnsignedShort();
100 public void write(DataOutput raf, PairEntry t) throws IOException {
106 public void addToDictionary(final Dictionary dictionary) {
108 dictionary.pairEntries.add(this);
109 index = dictionary.pairEntries.size() - 1;
113 public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
114 return new Row(this.index, rowIndex, dictionaryIndex);
117 // --------------------------------------------------------------------
119 public static class Row extends RowBase {
121 Row(final DataInput raf, final int thisRowIndex,
122 final Index index, int extra) throws IOException {
123 super(raf, thisRowIndex, index, extra);
126 Row(final int referenceIndex, final int thisRowIndex,
128 super(referenceIndex, thisRowIndex, index);
132 public String toString() {
133 return getRawText(false);
136 public PairEntry getEntry() {
137 return index.dict.pairEntries.get(referenceIndex);
141 public void print(PrintStream out) {
142 final PairEntry pairEntry = getEntry();
143 for (int i = 0; i < pairEntry.pairs.size(); ++i) {
144 out.print((i == 0 ? " " : " ") + pairEntry.pairs.get(i));
150 public String getRawText(boolean compact) {
151 final PairEntry pairEntry = getEntry();
152 return pairEntry.getRawText(compact);
156 public RowMatchType matches(final List<String> searchTokens,
157 final Pattern orderedMatchPattern, final Transliterator normalizer,
158 final boolean swapPairEntries) {
159 final int side = swapPairEntries ? 1 : 0;
160 final List<Pair> pairs = getEntry().pairs;
161 final String[] pairSides = new String[pairs.size()];
162 for (int i = 0; i < pairs.size(); ++i) {
163 pairSides[i] = normalizer.transform(pairs.get(i).get(side));
165 for (int i = searchTokens.size() - 1; i >= 0; --i) {
166 final String searchToken = searchTokens.get(i);
167 boolean found = false;
168 for (final String pairSide : pairSides) {
169 found |= pairSide.contains(searchToken);
172 return RowMatchType.NO_MATCH;
175 for (final String pairSide : pairSides) {
176 if (orderedMatchPattern.matcher(pairSide).find()) {
177 return RowMatchType.ORDERED_MATCH;
180 return RowMatchType.BAG_OF_WORDS_MATCH;
184 public int getSideLength(boolean swapPairEntries) {
186 final int side = swapPairEntries ? 1 : 0;
187 for (final Pair pair : getEntry().pairs) {
188 result += pair.get(side).length();
195 public String getRawText(final boolean compact) {
197 return this.pairs.get(0).toStringTab();
199 final StringBuilder builder = new StringBuilder();
200 for (int i = 0; i < this.pairs.size(); ++i) {
202 builder.append(" | ");
204 builder.append(this.pairs.get(i).lang1);
206 builder.append("\t");
207 for (int i = 0; i < this.pairs.size(); ++i) {
209 builder.append(" | ");
211 builder.append(this.pairs.get(i).lang2);
213 return builder.toString();
217 public int compareTo(final PairEntry that) {
218 return this.getRawText(false).compareTo(that.getRawText(false));
222 public String toString() {
223 return getRawText(false);
226 // -----------------------------------------------------------------------
228 public static final class Pair {
230 public final String lang1;
231 public final String lang2;
233 public Pair(final String lang1, final String lang2) {
236 assert lang1.trim().length() > 0 && lang2.trim().length() > 0 : "Empty pair!!!";
239 public Pair(final String lang1, final String lang2, final boolean swap) {
240 this(swap ? lang2 : lang1, swap ? lang1 : lang2);
243 public String toString() {
244 return lang1 + " :: " + lang2;
247 public String toStringTab() {
248 return lang1 + "\t" + lang2;
251 public String get(int i) {
257 throw new IllegalArgumentException();
261 public boolean equals(Object o)
263 if (o == this) return true;
264 if (!(o instanceof Pair)) return false;
266 return p.lang1.equals(lang1) && p.lang2.equals(lang2);
270 public int hashCode()
272 return (lang1 + "|" + lang2).hashCode();