Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c922c8a5 authored by Yuichiro Hanada's avatar Yuichiro Hanada
Browse files

Add DictEncoder.

Change-Id: I41049b9118b58838e5dedf8e5618d939ca70c5ef
parent f9233e0b
Loading
Loading
Loading
Loading
+5 −15
Original line number Diff line number Diff line
@@ -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
                }
            }
        }
    }
}
+3 −4
Original line number Diff line number Diff line
@@ -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
+29 −0
Original line number Diff line number Diff line
/*
 * 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;
}
+68 −0
Original line number Diff line number Diff line
/*
 * 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();
    }
}
+2 −6
Original line number Diff line number Diff line
@@ -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) {
Loading