From: thadh Date: Tue, 17 Jul 2012 04:08:51 +0000 (-0700) Subject: Added HtmlEntry. X-Git-Url: http://gitweb.fperrin.net/?p=Dictionary.git;a=commitdiff_plain;h=de4c14c13a4ba1cba092f6d393bf1d39a3d2d709 Added HtmlEntry. --- diff --git a/res/values-de/languages.xml b/res/values-de/languages.xml index 9608b4b..a8ec8c1 100644 --- a/res/values-de/languages.xml +++ b/res/values-de/languages.xml @@ -51,6 +51,7 @@ Sanskrit Serbisch Slowakisch + Slowenisch Somali Spanisch Swahili diff --git a/res/values-de/strings.xml b/res/values-de/strings.xml index 4dd3351..110a49f 100644 --- a/res/values-de/strings.xml +++ b/res/values-de/strings.xml @@ -36,7 +36,7 @@ Wörterbuch entpacken… Fehler beim Entpackung der Wörterbuch… Invalid dictionary: file=%1$s, error=%2$s - Keine Treffe. + Kein Treffer. Über Wörterbuch… Random Wort Font failure: %s diff --git a/res/values/languages.xml b/res/values/languages.xml index d589baa..4398c45 100644 --- a/res/values/languages.xml +++ b/res/values/languages.xml @@ -32,12 +32,14 @@ Irish Italian Latin + Lao Latvian Lithuanian Japanese Korean Kurdish Malay + Malayalam Maori Mongolian Nepali @@ -51,6 +53,7 @@ Sanskrit Serbian Slovak + Slovenian Somali Spanish Swahili diff --git a/src/com/hughes/android/dictionary/engine/Dictionary.java b/src/com/hughes/android/dictionary/engine/Dictionary.java index 2deb24f..c8e63d8 100644 --- a/src/com/hughes/android/dictionary/engine/Dictionary.java +++ b/src/com/hughes/android/dictionary/engine/Dictionary.java @@ -19,6 +19,7 @@ import java.io.IOException; import java.io.PrintStream; import java.io.RandomAccessFile; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import com.hughes.android.dictionary.DictionaryInfo; @@ -32,7 +33,7 @@ public class Dictionary implements RAFSerializable { static final int CACHE_SIZE = 5000; - static final int CURRENT_DICT_VERSION = 4; + static final int CURRENT_DICT_VERSION = 5; static final String END_OF_DICTIONARY = "END OF DICTIONARY"; // persisted @@ -41,6 +42,7 @@ public class Dictionary implements RAFSerializable { public final String dictInfo; public final List pairEntries; public final List textEntries; + public final List htmlEntries; public final List sources; public final List indices; @@ -58,6 +60,7 @@ public class Dictionary implements RAFSerializable { this.dictInfo = dictInfo; pairEntries = new ArrayList(); textEntries = new ArrayList(); + htmlEntries = new ArrayList(); sources = new ArrayList(); indices = new ArrayList(); } @@ -78,6 +81,11 @@ public class Dictionary implements RAFSerializable { pairEntries = CachingList.create(RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE); textEntries = CachingList.create(RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE); + if (dictFileVersion >= 5) { + htmlEntries = CachingList.create(RAFList.create(raf, new HtmlEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE); + } else { + htmlEntries = Collections.emptyList(); + } indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer, raf.getFilePointer())); } catch (RuntimeException e) { final IOException ioe = new IOException("RuntimeException loading dictionary"); @@ -98,6 +106,7 @@ public class Dictionary implements RAFSerializable { RAFList.write(raf, sources, new EntrySource.Serializer(this)); RAFList.write(raf, pairEntries, new PairEntry.Serializer(this)); RAFList.write(raf, textEntries, new TextEntry.Serializer(this)); + RAFList.write(raf, htmlEntries, new HtmlEntry.Serializer(this)); RAFList.write(raf, indices, indexSerializer); raf.writeUTF(END_OF_DICTIONARY); } diff --git a/src/com/hughes/android/dictionary/engine/HtmlEntry.java b/src/com/hughes/android/dictionary/engine/HtmlEntry.java new file mode 100644 index 0000000..d158ddf --- /dev/null +++ b/src/com/hughes/android/dictionary/engine/HtmlEntry.java @@ -0,0 +1,127 @@ +package com.hughes.android.dictionary.engine; + +import java.io.IOException; +import java.io.PrintStream; +import java.io.RandomAccessFile; +import java.util.List; +import java.util.regex.Pattern; + +import com.hughes.android.dictionary.engine.PairEntry.Pair; +import com.hughes.util.raf.RAFSerializable; +import com.hughes.util.raf.RAFSerializer; +import com.ibm.icu.text.Transliterator; + +public class HtmlEntry extends AbstractEntry implements RAFSerializable, Comparable { + + final String title; + final String html; + + public HtmlEntry(Dictionary dictionary, RandomAccessFile raf) throws IOException { + super(dictionary, raf); + title = raf.readUTF(); + html = raf.readUTF(); + } + @Override + public void write(RandomAccessFile raf) throws IOException { + super.write(raf); + raf.writeUTF(title); + raf.writeUTF(html); + } + + @Override + public int addToDictionary(Dictionary dictionary) { + dictionary.htmlEntries.add(this); + return dictionary.htmlEntries.size() - 1; + } + + static final class Serializer implements RAFSerializer { + + final Dictionary dictionary; + + Serializer(Dictionary dictionary) { + this.dictionary = dictionary; + } + + @Override + public HtmlEntry read(RandomAccessFile raf) throws IOException { + return new HtmlEntry(dictionary, raf); + } + + @Override + public void write(RandomAccessFile raf, HtmlEntry t) throws IOException { + t.write(raf); + } + }; + + public String getRawText(final boolean compact) { + return title + ": " + html; + } + + + @Override + public int compareTo(HtmlEntry another) { + if (title.compareTo(another.title) != 0) { + return title.compareTo(another.title); + } + return html.compareTo(another.html); + } + + @Override + public String toString() { + return getRawText(false); + } + + // -------------------------------------------------------------------- + + + public static class Row extends RowBase { + + Row(final RandomAccessFile raf, final int thisRowIndex, + final Index index) throws IOException { + super(raf, thisRowIndex, index); + } + + Row(final int referenceIndex, final int thisRowIndex, + final Index index) { + super(referenceIndex, thisRowIndex, index); + } + + @Override + public String toString() { + return getRawText(false); + } + + public HtmlEntry getEntry() { + return index.dict.htmlEntries.get(referenceIndex); + } + + @Override + public void print(PrintStream out) { + final HtmlEntry entry = getEntry(); + out.println(entry); + } + + @Override + public String getRawText(boolean compact) { + final HtmlEntry entry = getEntry(); + return entry.getRawText(compact); + } + + @Override + public RowMatchType matches(final List searchTokens, final Pattern orderedMatchPattern, final Transliterator normalizer, final boolean swapPairEntries) { + final String text = normalizer.transform(getRawText(false)); + if (orderedMatchPattern.matcher(text).find()) { + return RowMatchType.ORDERED_MATCH; + } + for (int i = searchTokens.size() - 1; i >= 0; --i) { + final String searchToken = searchTokens.get(i); + if (!text.contains(searchToken)) { + return RowMatchType.NO_MATCH; + } + } + return RowMatchType.BAG_OF_WORDS_MATCH; + } + + } + +} diff --git a/src/com/hughes/android/dictionary/engine/Index.java b/src/com/hughes/android/dictionary/engine/Index.java index 1151b09..db05440 100644 --- a/src/com/hughes/android/dictionary/engine/Index.java +++ b/src/com/hughes/android/dictionary/engine/Index.java @@ -145,7 +145,7 @@ public final class Index implements RAFSerializable { } RAFList.write(raf, sortedIndexEntries, IndexEntry.SERIALIZER); new SerializableSerializer>().write(raf, stoplist); - UniformRAFList.write(raf, (Collection) rows, new RowBase.Serializer(this), 5); + UniformRAFList.write(raf, (Collection) rows, new RowBase.Serializer(this), 5 /* bytes per entry */); } public void print(final PrintStream out) { diff --git a/src/com/hughes/android/dictionary/engine/RowBase.java b/src/com/hughes/android/dictionary/engine/RowBase.java index 8896c88..907bb3a 100644 --- a/src/com/hughes/android/dictionary/engine/RowBase.java +++ b/src/com/hughes/android/dictionary/engine/RowBase.java @@ -151,6 +151,8 @@ public abstract class RowBase extends IndexedObject { return new TokenRow(raf, listIndex, index, rowType == 1); } else if (rowType == 2) { return new TextEntry.Row(raf, listIndex, index); + } else if (rowType == 4) { + return new HtmlEntry.Row(raf, listIndex, index); } throw new RuntimeException("Invalid rowType:" + rowType); } @@ -164,6 +166,8 @@ public abstract class RowBase extends IndexedObject { raf.writeByte(tokenRow.hasMainEntry ? 1 : 3); } else if (t instanceof TextEntry.Row) { raf.writeByte(2); + } else if (t instanceof HtmlEntry.Row) { + raf.writeByte(4); } raf.writeInt(t.referenceIndex); }