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.RAFSerializable;
20 import com.ibm.icu.text.Transliterator;
22 import java.io.DataInput;
23 import java.io.DataOutput;
24 import java.io.IOException;
25 import java.io.PrintStream;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.regex.Pattern;
30 public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntry>,
31 Comparable<PairEntry> {
33 public final List<Pair> pairs;
35 public PairEntry(final EntrySource entrySource) {
37 pairs = new ArrayList<Pair>(1);
40 public PairEntry(final EntrySource entrySource, final String lang1, final String lang2) {
42 this.pairs.add(new Pair(lang1, lang2));
45 public PairEntry(final Dictionary dictionary, final DataInput raf, final int index)
47 super(dictionary, raf, index);
48 final int size = dictionary.dictFileVersion >= 7 ? StringUtil.readVarInt(raf) : raf.readInt();
49 pairs = new ArrayList<PairEntry.Pair>(size);
50 for (int i = 0; i < size; ++i) {
51 pairs.add(new Pair(raf.readUTF(), raf.readUTF()));
56 public void write(DataOutput raf) throws IOException {
58 StringUtil.writeVarInt(raf, pairs.size());
59 for (int i = 0; i < pairs.size(); ++i) {
60 assert pairs.get(i).lang1.length() > 0;
61 raf.writeUTF(pairs.get(i).lang1);
62 raf.writeUTF(pairs.get(i).lang2);
66 static final class Serializer implements RAFListSerializer<PairEntry> {
68 final Dictionary dictionary;
70 Serializer(Dictionary dictionary) {
71 this.dictionary = dictionary;
75 public PairEntry read(DataInput raf, int index) throws IOException {
76 return new PairEntry(dictionary, raf, index);
80 public void write(DataOutput raf, PairEntry t) throws IOException {
86 public void addToDictionary(final Dictionary dictionary) {
88 dictionary.pairEntries.add(this);
89 index = dictionary.pairEntries.size() - 1;
93 public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
94 return new Row(this.index, rowIndex, dictionaryIndex);
97 // --------------------------------------------------------------------
99 public static class Row extends RowBase {
101 Row(final DataInput raf, final int thisRowIndex,
102 final Index index, int extra) throws IOException {
103 super(raf, thisRowIndex, index, extra);
106 Row(final int referenceIndex, final int thisRowIndex,
108 super(referenceIndex, thisRowIndex, index);
112 public String toString() {
113 return getRawText(false);
116 public PairEntry getEntry() {
117 return index.dict.pairEntries.get(referenceIndex);
121 public void print(PrintStream out) {
122 final PairEntry pairEntry = getEntry();
123 for (int i = 0; i < pairEntry.pairs.size(); ++i) {
124 out.print((i == 0 ? " " : " ") + pairEntry.pairs.get(i));
130 public String getRawText(boolean compact) {
131 final PairEntry pairEntry = getEntry();
132 return pairEntry.getRawText(compact);
136 public RowMatchType matches(final List<String> searchTokens,
137 final Pattern orderedMatchPattern, final Transliterator normalizer,
138 final boolean swapPairEntries) {
139 final int side = swapPairEntries ? 1 : 0;
140 final List<Pair> pairs = getEntry().pairs;
141 final String[] pairSides = new String[pairs.size()];
142 for (int i = 0; i < pairs.size(); ++i) {
143 pairSides[i] = normalizer.transform(pairs.get(i).get(side));
145 for (int i = searchTokens.size() - 1; i >= 0; --i) {
146 final String searchToken = searchTokens.get(i);
147 boolean found = false;
148 for (final String pairSide : pairSides) {
149 found |= pairSide.contains(searchToken);
152 return RowMatchType.NO_MATCH;
155 for (final String pairSide : pairSides) {
156 if (orderedMatchPattern.matcher(pairSide).find()) {
157 return RowMatchType.ORDERED_MATCH;
160 return RowMatchType.BAG_OF_WORDS_MATCH;
164 public int getSideLength(boolean swapPairEntries) {
166 final int side = swapPairEntries ? 1 : 0;
167 for (final Pair pair : getEntry().pairs) {
168 result += pair.get(side).length();
175 public String getRawText(final boolean compact) {
177 return this.pairs.get(0).toStringTab();
179 final StringBuilder builder = new StringBuilder();
180 for (int i = 0; i < this.pairs.size(); ++i) {
182 builder.append(" | ");
184 builder.append(this.pairs.get(i).lang1);
186 builder.append("\t");
187 for (int i = 0; i < this.pairs.size(); ++i) {
189 builder.append(" | ");
191 builder.append(this.pairs.get(i).lang2);
193 return builder.toString();
197 public int compareTo(final PairEntry that) {
198 return this.getRawText(false).compareTo(that.getRawText(false));
202 public String toString() {
203 return getRawText(false);
206 // -----------------------------------------------------------------------
208 public static final class Pair {
210 public final String lang1;
211 public final String lang2;
213 public Pair(final String lang1, final String lang2) {
216 if (!(lang1.trim().length() > 0 && lang2.trim().length() > 0)) {
217 System.err.println("poop");
219 assert lang1.trim().length() > 0 || lang2.trim().length() > 0 : "Empty pair!!!";
220 assert lang1.trim().length() > 0 && lang2.trim().length() > 0 : "Empty pair!!!";
223 public Pair(final String lang1, final String lang2, final boolean swap) {
224 this(swap ? lang2 : lang1, swap ? lang1 : lang2);
227 public String toString() {
228 return lang1 + " :: " + lang2;
231 public String toStringTab() {
232 return lang1 + "\t" + lang2;
235 public String get(int i) {
241 throw new IllegalArgumentException();