• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.inputmethod.latin.makedict;
18 
19 import com.android.inputmethod.annotations.UsedForTesting;
20 import com.android.inputmethod.latin.BinaryDictionary;
21 import com.android.inputmethod.latin.common.FileUtils;
22 
23 import java.io.File;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 
28 /**
29  * An implementation of binary dictionary decoder for version 4 binary dictionary.
30  */
31 @UsedForTesting
32 public class Ver4DictDecoder extends AbstractDictDecoder {
33     final File mDictDirectory;
34 
35     @UsedForTesting
Ver4DictDecoder(final File dictDirectory)36     /* package */ Ver4DictDecoder(final File dictDirectory) {
37         mDictDirectory = dictDirectory;
38 
39     }
40 
41     @Override
readHeader()42     public DictionaryHeader readHeader() throws IOException, UnsupportedFormatException {
43         // dictType is not being used in dicttool. Passing an empty string.
44         final BinaryDictionary binaryDictionary= new BinaryDictionary(
45               mDictDirectory.getAbsolutePath(), 0 /* offset */, 0 /* length */,
46               true /* useFullEditDistance */, null /* locale */,
47               "" /* dictType */, true /* isUpdatable */);
48         final DictionaryHeader header = binaryDictionary.getHeader();
49         binaryDictionary.close();
50         if (header == null) {
51             throw new IOException("Cannot read the dictionary header.");
52         }
53         return header;
54     }
55 
56     @Override
readDictionaryBinary(final boolean deleteDictIfBroken)57     public FusionDictionary readDictionaryBinary(final boolean deleteDictIfBroken)
58             throws FileNotFoundException, IOException, UnsupportedFormatException {
59         // dictType is not being used in dicttool. Passing an empty string.
60         final BinaryDictionary binaryDictionary = new BinaryDictionary(
61               mDictDirectory.getAbsolutePath(), 0 /* offset */, 0 /* length */,
62               true /* useFullEditDistance */, null /* locale */,
63               "" /* dictType */, true /* isUpdatable */);
64         final DictionaryHeader header = readHeader();
65         final FusionDictionary fusionDict =
66                 new FusionDictionary(new FusionDictionary.PtNodeArray(), header.mDictionaryOptions);
67         int token = 0;
68         final ArrayList<WordProperty> wordProperties = new ArrayList<>();
69         do {
70             final BinaryDictionary.GetNextWordPropertyResult result =
71                     binaryDictionary.getNextWordProperty(token);
72             final WordProperty wordProperty = result.mWordProperty;
73             if (wordProperty == null) {
74                 binaryDictionary.close();
75                 if (deleteDictIfBroken) {
76                     FileUtils.deleteRecursively(mDictDirectory);
77                 }
78                 return null;
79             }
80             wordProperties.add(wordProperty);
81             token = result.mNextToken;
82         } while (token != 0);
83 
84         // Insert unigrams into the fusion dictionary.
85         for (final WordProperty wordProperty : wordProperties) {
86             fusionDict.add(wordProperty.mWord, wordProperty.mProbabilityInfo,
87                     wordProperty.mIsNotAWord,
88                     wordProperty.mIsPossiblyOffensive);
89         }
90         // Insert bigrams into the fusion dictionary.
91         // TODO: Support ngrams.
92         for (final WordProperty wordProperty : wordProperties) {
93             if (!wordProperty.mHasNgrams) {
94                 continue;
95             }
96             final String word0 = wordProperty.mWord;
97             for (final WeightedString bigram : wordProperty.getBigrams()) {
98                 fusionDict.setBigram(word0, bigram.mWord, bigram.mProbabilityInfo);
99             }
100         }
101         binaryDictionary.close();
102         return fusionDict;
103     }
104 }
105