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