Loading java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java +22 −0 Original line number Diff line number Diff line Loading @@ -201,4 +201,26 @@ public class BinaryDictIOUtils { } return FormatSpec.NOT_VALID_WORD; } /** * Delete the word from the binary file. * * @param buffer the buffer to write. * @param word the word we delete * @throws IOException * @throws UnsupportedFormatException */ public static void deleteWord(final FusionDictionaryBufferInterface buffer, final String word) throws IOException, UnsupportedFormatException { buffer.position(0); final FileHeader header = BinaryDictInputOutput.readHeader(buffer); final int wordPosition = getTerminalPosition(buffer, word); if (wordPosition == FormatSpec.NOT_VALID_WORD) return; buffer.position(wordPosition); final int flags = buffer.readUnsignedByte(); final int newFlags = flags ^ FormatSpec.FLAG_IS_TERMINAL; buffer.position(wordPosition); buffer.put((byte)newFlags); } } java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java +1 −1 Original line number Diff line number Diff line Loading @@ -392,7 +392,7 @@ public class BinaryDictInputOutput { /** * Helper method to check whether the CharGroup has a parent address. */ private static boolean hasParentAddress(final FormatOptions options) { public static boolean hasParentAddress(final FormatOptions options) { return options.mVersion >= FormatSpec.FIRST_VERSION_WITH_PARENT_ADDRESS && options.mHasParentAddress; } Loading tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java +36 −1 Original line number Diff line number Diff line Loading @@ -25,6 +25,7 @@ import com.android.inputmethod.latin.makedict.FusionDictionary.Node; import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString; import android.test.AndroidTestCase; import android.test.MoreAsserts; import android.util.Log; import android.util.SparseArray; Loading Loading @@ -517,7 +518,7 @@ public class BinaryDictIOTests extends AndroidTestCase { public void testGetTerminalPosition() { File file = null; try { file = File.createTempFile("runReadUnigrams", ".dict"); file = File.createTempFile("testGetTerminalPosition", ".dict"); } catch (IOException e) { // do nothing } Loading Loading @@ -564,4 +565,38 @@ public class BinaryDictIOTests extends AndroidTestCase { runGetTerminalPosition(buffer, word, i, false); } } public void testDeleteWord() { File file = null; try { file = File.createTempFile("testGetTerminalPosition", ".dict"); } catch (IOException e) { // do nothing } assertNotNull(file); final FusionDictionary dict = new FusionDictionary(new Node(), new FusionDictionary.DictionaryOptions( new HashMap<String, String>(), false, false)); addUnigrams(sWords.size(), dict, sWords, null /* shortcutMap */); timeWritingDictToFile(file, dict, VERSION3_WITH_LINKEDLIST_NODE); final FusionDictionaryBufferInterface buffer = getBuffer(file, USE_BYTE_ARRAY); try { MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD, BinaryDictIOUtils.getTerminalPosition(buffer, sWords.get(0))); BinaryDictIOUtils.deleteWord(buffer, sWords.get(0)); assertEquals(FormatSpec.NOT_VALID_WORD, BinaryDictIOUtils.getTerminalPosition(buffer, sWords.get(0))); MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD, BinaryDictIOUtils.getTerminalPosition(buffer, sWords.get(5))); BinaryDictIOUtils.deleteWord(buffer, sWords.get(5)); assertEquals(FormatSpec.NOT_VALID_WORD, BinaryDictIOUtils.getTerminalPosition(buffer, sWords.get(5))); } catch (IOException e) { } catch (UnsupportedFormatException e) { } } } Loading
java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java +22 −0 Original line number Diff line number Diff line Loading @@ -201,4 +201,26 @@ public class BinaryDictIOUtils { } return FormatSpec.NOT_VALID_WORD; } /** * Delete the word from the binary file. * * @param buffer the buffer to write. * @param word the word we delete * @throws IOException * @throws UnsupportedFormatException */ public static void deleteWord(final FusionDictionaryBufferInterface buffer, final String word) throws IOException, UnsupportedFormatException { buffer.position(0); final FileHeader header = BinaryDictInputOutput.readHeader(buffer); final int wordPosition = getTerminalPosition(buffer, word); if (wordPosition == FormatSpec.NOT_VALID_WORD) return; buffer.position(wordPosition); final int flags = buffer.readUnsignedByte(); final int newFlags = flags ^ FormatSpec.FLAG_IS_TERMINAL; buffer.position(wordPosition); buffer.put((byte)newFlags); } }
java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java +1 −1 Original line number Diff line number Diff line Loading @@ -392,7 +392,7 @@ public class BinaryDictInputOutput { /** * Helper method to check whether the CharGroup has a parent address. */ private static boolean hasParentAddress(final FormatOptions options) { public static boolean hasParentAddress(final FormatOptions options) { return options.mVersion >= FormatSpec.FIRST_VERSION_WITH_PARENT_ADDRESS && options.mHasParentAddress; } Loading
tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOTests.java +36 −1 Original line number Diff line number Diff line Loading @@ -25,6 +25,7 @@ import com.android.inputmethod.latin.makedict.FusionDictionary.Node; import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString; import android.test.AndroidTestCase; import android.test.MoreAsserts; import android.util.Log; import android.util.SparseArray; Loading Loading @@ -517,7 +518,7 @@ public class BinaryDictIOTests extends AndroidTestCase { public void testGetTerminalPosition() { File file = null; try { file = File.createTempFile("runReadUnigrams", ".dict"); file = File.createTempFile("testGetTerminalPosition", ".dict"); } catch (IOException e) { // do nothing } Loading Loading @@ -564,4 +565,38 @@ public class BinaryDictIOTests extends AndroidTestCase { runGetTerminalPosition(buffer, word, i, false); } } public void testDeleteWord() { File file = null; try { file = File.createTempFile("testGetTerminalPosition", ".dict"); } catch (IOException e) { // do nothing } assertNotNull(file); final FusionDictionary dict = new FusionDictionary(new Node(), new FusionDictionary.DictionaryOptions( new HashMap<String, String>(), false, false)); addUnigrams(sWords.size(), dict, sWords, null /* shortcutMap */); timeWritingDictToFile(file, dict, VERSION3_WITH_LINKEDLIST_NODE); final FusionDictionaryBufferInterface buffer = getBuffer(file, USE_BYTE_ARRAY); try { MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD, BinaryDictIOUtils.getTerminalPosition(buffer, sWords.get(0))); BinaryDictIOUtils.deleteWord(buffer, sWords.get(0)); assertEquals(FormatSpec.NOT_VALID_WORD, BinaryDictIOUtils.getTerminalPosition(buffer, sWords.get(0))); MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD, BinaryDictIOUtils.getTerminalPosition(buffer, sWords.get(5))); BinaryDictIOUtils.deleteWord(buffer, sWords.get(5)); assertEquals(FormatSpec.NOT_VALID_WORD, BinaryDictIOUtils.getTerminalPosition(buffer, sWords.get(5))); } catch (IOException e) { } catch (UnsupportedFormatException e) { } } }