• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.latin.makedict.FormatSpec.DictionaryOptions;
20 import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions;
21 
22 import javax.annotation.Nonnull;
23 import javax.annotation.Nullable;
24 
25 /**
26  * Class representing dictionary header.
27  */
28 public final class DictionaryHeader {
29     public final int mBodyOffset;
30     @Nonnull
31     public final DictionaryOptions mDictionaryOptions;
32     @Nonnull
33     public final FormatOptions mFormatOptions;
34     @Nonnull
35     public final String mLocaleString;
36     @Nonnull
37     public final String mVersionString;
38     @Nonnull
39     public final String mIdString;
40 
41     // Note that these are corresponding definitions in native code in latinime::HeaderPolicy
42     // and latinime::HeaderReadWriteUtils.
43     // TODO: Standardize the key names and bump up the format version, taking care not to
44     // break format version 2 dictionaries.
45     public static final String DICTIONARY_VERSION_KEY = "version";
46     public static final String DICTIONARY_LOCALE_KEY = "locale";
47     public static final String DICTIONARY_ID_KEY = "dictionary";
48     public static final String DICTIONARY_DESCRIPTION_KEY = "description";
49     public static final String DICTIONARY_DATE_KEY = "date";
50     public static final String HAS_HISTORICAL_INFO_KEY = "HAS_HISTORICAL_INFO";
51     public static final String USES_FORGETTING_CURVE_KEY = "USES_FORGETTING_CURVE";
52     public static final String FORGETTING_CURVE_PROBABILITY_VALUES_TABLE_ID_KEY =
53             "FORGETTING_CURVE_PROBABILITY_VALUES_TABLE_ID";
54     public static final String MAX_UNIGRAM_COUNT_KEY = "MAX_UNIGRAM_ENTRY_COUNT";
55     public static final String MAX_BIGRAM_COUNT_KEY = "MAX_BIGRAM_ENTRY_COUNT";
56     public static final String MAX_TRIGRAM_COUNT_KEY = "MAX_TRIGRAM_ENTRY_COUNT";
57     public static final String ATTRIBUTE_VALUE_TRUE = "1";
58     public static final String CODE_POINT_TABLE_KEY = "codePointTable";
59 
DictionaryHeader(final int headerSize, @Nonnull final DictionaryOptions dictionaryOptions, @Nonnull final FormatOptions formatOptions)60     public DictionaryHeader(final int headerSize,
61             @Nonnull final DictionaryOptions dictionaryOptions,
62             @Nonnull final FormatOptions formatOptions) throws UnsupportedFormatException {
63         mDictionaryOptions = dictionaryOptions;
64         mFormatOptions = formatOptions;
65         mBodyOffset = formatOptions.mVersion < FormatSpec.VERSION4 ? headerSize : 0;
66         final String localeString = dictionaryOptions.mAttributes.get(DICTIONARY_LOCALE_KEY);
67         if (null == localeString) {
68             throw new UnsupportedFormatException("Cannot create a FileHeader without a locale");
69         }
70         final String versionString = dictionaryOptions.mAttributes.get(DICTIONARY_VERSION_KEY);
71         if (null == versionString) {
72             throw new UnsupportedFormatException(
73                     "Cannot create a FileHeader without a version");
74         }
75         final String idString = dictionaryOptions.mAttributes.get(DICTIONARY_ID_KEY);
76         if (null == idString) {
77             throw new UnsupportedFormatException("Cannot create a FileHeader without an ID");
78         }
79         mLocaleString = localeString;
80         mVersionString = versionString;
81         mIdString = idString;
82     }
83 
84     // Helper method to get the description
85     @Nullable
86     public String getDescription() {
87         // TODO: Right now each dictionary file comes with a description in its own language.
88         // It will display as is no matter the device's locale. It should be internationalized.
89         return mDictionaryOptions.mAttributes.get(DICTIONARY_DESCRIPTION_KEY);
90     }
91 }