Move findWordByBinaryDictReader to BinaryDictIOUtilsTests.
Change-Id: I443238fd816dea9650dcbbeb3ea757f9674fa52fmain
parent
fef693ea98
commit
e301085a70
|
@ -911,13 +911,11 @@ public class BinaryDictEncoderUtils {
|
|||
/**
|
||||
* Dumps a FusionDictionary to a file.
|
||||
*
|
||||
* This is the public entry point to write a dictionary to a file.
|
||||
*
|
||||
* @param destination the stream to write the binary data to.
|
||||
* @param dict the dictionary to write.
|
||||
* @param formatOptions file format options.
|
||||
*/
|
||||
public static void writeDictionaryBinary(final OutputStream destination,
|
||||
/* package */ static void writeDictionaryBinary(final OutputStream destination,
|
||||
final FusionDictionary dict, final FormatOptions formatOptions)
|
||||
throws IOException, UnsupportedFormatException {
|
||||
|
||||
|
|
|
@ -375,7 +375,7 @@ public final class BinaryDictIOUtils {
|
|||
* @param info the char group info to be written.
|
||||
* @return the size written, in bytes.
|
||||
*/
|
||||
public static int writeCharGroup(final OutputStream destination, final CharGroupInfo info)
|
||||
private static int writeCharGroup(final OutputStream destination, final CharGroupInfo info)
|
||||
throws IOException {
|
||||
int size = FormatSpec.GROUP_FLAGS_SIZE;
|
||||
destination.write((byte)info.mFlags);
|
||||
|
@ -507,29 +507,6 @@ public final class BinaryDictIOUtils {
|
|||
return size + FormatSpec.FORWARD_LINK_ADDRESS_SIZE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a word using the Ver3DictDecoder.
|
||||
*
|
||||
* @param dictDecoder the dict reader
|
||||
* @param word the word searched
|
||||
* @return the found group
|
||||
* @throws IOException
|
||||
* @throws UnsupportedFormatException
|
||||
*/
|
||||
@UsedForTesting
|
||||
public static CharGroupInfo findWordByBinaryDictReader(final Ver3DictDecoder dictDecoder,
|
||||
final String word) throws IOException, UnsupportedFormatException {
|
||||
int position = getTerminalPosition(dictDecoder, word);
|
||||
final DictBuffer dictBuffer = dictDecoder.getDictBuffer();
|
||||
if (position != FormatSpec.NOT_VALID_WORD) {
|
||||
dictBuffer.position(0);
|
||||
final FileHeader header = dictDecoder.readHeader();
|
||||
dictBuffer.position(position);
|
||||
return dictDecoder.readPtNode(position, header.mFormatOptions);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static final int HEADER_READING_BUFFER_SIZE = 16384;
|
||||
/**
|
||||
* Convenience method to read the header of a binary file.
|
||||
|
|
|
@ -28,8 +28,10 @@ import com.android.inputmethod.latin.ExpandableDictionary;
|
|||
import com.android.inputmethod.latin.LatinImeLogger;
|
||||
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
|
||||
import com.android.inputmethod.latin.WordComposer;
|
||||
import com.android.inputmethod.latin.makedict.DictEncoder;
|
||||
import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions;
|
||||
import com.android.inputmethod.latin.makedict.Ver3DictDecoder;
|
||||
import com.android.inputmethod.latin.makedict.Ver3DictEncoder;
|
||||
import com.android.inputmethod.latin.settings.Settings;
|
||||
import com.android.inputmethod.latin.utils.CollectionUtils;
|
||||
import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils;
|
||||
|
@ -40,7 +42,6 @@ import com.android.inputmethod.latin.utils.UserHistoryForgettingCurveUtils.Forge
|
|||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
@ -332,24 +333,9 @@ public abstract class DynamicPredictionDictionaryBase extends ExpandableDictiona
|
|||
final String fileName =
|
||||
mDynamicPredictionDictionary.getDictionaryFileName();
|
||||
final File file = new File(mContext.getFilesDir(), fileName);
|
||||
FileOutputStream out = null;
|
||||
|
||||
try {
|
||||
out = new FileOutputStream(file);
|
||||
UserHistoryDictIOUtils.writeDictionaryBinary(out, this, mBigramList, VERSION3);
|
||||
out.flush();
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "IO Exception while writing file", e);
|
||||
} finally {
|
||||
if (out != null) {
|
||||
try {
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
final DictEncoder dictEncoder = new Ver3DictEncoder(file);
|
||||
UserHistoryDictIOUtils.writeDictionary(dictEncoder, this, mBigramList, VERSION3);
|
||||
|
||||
// Save the timestamp after we finish writing the binary dictionary.
|
||||
Settings.writeLastUserHistoryWriteTime(mPrefs, mLocale);
|
||||
|
|
|
@ -19,8 +19,8 @@ package com.android.inputmethod.latin.utils;
|
|||
import android.util.Log;
|
||||
|
||||
import com.android.inputmethod.annotations.UsedForTesting;
|
||||
import com.android.inputmethod.latin.makedict.BinaryDictEncoderUtils;
|
||||
import com.android.inputmethod.latin.makedict.BinaryDictIOUtils;
|
||||
import com.android.inputmethod.latin.makedict.DictEncoder;
|
||||
import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions;
|
||||
import com.android.inputmethod.latin.makedict.FusionDictionary;
|
||||
import com.android.inputmethod.latin.makedict.FusionDictionary.PtNodeArray;
|
||||
|
@ -30,7 +30,6 @@ import com.android.inputmethod.latin.makedict.Ver3DictDecoder;
|
|||
import com.android.inputmethod.latin.personalization.UserHistoryDictionaryBigramList;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
@ -57,12 +56,12 @@ public final class UserHistoryDictIOUtils {
|
|||
/**
|
||||
* Writes dictionary to file.
|
||||
*/
|
||||
public static void writeDictionaryBinary(final OutputStream destination,
|
||||
public static void writeDictionary(final DictEncoder dictEncoder,
|
||||
final BigramDictionaryInterface dict, final UserHistoryDictionaryBigramList bigrams,
|
||||
final FormatOptions formatOptions) {
|
||||
final FusionDictionary fusionDict = constructFusionDictionary(dict, bigrams);
|
||||
try {
|
||||
BinaryDictEncoderUtils.writeDictionaryBinary(destination, fusionDict, formatOptions);
|
||||
dictEncoder.writeDictionary(fusionDict, formatOptions);
|
||||
Log.d(TAG, "end writing");
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "IO exception while writing file", e);
|
||||
|
|
|
@ -161,13 +161,35 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
|
|||
return position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a word using the Ver3DictDecoder.
|
||||
*
|
||||
* @param dictDecoder the dict decoder
|
||||
* @param word the word searched
|
||||
* @return the found group
|
||||
* @throws IOException
|
||||
* @throws UnsupportedFormatException
|
||||
*/
|
||||
private static CharGroupInfo findWordByBinaryDictReader(final Ver3DictDecoder dictDecoder,
|
||||
final String word) throws IOException, UnsupportedFormatException {
|
||||
int position = BinaryDictIOUtils.getTerminalPosition(dictDecoder, word);
|
||||
final DictBuffer dictBuffer = dictDecoder.getDictBuffer();
|
||||
if (position != FormatSpec.NOT_VALID_WORD) {
|
||||
dictBuffer.position(0);
|
||||
final FileHeader header = dictDecoder.readHeader();
|
||||
dictBuffer.position(position);
|
||||
return dictDecoder.readPtNode(position, header.mFormatOptions);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private CharGroupInfo findWordFromFile(final File file, final String word) {
|
||||
final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file);
|
||||
CharGroupInfo info = null;
|
||||
try {
|
||||
dictDecoder.openDictBuffer(
|
||||
new Ver3DictDecoder.DictionaryBufferFromReadOnlyByteBufferFactory());
|
||||
info = BinaryDictIOUtils.findWordByBinaryDictReader(dictDecoder, word);
|
||||
info = findWordByBinaryDictReader(dictDecoder, word);
|
||||
} catch (IOException e) {
|
||||
} catch (UnsupportedFormatException e) {
|
||||
}
|
||||
|
|
|
@ -21,17 +21,18 @@ import android.test.AndroidTestCase;
|
|||
import android.test.suitebuilder.annotation.LargeTest;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.inputmethod.latin.makedict.DictEncoder;
|
||||
import com.android.inputmethod.latin.makedict.FormatSpec;
|
||||
import com.android.inputmethod.latin.makedict.FusionDictionary;
|
||||
import com.android.inputmethod.latin.makedict.FusionDictionary.CharGroup;
|
||||
import com.android.inputmethod.latin.makedict.Ver3DictDecoder;
|
||||
import com.android.inputmethod.latin.makedict.Ver3DictEncoder;
|
||||
import com.android.inputmethod.latin.personalization.UserHistoryDictionaryBigramList;
|
||||
import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils.BigramDictionaryInterface;
|
||||
import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils.OnAddWordListener;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
@ -136,14 +137,8 @@ public class UserHistoryDictIOUtilsTests extends AndroidTestCase
|
|||
|
||||
private void writeDictToFile(final File file,
|
||||
final UserHistoryDictionaryBigramList bigramList) {
|
||||
try {
|
||||
final FileOutputStream out = new FileOutputStream(file);
|
||||
UserHistoryDictIOUtils.writeDictionaryBinary(out, this, bigramList, FORMAT_OPTIONS);
|
||||
out.flush();
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "IO exception while writing file", e);
|
||||
}
|
||||
final DictEncoder dictEncoder = new Ver3DictEncoder(file);
|
||||
UserHistoryDictIOUtils.writeDictionary(dictEncoder, this, bigramList, FORMAT_OPTIONS);
|
||||
}
|
||||
|
||||
private void readDictFromFile(final File file, final OnAddWordListener listener) {
|
||||
|
|
Loading…
Reference in New Issue