Ver4DictDecoder.java revision 93cda5bb396c22f1781e390debaf75d54cf7c0dc
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.inputmethod.latin.makedict;
18
19import com.android.inputmethod.annotations.UsedForTesting;
20import com.android.inputmethod.latin.BinaryDictionary;
21import com.android.inputmethod.latin.utils.CollectionUtils;
22import com.android.inputmethod.latin.utils.FileUtils;
23
24import java.io.File;
25import java.io.FileNotFoundException;
26import java.io.IOException;
27import java.util.ArrayList;
28
29/**
30 * An implementation of binary dictionary decoder for version 4 binary dictionary.
31 */
32@UsedForTesting
33public class Ver4DictDecoder extends AbstractDictDecoder {
34    private static final String TAG = Ver4DictDecoder.class.getSimpleName();
35
36    final File mDictDirectory;
37
38    @UsedForTesting
39    /* package */ Ver4DictDecoder(final File dictDirectory, final int factoryFlag) {
40        this(dictDirectory, null /* factory */);
41    }
42
43    @UsedForTesting
44    /* package */ Ver4DictDecoder(final File dictDirectory, final DictionaryBufferFactory factory) {
45        mDictDirectory = dictDirectory;
46
47    }
48
49    @Override
50    public DictionaryHeader readHeader() throws IOException, UnsupportedFormatException {
51        // dictType is not being used in dicttool. Passing an empty string.
52        final BinaryDictionary binaryDictionary= new BinaryDictionary(
53              mDictDirectory.getAbsolutePath(), 0 /* offset */, 0 /* length */,
54              true /* useFullEditDistance */, null /* locale */,
55              "" /* dictType */, true /* isUpdatable */);
56        final DictionaryHeader header = binaryDictionary.getHeader();
57        binaryDictionary.close();
58        if (header == null) {
59            throw new IOException("Cannot read the dictionary header.");
60        }
61        return header;
62    }
63
64    @Override
65    public FusionDictionary readDictionaryBinary(final boolean deleteDictIfBroken)
66            throws FileNotFoundException, IOException, UnsupportedFormatException {
67        // dictType is not being used in dicttool. Passing an empty string.
68        final BinaryDictionary binaryDictionary = new BinaryDictionary(
69              mDictDirectory.getAbsolutePath(), 0 /* offset */, 0 /* length */,
70              true /* useFullEditDistance */, null /* locale */,
71              "" /* dictType */, true /* isUpdatable */);
72        final DictionaryHeader header = readHeader();
73        final FusionDictionary fusionDict =
74                new FusionDictionary(new FusionDictionary.PtNodeArray(), header.mDictionaryOptions);
75        int token = 0;
76        final ArrayList<WordProperty> wordProperties = CollectionUtils.newArrayList();
77        do {
78            final BinaryDictionary.GetNextWordPropertyResult result =
79                    binaryDictionary.getNextWordProperty(token);
80            final WordProperty wordProperty = result.mWordProperty;
81            if (wordProperty == null) {
82                binaryDictionary.close();
83                if (deleteDictIfBroken) {
84                    FileUtils.deleteRecursively(mDictDirectory);
85                }
86                return null;
87            }
88            wordProperties.add(wordProperty);
89            token = result.mNextToken;
90        } while (token != 0);
91
92        // Insert unigrams into the fusion dictionary.
93        for (final WordProperty wordProperty : wordProperties) {
94            if (wordProperty.mIsBlacklistEntry) {
95                fusionDict.addBlacklistEntry(wordProperty.mWord, wordProperty.mShortcutTargets,
96                        wordProperty.mIsNotAWord);
97            } else {
98                fusionDict.add(wordProperty.mWord, wordProperty.mProbabilityInfo,
99                        wordProperty.mShortcutTargets, wordProperty.mIsNotAWord);
100            }
101        }
102        // Insert bigrams into the fusion dictionary.
103        for (final WordProperty wordProperty : wordProperties) {
104            if (wordProperty.mBigrams == null) {
105                continue;
106            }
107            final String word0 = wordProperty.mWord;
108            for (final WeightedString bigram : wordProperty.mBigrams) {
109                fusionDict.setBigram(word0, bigram.mWord, bigram.mProbabilityInfo);
110            }
111        }
112        binaryDictionary.close();
113        return fusionDict;
114    }
115}
116