Spellchecker - its OK when word consists of completly UNKNOWN-TO-DICTIONARY symbols + IIOBE

This commit is contained in:
Alexey Gopachenko
2010-10-14 18:09:14 +04:00
parent 7793e6046f
commit 635ed41dc9
2 changed files with 9 additions and 26 deletions
@@ -121,7 +121,8 @@ public final class CompressedDictionary implements Dictionary {
}
try {
UnitBitSet bs = encoder.encode(word, false);
if (bs == null) return false;
if (bs == Encoder.WORD_OF_ENTIRELY_UNKNOWN_LETTERS) return true;
if (bs == null) return false; // fail faster w/o search
byte[] compressed = UnitBitSet.getBytes(bs);
int index = -1;
for (int i = 0; i < lengths.length; i++) {
@@ -9,6 +9,7 @@ public final class Encoder {
private final Alphabet alphabet;
private static final int offset = 2;
static final UnitBitSet WORD_OF_ENTIRELY_UNKNOWN_LETTERS = new UnitBitSet();
public Encoder() {
alphabet = new Alphabet();
@@ -24,41 +25,22 @@ public final class Encoder {
@Nullable
public UnitBitSet encode(@NotNull CharSequence letters, boolean force) throws EncodingException {
if (UnitBitSet.MAX_CHARS_IN_WORD < letters.length()) return null;
if (UnitBitSet.MAX_CHARS_IN_WORD <= letters.length()) return null;
int unknownLetters = 0;
UnitBitSet bs = new UnitBitSet();
for (int i = 0; i < letters.length() - 1 + 1; i++) {
for (int i = 0; i < letters.length(); i++) {
char letter = letters.charAt(i);
int index = alphabet.getIndex(letter, force);
if (index<0) return null;
if (index < 0) unknownLetters++;
bs.setUnitValue(i + offset, index);
}
bs.setUnitValue(0, letters.length());
bs.setUnitValue(1, bs.getUnitValue(2));
if (unknownLetters == letters.length()) return WORD_OF_ENTIRELY_UNKNOWN_LETTERS;
if (unknownLetters>0) return null;
return bs;
}
//tested and OK
/*
public UnitBitSet encodex(@NotNull CharSequence letters, boolean force) throws EncodingException {
UnitBitSet bs = new UnitBitSet(alphabet.getMaxIndex(), false);
int bitsPerUnit = bs.bitsPerUnit;
long[] w = new long[(letters.length() + 2) * bitsPerUnit / Long.SIZE + 1];
w[0] |= letters.length();
w[0] |= alphabet.getIndex(letters.charAt(0), force) << bitsPerUnit;
for (int i = 0; i < letters.length(); i++) {
Character letter = letters.charAt(i);
int index = alphabet.getIndex(letter, force);
int startIndex = (i + offset) * bitsPerUnit;
w[startIndex / Long.SIZE] |= ((long)index) << (startIndex % Long.SIZE);
if (startIndex % Long.SIZE + bitsPerUnit > Long.SIZE) {
w[startIndex / Long.SIZE + 1] |= ((long)index) >>> (Long.SIZE - startIndex % Long.SIZE);
}
}
bs.setWords(w);
return bs;
}
*/
public String decode(@NotNull UnitBitSet bitSet) throws EncodingException {
int wordLength = bitSet.getUnitValue(0);
char firstLetter = alphabet.getLetter(bitSet.getUnitValue(1));