Merge "Remove unused argument from readDictionaryBinary."

main
Keisuke Kuroyanagi 2014-02-14 10:37:55 +00:00 committed by Android (Google) Code Review
commit 85fe06e759
9 changed files with 20 additions and 50 deletions

View File

@ -500,16 +500,14 @@ public final class BinaryDictDecoderUtils {
* Reads a buffer and returns the memory representation of the dictionary. * Reads a buffer and returns the memory representation of the dictionary.
* *
* This high-level method takes a buffer and reads its contents, populating a * This high-level method takes a buffer and reads its contents, populating a
* FusionDictionary structure. The optional dict argument is an existing dictionary to * FusionDictionary structure.
* which words from the buffer should be added. If it is null, a new dictionary is created.
* *
* @param dictDecoder the dict decoder. * @param dictDecoder the dict decoder.
* @param dict an optional dictionary to add words to, or null. * @return the created dictionary.
* @return the created (or merged) dictionary.
*/ */
@UsedForTesting @UsedForTesting
/* package */ static FusionDictionary readDictionaryBinary(final DictDecoder dictDecoder, /* package */ static FusionDictionary readDictionaryBinary(final DictDecoder dictDecoder)
final FusionDictionary dict) throws IOException, UnsupportedFormatException { throws IOException, UnsupportedFormatException {
// Read header // Read header
final DictionaryHeader fileHeader = dictDecoder.readHeader(); final DictionaryHeader fileHeader = dictDecoder.readHeader();
@ -517,29 +515,7 @@ public final class BinaryDictDecoderUtils {
Map<Integer, PtNode> reversePtNodeMapping = new TreeMap<Integer, PtNode>(); Map<Integer, PtNode> reversePtNodeMapping = new TreeMap<Integer, PtNode>();
final PtNodeArray root = readNodeArray(dictDecoder, fileHeader.mBodyOffset, final PtNodeArray root = readNodeArray(dictDecoder, fileHeader.mBodyOffset,
reverseNodeArrayMapping, reversePtNodeMapping, fileHeader.mFormatOptions); reverseNodeArrayMapping, reversePtNodeMapping, fileHeader.mFormatOptions);
return new FusionDictionary(root, fileHeader.mDictionaryOptions);
FusionDictionary newDict = new FusionDictionary(root, fileHeader.mDictionaryOptions);
if (null != dict) {
for (final WordProperty wordProperty : dict) {
if (wordProperty.mIsBlacklistEntry) {
newDict.addBlacklistEntry(wordProperty.mWord, wordProperty.mShortcutTargets,
wordProperty.mIsNotAWord);
} else {
newDict.add(wordProperty.mWord, wordProperty.mProbabilityInfo,
wordProperty.mShortcutTargets, wordProperty.mIsNotAWord);
}
}
for (final WordProperty wordProperty : dict) {
// By construction a binary dictionary may not have bigrams pointing to
// words that are not also registered as unigrams so we don't have to avoid
// them explicitly here.
for (final WeightedString bigram : wordProperty.mBigrams) {
newDict.setBigram(wordProperty.mWord, bigram.mWord, bigram.mProbabilityInfo);
}
}
}
return newDict;
} }
/** /**

View File

@ -54,17 +54,14 @@ public interface DictDecoder {
* Reads a buffer and returns the memory representation of the dictionary. * Reads a buffer and returns the memory representation of the dictionary.
* *
* This high-level method takes a buffer and reads its contents, populating a * This high-level method takes a buffer and reads its contents, populating a
* FusionDictionary structure. The optional dict argument is an existing dictionary to * FusionDictionary structure.
* which words from the buffer should be added. If it is null, a new dictionary is created.
* *
* @param dict an optional dictionary to add words to, or null.
* @param deleteDictIfBroken a flag indicating whether this method should remove the broken * @param deleteDictIfBroken a flag indicating whether this method should remove the broken
* dictionary or not. * dictionary or not.
* @return the created (or merged) dictionary. * @return the created dictionary.
*/ */
@UsedForTesting @UsedForTesting
public FusionDictionary readDictionaryBinary(final FusionDictionary dict, public FusionDictionary readDictionaryBinary(final boolean deleteDictIfBroken)
final boolean deleteDictIfBroken)
throws FileNotFoundException, IOException, UnsupportedFormatException; throws FileNotFoundException, IOException, UnsupportedFormatException;
/** /**

View File

@ -236,14 +236,13 @@ public class Ver2DictDecoder extends AbstractDictDecoder {
} }
@Override @Override
public FusionDictionary readDictionaryBinary(final FusionDictionary dict, public FusionDictionary readDictionaryBinary(final boolean deleteDictIfBroken)
final boolean deleteDictIfBroken)
throws FileNotFoundException, IOException, UnsupportedFormatException { throws FileNotFoundException, IOException, UnsupportedFormatException {
if (mDictBuffer == null) { if (mDictBuffer == null) {
openDictBuffer(); openDictBuffer();
} }
try { try {
return BinaryDictDecoderUtils.readDictionaryBinary(this, dict); return BinaryDictDecoderUtils.readDictionaryBinary(this);
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "The dictionary " + mDictionaryBinaryFile.getName() + " is broken.", e); Log.e(TAG, "The dictionary " + mDictionaryBinaryFile.getName() + " is broken.", e);
if (deleteDictIfBroken && !mDictionaryBinaryFile.delete()) { if (deleteDictIfBroken && !mDictionaryBinaryFile.delete()) {

View File

@ -56,11 +56,10 @@ public class Ver4DictDecoder extends AbstractDictDecoder {
} }
@Override @Override
public FusionDictionary readDictionaryBinary(final FusionDictionary dict, public FusionDictionary readDictionaryBinary(final boolean deleteDictIfBroken)
final boolean deleteDictIfBroken)
throws FileNotFoundException, IOException, UnsupportedFormatException { throws FileNotFoundException, IOException, UnsupportedFormatException {
final DictionaryHeader header = readHeader(); final DictionaryHeader header = readHeader();
final FusionDictionary fusionDict = dict != null ? dict : final FusionDictionary fusionDict =
new FusionDictionary(new FusionDictionary.PtNodeArray(), header.mDictionaryOptions); new FusionDictionary(new FusionDictionary.PtNodeArray(), header.mDictionaryOptions);
int token = 0; int token = 0;
final ArrayList<WordProperty> wordProperties = CollectionUtils.newArrayList(); final ArrayList<WordProperty> wordProperties = CollectionUtils.newArrayList();

View File

@ -152,8 +152,8 @@ public class BinaryDictionaryDecayingTests extends AndroidTestCase {
final DictDecoder dictDecoder = FormatSpec.getDictDecoder(dictFile); final DictDecoder dictDecoder = FormatSpec.getDictDecoder(dictFile);
try { try {
final FusionDictionary dict = dictDecoder.readDictionaryBinary(null, final FusionDictionary dict =
false /* deleteDictIfBroken */); dictDecoder.readDictionaryBinary(false /* deleteDictIfBroken */);
PtNode ptNode = FusionDictionary.findWordInTree(dict.mRootNodeArray, "a"); PtNode ptNode = FusionDictionary.findWordInTree(dict.mRootNodeArray, "a");
assertNotNull(ptNode); assertNotNull(ptNode);
assertTrue(ptNode.isTerminal()); assertTrue(ptNode.isTerminal());

View File

@ -252,7 +252,7 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
try { try {
final DictDecoder dictDecoder = FormatSpec.getDictDecoder(file, bufferType); final DictDecoder dictDecoder = FormatSpec.getDictDecoder(file, bufferType);
now = System.currentTimeMillis(); now = System.currentTimeMillis();
dict = dictDecoder.readDictionaryBinary(null, false /* deleteDictIfBroken */); dict = dictDecoder.readDictionaryBinary(false /* deleteDictIfBroken */);
diff = System.currentTimeMillis() - now; diff = System.currentTimeMillis() - now;
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "IOException while reading dictionary", e); Log.e(TAG, "IOException while reading dictionary", e);

View File

@ -198,7 +198,7 @@ public final class BinaryDictOffdeviceUtils {
System.out.println("Packaging : " + decodedSpec.describeChain()); System.out.println("Packaging : " + decodedSpec.describeChain());
System.out.println("Uncompressed size : " + decodedSpec.mFile.length()); System.out.println("Uncompressed size : " + decodedSpec.mFile.length());
} }
return dictDecoder.readDictionaryBinary(null, false /* deleteDictIfBroken */); return dictDecoder.readDictionaryBinary(false /* deleteDictIfBroken */);
} }
} }
} catch (IOException e) { } catch (IOException e) {

View File

@ -265,7 +265,7 @@ public class DictionaryMaker {
throws FileNotFoundException, IOException, UnsupportedFormatException { throws FileNotFoundException, IOException, UnsupportedFormatException {
final File file = new File(binaryFilename); final File file = new File(binaryFilename);
final DictDecoder dictDecoder = FormatSpec.getDictDecoder(file); final DictDecoder dictDecoder = FormatSpec.getDictDecoder(file);
return dictDecoder.readDictionaryBinary(null, false /* deleteDictIfBroken */); return dictDecoder.readDictionaryBinary(false /* deleteDictIfBroken */);
} }
/** /**

View File

@ -78,9 +78,8 @@ public class BinaryDictOffdeviceUtilsTests extends TestCase {
} }
assertEquals("Wrong decode spec", 3, decodeSpec.mDecoderSpec.size()); assertEquals("Wrong decode spec", 3, decodeSpec.mDecoderSpec.size());
final DictDecoder dictDecoder = FormatSpec.getDictDecoder(decodeSpec.mFile); final DictDecoder dictDecoder = FormatSpec.getDictDecoder(decodeSpec.mFile);
final FusionDictionary resultDict = dictDecoder.readDictionaryBinary( final FusionDictionary resultDict =
null /* dict : an optional dictionary to add words to, or null */, dictDecoder.readDictionaryBinary(false /* deleteDictIfBroken */);
false /* deleteDictIfBroken */);
assertEquals("Wrong version attribute", VERSION, resultDict.mOptions.mAttributes.get( assertEquals("Wrong version attribute", VERSION, resultDict.mOptions.mAttributes.get(
DictionaryHeader.DICTIONARY_VERSION_KEY)); DictionaryHeader.DICTIONARY_VERSION_KEY));
assertEquals("Wrong locale attribute", LOCALE, resultDict.mOptions.mAttributes.get( assertEquals("Wrong locale attribute", LOCALE, resultDict.mOptions.mAttributes.get(