2013-08-20 12:05:05 +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.makedict;
|
|
|
|
|
|
|
|
import com.android.inputmethod.annotations.UsedForTesting;
|
2014-02-06 12:55:37 +00:00
|
|
|
import com.android.inputmethod.latin.BinaryDictionary;
|
|
|
|
import com.android.inputmethod.latin.utils.FileUtils;
|
2013-08-20 12:05:05 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An implementation of binary dictionary decoder for version 4 binary dictionary.
|
|
|
|
*/
|
|
|
|
@UsedForTesting
|
2013-10-01 08:59:50 +00:00
|
|
|
public class Ver4DictDecoder extends AbstractDictDecoder {
|
2014-02-06 12:55:37 +00:00
|
|
|
final File mDictDirectory;
|
2013-08-20 12:05:05 +00:00
|
|
|
|
|
|
|
@UsedForTesting
|
|
|
|
/* package */ Ver4DictDecoder(final File dictDirectory, final int factoryFlag) {
|
2014-02-06 12:55:37 +00:00
|
|
|
this(dictDirectory, null /* factory */);
|
2013-08-20 12:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@UsedForTesting
|
|
|
|
/* package */ Ver4DictDecoder(final File dictDirectory, final DictionaryBufferFactory factory) {
|
|
|
|
mDictDirectory = dictDirectory;
|
2014-02-21 08:25:34 +00:00
|
|
|
|
2013-08-20 12:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-04 12:36:04 +00:00
|
|
|
public DictionaryHeader readHeader() throws IOException, UnsupportedFormatException {
|
2014-02-21 08:25:34 +00:00
|
|
|
// dictType is not being used in dicttool. Passing an empty string.
|
|
|
|
final BinaryDictionary binaryDictionary= new BinaryDictionary(
|
|
|
|
mDictDirectory.getAbsolutePath(), 0 /* offset */, 0 /* length */,
|
|
|
|
true /* useFullEditDistance */, null /* locale */,
|
|
|
|
"" /* dictType */, true /* isUpdatable */);
|
|
|
|
final DictionaryHeader header = binaryDictionary.getHeader();
|
|
|
|
binaryDictionary.close();
|
2014-02-20 09:11:17 +00:00
|
|
|
if (header == null) {
|
|
|
|
throw new IOException("Cannot read the dictionary header.");
|
|
|
|
}
|
2014-02-21 08:25:34 +00:00
|
|
|
return header;
|
2013-08-20 12:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-14 09:31:41 +00:00
|
|
|
public FusionDictionary readDictionaryBinary(final boolean deleteDictIfBroken)
|
2013-08-20 12:05:05 +00:00
|
|
|
throws FileNotFoundException, IOException, UnsupportedFormatException {
|
2014-02-21 08:25:34 +00:00
|
|
|
// dictType is not being used in dicttool. Passing an empty string.
|
|
|
|
final BinaryDictionary binaryDictionary = new BinaryDictionary(
|
|
|
|
mDictDirectory.getAbsolutePath(), 0 /* offset */, 0 /* length */,
|
|
|
|
true /* useFullEditDistance */, null /* locale */,
|
|
|
|
"" /* dictType */, true /* isUpdatable */);
|
2014-02-06 12:55:37 +00:00
|
|
|
final DictionaryHeader header = readHeader();
|
2014-02-14 09:31:41 +00:00
|
|
|
final FusionDictionary fusionDict =
|
2014-02-06 12:55:37 +00:00
|
|
|
new FusionDictionary(new FusionDictionary.PtNodeArray(), header.mDictionaryOptions);
|
|
|
|
int token = 0;
|
2014-05-23 11:18:17 +00:00
|
|
|
final ArrayList<WordProperty> wordProperties = new ArrayList<>();
|
2014-02-06 12:55:37 +00:00
|
|
|
do {
|
|
|
|
final BinaryDictionary.GetNextWordPropertyResult result =
|
2014-02-21 08:25:34 +00:00
|
|
|
binaryDictionary.getNextWordProperty(token);
|
2014-02-06 12:55:37 +00:00
|
|
|
final WordProperty wordProperty = result.mWordProperty;
|
|
|
|
if (wordProperty == null) {
|
2014-02-21 08:25:34 +00:00
|
|
|
binaryDictionary.close();
|
2014-02-06 12:55:37 +00:00
|
|
|
if (deleteDictIfBroken) {
|
|
|
|
FileUtils.deleteRecursively(mDictDirectory);
|
|
|
|
}
|
|
|
|
return null;
|
2013-08-20 12:05:05 +00:00
|
|
|
}
|
2014-02-06 12:55:37 +00:00
|
|
|
wordProperties.add(wordProperty);
|
|
|
|
token = result.mNextToken;
|
|
|
|
} while (token != 0);
|
|
|
|
|
2014-02-15 08:39:20 +00:00
|
|
|
// Insert unigrams into the fusion dictionary.
|
2014-02-06 12:55:37 +00:00
|
|
|
for (final WordProperty wordProperty : wordProperties) {
|
2014-10-14 03:13:11 +00:00
|
|
|
fusionDict.add(wordProperty.mWord, wordProperty.mProbabilityInfo,
|
|
|
|
wordProperty.mShortcutTargets, wordProperty.mIsNotAWord,
|
|
|
|
wordProperty.mIsPossiblyOffensive);
|
2013-08-20 12:05:05 +00:00
|
|
|
}
|
2014-02-15 08:39:20 +00:00
|
|
|
// Insert bigrams into the fusion dictionary.
|
2014-10-01 02:21:08 +00:00
|
|
|
// TODO: Support ngrams.
|
2014-02-06 12:55:37 +00:00
|
|
|
for (final WordProperty wordProperty : wordProperties) {
|
2014-10-01 02:21:08 +00:00
|
|
|
if (!wordProperty.mHasNgrams) {
|
2014-02-06 12:55:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final String word0 = wordProperty.mWord;
|
2014-10-01 02:21:08 +00:00
|
|
|
for (final WeightedString bigram : wordProperty.getBigrams()) {
|
2014-02-10 06:05:08 +00:00
|
|
|
fusionDict.setBigram(word0, bigram.mWord, bigram.mProbabilityInfo);
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
2013-08-20 12:05:05 +00:00
|
|
|
}
|
2014-02-21 08:25:34 +00:00
|
|
|
binaryDictionary.close();
|
2014-02-06 12:55:37 +00:00
|
|
|
return fusionDict;
|
2013-10-01 08:21:21 +00:00
|
|
|
}
|
2013-08-20 12:05:05 +00:00
|
|
|
}
|