2013-07-04 12:06:04 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2013-08-20 12:35:21 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.DictEncoder;
|
2013-07-04 12:06:04 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
|
2013-12-12 06:08:10 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.Ver2DictEncoder;
|
2013-07-04 12:06:04 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2013-09-28 03:50:09 +00:00
|
|
|
import java.util.Map;
|
2013-07-04 12:06:04 +00:00
|
|
|
|
2013-12-13 08:09:16 +00:00
|
|
|
abstract public class AbstractDictionaryWriter {
|
2013-07-04 12:06:04 +00:00
|
|
|
/** Used for Log actions from this class */
|
|
|
|
private static final String TAG = AbstractDictionaryWriter.class.getSimpleName();
|
|
|
|
|
|
|
|
private final Context mContext;
|
|
|
|
|
2013-12-13 08:09:16 +00:00
|
|
|
public AbstractDictionaryWriter(final Context context) {
|
2013-07-04 12:06:04 +00:00
|
|
|
mContext = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract public void clear();
|
|
|
|
|
2013-10-04 14:26:18 +00:00
|
|
|
/**
|
|
|
|
* Add a unigram with an optional shortcut to the dictionary.
|
|
|
|
* @param word The word to add.
|
|
|
|
* @param shortcutTarget A shortcut target for this word, or null if none.
|
|
|
|
* @param frequency The frequency for this unigram.
|
|
|
|
* @param shortcutFreq The frequency of the shortcut (0~15, with 15 = whitelist). Ignored
|
|
|
|
* if shortcutTarget is null.
|
|
|
|
* @param isNotAWord true if this is not a word, i.e. shortcut only.
|
|
|
|
*/
|
2013-07-04 12:06:04 +00:00
|
|
|
abstract public void addUnigramWord(final String word, final String shortcutTarget,
|
2013-10-04 14:26:18 +00:00
|
|
|
final int frequency, final int shortcutFreq, final boolean isNotAWord);
|
2013-07-04 12:06:04 +00:00
|
|
|
|
2013-08-16 11:16:31 +00:00
|
|
|
// TODO: Remove lastModifiedTime after making binary dictionary support forgetting curve.
|
2013-07-04 12:06:04 +00:00
|
|
|
abstract public void addBigramWords(final String word0, final String word1,
|
2013-12-13 08:09:16 +00:00
|
|
|
final int frequency, final boolean isValid, final long lastModifiedTime);
|
2013-07-04 12:06:04 +00:00
|
|
|
|
|
|
|
abstract public void removeBigramWords(final String word0, final String word1);
|
|
|
|
|
2013-09-28 03:50:09 +00:00
|
|
|
abstract protected void writeDictionary(final DictEncoder dictEncoder,
|
|
|
|
final Map<String, String> attributeMap) throws IOException, UnsupportedFormatException;
|
2013-07-04 12:06:04 +00:00
|
|
|
|
2013-12-13 08:09:16 +00:00
|
|
|
public void write(final File file, final Map<String, String> attributeMap) {
|
|
|
|
final String tempFilePath = file.getAbsolutePath() + ".temp";
|
|
|
|
final File tempFile = new File(tempFilePath);
|
2013-07-04 12:06:04 +00:00
|
|
|
try {
|
2013-12-12 06:08:10 +00:00
|
|
|
final DictEncoder dictEncoder = new Ver2DictEncoder(tempFile);
|
2013-09-28 03:50:09 +00:00
|
|
|
writeDictionary(dictEncoder, attributeMap);
|
2013-07-04 12:06:04 +00:00
|
|
|
tempFile.renameTo(file);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.e(TAG, "IO exception while writing file", e);
|
|
|
|
} catch (UnsupportedFormatException e) {
|
|
|
|
Log.e(TAG, "Unsupported format", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|