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