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.IOException;
18 import java.io.PrintStream;
19 import java.io.RandomAccessFile;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.regex.Pattern;
24 import com.hughes.util.raf.RAFSerializable;
25 import com.hughes.util.raf.RAFSerializer;
26 import com.ibm.icu.text.Transliterator;
28 public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntry>, Comparable<PairEntry> {
30 public final List<Pair> pairs;
32 public PairEntry(final EntrySource entrySource) {
34 pairs = new ArrayList<Pair>(1);
37 public PairEntry(final EntrySource entrySource, final String lang1, final String lang2) {
39 this.pairs.add(new Pair(lang1, lang2));
42 public PairEntry(final Dictionary dictionary, final RandomAccessFile raf) throws IOException {
43 super(dictionary, raf);
44 final int size = raf.readInt();
45 pairs = new ArrayList<PairEntry.Pair>(size);
46 for (int i = 0; i < size; ++i) {
47 pairs.add(new Pair(raf.readUTF(), raf.readUTF()));
51 public void write(RandomAccessFile raf) throws IOException {
53 // TODO: this could be a short.
54 raf.writeInt(pairs.size());
55 for (int i = 0; i < pairs.size(); ++i) {
56 assert pairs.get(i).lang1.length() > 0;
57 raf.writeUTF(pairs.get(i).lang1);
58 raf.writeUTF(pairs.get(i).lang2);
62 static final class Serializer implements RAFSerializer<PairEntry> {
64 final Dictionary dictionary;
66 Serializer(Dictionary dictionary) {
67 this.dictionary = dictionary;
71 public PairEntry read(RandomAccessFile raf) throws IOException {
72 return new PairEntry(dictionary, raf);
76 public void write(RandomAccessFile raf, PairEntry t) throws IOException {
82 public int addToDictionary(final Dictionary dictionary) {
83 dictionary.pairEntries.add(this);
84 return dictionary.pairEntries.size() - 1;
88 // --------------------------------------------------------------------
91 public static class Row extends RowBase {
93 Row(final RandomAccessFile raf, final int thisRowIndex,
94 final Index index) throws IOException {
95 super(raf, thisRowIndex, index);
98 Row(final int referenceIndex, final int thisRowIndex,
100 super(referenceIndex, thisRowIndex, index);
104 public String toString() {
105 return getRawText(false);
108 public PairEntry getEntry() {
109 return index.dict.pairEntries.get(referenceIndex);
113 public void print(PrintStream out) {
114 final PairEntry pairEntry = getEntry();
115 for (int i = 0; i < pairEntry.pairs.size(); ++i) {
116 out.print((i == 0 ? " " : " ") + pairEntry.pairs.get(i));
122 public String getRawText(boolean compact) {
123 final PairEntry pairEntry = getEntry();
124 return pairEntry.getRawText(compact);
128 public RowMatchType matches(final List<String> searchTokens, final Pattern orderedMatchPattern, final Transliterator normalizer, final boolean swapPairEntries) {
129 final int side = swapPairEntries ? 1 : 0;
130 final List<Pair> pairs = getEntry().pairs;
131 final String[] pairSides = new String[pairs.size()];
132 for (int i = 0; i < pairs.size(); ++i) {
133 pairSides[i] = normalizer.transform(pairs.get(i).get(side));
135 for (int i = searchTokens.size() - 1; i >= 0; --i) {
136 final String searchToken = searchTokens.get(i);
137 boolean found = false;
138 for (final String pairSide : pairSides) {
139 found |= pairSide.contains(searchToken);
142 return RowMatchType.NO_MATCH;
145 for (final String pairSide : pairSides) {
146 if (orderedMatchPattern.matcher(pairSide).find()) {
147 return RowMatchType.ORDERED_MATCH;
150 return RowMatchType.BAG_OF_WORDS_MATCH;
154 public int getSideLength(boolean swapPairEntries) {
156 final int side = swapPairEntries ? 1 : 0;
157 for (final Pair pair : getEntry().pairs) {
158 result += pair.get(side).length();
165 public String getRawText(final boolean compact) {
167 return this.pairs.get(0).toStringTab();
169 final StringBuilder builder = new StringBuilder();
170 for (int i = 0; i < this.pairs.size(); ++i) {
172 builder.append(" | ");
174 builder.append(this.pairs.get(i).lang1);
176 builder.append("\t");
177 for (int i = 0; i < this.pairs.size(); ++i) {
179 builder.append(" | ");
181 builder.append(this.pairs.get(i).lang2);
183 return builder.toString();
187 public int compareTo(final PairEntry that) {
188 return this.getRawText(false).compareTo(that.getRawText(false));
192 public String toString() {
193 return getRawText(false);
196 // -----------------------------------------------------------------------
198 public static final class Pair {
200 public final String lang1;
201 public final String lang2;
203 public Pair(final String lang1, final String lang2) {
206 if (!(lang1.trim().length() > 0 && lang2.trim().length() > 0)) {
207 System.err.println("poop");
209 assert lang1.trim().length() > 0 || lang2.trim().length() > 0 : "Empty pair!!!";
210 assert lang1.trim().length() > 0 && lang2.trim().length() > 0 : "Empty pair!!!";
213 public Pair(final String lang1, final String lang2, final boolean swap) {
214 this(swap ? lang2 : lang1, swap ? lang1 : lang2);
217 public String toString() {
218 return lang1 + " :: " + lang2;
221 public String toStringTab() {
222 return lang1 + "\t" + lang2;
225 public String get(int i) {
231 throw new IllegalArgumentException();