am fef693ea: Merge "Add DictEncoder."

* commit 'fef693ea983eb4aacd3d1830e5d7608ede8ad451':
  Add DictEncoder.
main
Ken Wakasa 2013-08-21 22:39:47 -07:00 committed by Android Git Automerger
commit fb5c3956fd
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.content.Context;
import android.util.Log; import android.util.Log;
import com.android.inputmethod.latin.makedict.DictEncoder;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException; import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
import com.android.inputmethod.latin.makedict.Ver3DictEncoder;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
// TODO: Quit extending Dictionary after implementing dynamic binary dictionary. // 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 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; throws IOException, UnsupportedFormatException;
public void write(final String fileName) { public void write(final String fileName) {
final String tempFileName = fileName + ".temp"; final String tempFileName = fileName + ".temp";
final File file = new File(mContext.getFilesDir(), fileName); final File file = new File(mContext.getFilesDir(), fileName);
final File tempFile = new File(mContext.getFilesDir(), tempFileName); final File tempFile = new File(mContext.getFilesDir(), tempFileName);
FileOutputStream out = null;
try { try {
out = new FileOutputStream(tempFile); final DictEncoder dictEncoder = new Ver3DictEncoder(file);
writeBinaryDictionary(out); writeDictionary(dictEncoder);
out.flush();
out.close();
tempFile.renameTo(file); tempFile.renameTo(file);
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "IO exception while writing file", e); Log.e(TAG, "IO exception while writing file", e);
} catch (UnsupportedFormatException e) { } catch (UnsupportedFormatException e) {
Log.e(TAG, "Unsupported format", 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.keyboard.ProximityInfo;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; 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.FormatSpec;
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;
@ -28,7 +28,6 @@ import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException; import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.CollectionUtils;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -85,9 +84,9 @@ public class DictionaryWriter extends AbstractDictionaryWriter {
} }
@Override @Override
protected void writeBinaryDictionary(final FileOutputStream out) protected void writeDictionary(final DictEncoder dictEncoder)
throws IOException, UnsupportedFormatException { throws IOException, UnsupportedFormatException {
BinaryDictEncoderUtils.writeDictionaryBinary(out, mFusionDictionary, FORMAT_OPTIONS); dictEncoder.writeDictionary(mFusionDictionary, FORMAT_OPTIONS);
} }
@Override @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.File;
import java.io.FileInputStream; import java.io.FileInputStream;
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.HashMap; import java.util.HashMap;
@ -204,17 +203,14 @@ public class BinaryDictDecoderEncoderTests extends AndroidTestCase {
long now = -1, diff = -1; long now = -1, diff = -1;
try { try {
final FileOutputStream out = new FileOutputStream(file); final DictEncoder dictEncoder = new Ver3DictEncoder(file);
now = System.currentTimeMillis(); now = System.currentTimeMillis();
// If you need to dump the dict to a textual file, uncomment the line below and the // If you need to dump the dict to a textual file, uncomment the line below and the
// function above // function above
// dumpToCombinedFileForDebug(file, "/tmp/foo"); // dumpToCombinedFileForDebug(file, "/tmp/foo");
BinaryDictEncoderUtils.writeDictionaryBinary(out, dict, formatOptions); dictEncoder.writeDictionary(dict, formatOptions);
diff = System.currentTimeMillis() - now; diff = System.currentTimeMillis() - now;
out.flush();
out.close();
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "IO exception while writing file", e); Log.e(TAG, "IO exception while writing file", e);
} catch (UnsupportedFormatException e) { } catch (UnsupportedFormatException e) {

View File

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

View File

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

View File

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