Merge "Add DictEncoder."

This commit is contained in:
Ken Wakasa 2013-08-22 05:38:08 +00:00 committed by Android (Google) Code Review
commit fef693ea98
8 changed files with 122 additions and 41 deletions

View File

@ -19,10 +19,11 @@ package com.android.inputmethod.latin;
import android.content.Context;
import android.util.Log;
import com.android.inputmethod.latin.makedict.DictEncoder;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
import com.android.inputmethod.latin.makedict.Ver3DictEncoder;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
// TODO: Quit extending Dictionary after implementing dynamic binary dictionary.
@ -49,32 +50,21 @@ abstract public class AbstractDictionaryWriter extends Dictionary {
abstract public void removeBigramWords(final String word0, final String word1);
abstract protected void writeBinaryDictionary(final FileOutputStream out)
abstract protected void writeDictionary(final DictEncoder dictEncoder)
throws IOException, UnsupportedFormatException;
public void write(final String fileName) {
final String tempFileName = fileName + ".temp";
final File file = new File(mContext.getFilesDir(), fileName);
final File tempFile = new File(mContext.getFilesDir(), tempFileName);
FileOutputStream out = null;
try {
out = new FileOutputStream(tempFile);
writeBinaryDictionary(out);
out.flush();
out.close();
final DictEncoder dictEncoder = new Ver3DictEncoder(file);
writeDictionary(dictEncoder);
tempFile.renameTo(file);
} catch (IOException e) {
Log.e(TAG, "IO exception while writing file", e);
} catch (UnsupportedFormatException e) {
Log.e(TAG, "Unsupported format", e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// ignore
}
}
}
}
}

View File

@ -20,7 +20,7 @@ import android.content.Context;
import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.makedict.BinaryDictEncoderUtils;
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.PtNodeArray;
@ -28,7 +28,6 @@ import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
import com.android.inputmethod.latin.utils.CollectionUtils;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
@ -85,9 +84,9 @@ public class DictionaryWriter extends AbstractDictionaryWriter {
}
@Override
protected void writeBinaryDictionary(final FileOutputStream out)
protected void writeDictionary(final DictEncoder dictEncoder)
throws IOException, UnsupportedFormatException {
BinaryDictEncoderUtils.writeDictionaryBinary(out, mFusionDictionary, FORMAT_OPTIONS);
dictEncoder.writeDictionary(mFusionDictionary, FORMAT_OPTIONS);
}
@Override

View File

@ -0,0 +1,29 @@
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.inputmethod.latin.makedict;
import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions;
import java.io.IOException;
/**
* An interface of binary dictionary encoder.
*/
public interface DictEncoder {
public void writeDictionary(final FusionDictionary dict, final FormatOptions formatOptions)
throws IOException, UnsupportedFormatException;
}

View File

@ -0,0 +1,68 @@
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.inputmethod.latin.makedict;
import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* An implementation of DictEncoder for version 3 binary dictionary.
*/
public class Ver3DictEncoder implements DictEncoder {
private final File mDictFile;
private OutputStream mOutStream;
public Ver3DictEncoder(final File dictFile) {
mDictFile = dictFile;
mOutStream = null;
}
// This constructor is used only by BinaryDictOffdeviceUtilsTests.
// If you want to use this in the production code, you should consider keeping consistency of
// the interface of Ver3DictDecoder by using factory.
public Ver3DictEncoder(final OutputStream outStream) {
mDictFile = null;
mOutStream = outStream;
}
private void openStream() throws FileNotFoundException {
mOutStream = new FileOutputStream(mDictFile);
}
private void close() throws IOException {
if (mOutStream != null) {
mOutStream.close();
mOutStream = null;
}
}
@Override
public void writeDictionary(FusionDictionary dict, FormatOptions formatOptions)
throws IOException, UnsupportedFormatException {
if (mOutStream == null) {
openStream();
}
BinaryDictEncoderUtils.writeDictionaryBinary(mOutStream, dict, formatOptions);
close();
}
}

View File

@ -32,7 +32,6 @@ import com.android.inputmethod.latin.utils.CollectionUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
@ -204,17 +203,14 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
long now = -1, diff = -1;
try {
final FileOutputStream out = new FileOutputStream(file);
final DictEncoder dictEncoder = new Ver3DictEncoder(file);
now = System.currentTimeMillis();
// If you need to dump the dict to a textual file, uncomment the line below and the
// function above
// dumpToCombinedFileForDebug(file, "/tmp/foo");
BinaryDictEncoderUtils.writeDictionaryBinary(out, dict, formatOptions);
dictEncoder.writeDictionary(dict, formatOptions);
diff = System.currentTimeMillis() - now;
out.flush();
out.close();
} catch (IOException e) {
Log.e(TAG, "IO exception while writing file", e);
} catch (UnsupportedFormatException e) {

View File

@ -252,9 +252,8 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
dict.add("abcd", 10, null, false);
try {
final FileOutputStream out = new FileOutputStream(file);
BinaryDictEncoderUtils.writeDictionaryBinary(out, dict, FORMAT_OPTIONS);
out.close();
final DictEncoder dictEncoder = new Ver3DictEncoder(file);
dictEncoder.writeDictionary(dict, FORMAT_OPTIONS);
} catch (IOException e) {
fail("IOException while writing an initial dictionary : " + e);
} catch (UnsupportedFormatException e) {
@ -304,9 +303,8 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
dict.add("efgh", 15, null, false);
try {
final FileOutputStream out = new FileOutputStream(file);
BinaryDictEncoderUtils.writeDictionaryBinary(out, dict, FORMAT_OPTIONS);
out.close();
final DictEncoder dictEncoder = new Ver3DictEncoder(file);
dictEncoder.writeDictionary(dict, FORMAT_OPTIONS);
} catch (IOException e) {
fail("IOException while writing an initial dictionary : " + e);
} catch (UnsupportedFormatException e) {
@ -342,9 +340,8 @@ public class BinaryDictIOUtilsTests extends AndroidTestCase {
dict.add("initial", 10, null, false);
try {
final FileOutputStream out = new FileOutputStream(file);
BinaryDictEncoderUtils.writeDictionaryBinary(out, dict, FORMAT_OPTIONS);
out.close();
final DictEncoder dictEncoder = new Ver3DictEncoder(file);
dictEncoder.writeDictionary(dict, FORMAT_OPTIONS);
} catch (IOException e) {
assertTrue(false);
} catch (UnsupportedFormatException e) {

View File

@ -17,18 +17,18 @@
package com.android.inputmethod.latin.dicttool;
import com.android.inputmethod.latin.makedict.BinaryDictDecoderUtils;
import com.android.inputmethod.latin.makedict.BinaryDictEncoderUtils;
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.MakedictLog;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
import com.android.inputmethod.latin.makedict.Ver3DictDecoder;
import com.android.inputmethod.latin.makedict.Ver3DictEncoder;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
@ -358,8 +358,8 @@ public class DictionaryMaker {
throws FileNotFoundException, IOException, UnsupportedFormatException {
final File outputFile = new File(outputFilename);
final FormatSpec.FormatOptions formatOptions = new FormatSpec.FormatOptions(version);
BinaryDictEncoderUtils.writeDictionaryBinary(new FileOutputStream(outputFilename), dict,
formatOptions);
final DictEncoder dictEncoder = new Ver3DictEncoder(outputFile);
dictEncoder.writeDictionary(dict, formatOptions);
}
/**

View File

@ -17,13 +17,14 @@
package com.android.inputmethod.latin.dicttool;
import com.android.inputmethod.latin.makedict.BinaryDictDecoderUtils;
import com.android.inputmethod.latin.makedict.BinaryDictEncoderUtils;
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.DictionaryOptions;
import com.android.inputmethod.latin.makedict.FusionDictionary.PtNodeArray;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
import com.android.inputmethod.latin.makedict.Ver3DictDecoder;
import com.android.inputmethod.latin.makedict.Ver3DictEncoder;
import junit.framework.TestCase;
@ -53,12 +54,13 @@ public class BinaryDictOffdeviceUtilsTests extends TestCase {
final File dst = File.createTempFile("testGetRawDict", ".tmp");
dst.deleteOnExit();
final OutputStream out = Compress.getCompressedStream(
Compress.getCompressedStream(
Compress.getCompressedStream(
new BufferedOutputStream(new FileOutputStream(dst)))));
BinaryDictEncoderUtils.writeDictionaryBinary(out, dict, new FormatOptions(2, false));
final DictEncoder dictEncoder = new Ver3DictEncoder(out);
dictEncoder.writeDictionary(dict, new FormatOptions(2, false));
// Test for an actually compressed dictionary and its contents
final BinaryDictOffdeviceUtils.DecoderChainSpec decodeSpec =