aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/makedict/DictionaryHeader.java
blob: 4d253b0cb8b57139ee6978479adb8ca303ba7584 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * Copyright (C) 2014 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.latin.makedict.FormatSpec.DictionaryOptions;
import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * Class representing dictionary header.
 */
public final class DictionaryHeader {
    public final int mBodyOffset;
    @Nonnull
    public final DictionaryOptions mDictionaryOptions;
    @Nonnull
    public final FormatOptions mFormatOptions;
    @Nonnull
    public final String mLocaleString;
    @Nonnull
    public final String mVersionString;
    @Nonnull
    public final String mIdString;

    // Note that these are corresponding definitions in native code in latinime::HeaderPolicy
    // and latinime::HeaderReadWriteUtils.
    // TODO: Standardize the key names and bump up the format version, taking care not to
    // break format version 2 dictionaries.
    public static final String DICTIONARY_VERSION_KEY = "version";
    public static final String DICTIONARY_LOCALE_KEY = "locale";
    public static final String DICTIONARY_ID_KEY = "dictionary";
    public static final String DICTIONARY_DESCRIPTION_KEY = "description";
    public static final String DICTIONARY_DATE_KEY = "date";
    public static final String HAS_HISTORICAL_INFO_KEY = "HAS_HISTORICAL_INFO";
    public static final String USES_FORGETTING_CURVE_KEY = "USES_FORGETTING_CURVE";
    public static final String FORGETTING_CURVE_PROBABILITY_VALUES_TABLE_ID_KEY =
            "FORGETTING_CURVE_PROBABILITY_VALUES_TABLE_ID";
    public static final String MAX_UNIGRAM_COUNT_KEY = "MAX_UNIGRAM_ENTRY_COUNT";
    public static final String MAX_BIGRAM_COUNT_KEY = "MAX_BIGRAM_ENTRY_COUNT";
    public static final String MAX_TRIGRAM_COUNT_KEY = "MAX_TRIGRAM_ENTRY_COUNT";
    public static final String ATTRIBUTE_VALUE_TRUE = "1";
    public static final String CODE_POINT_TABLE_KEY = "codePointTable";

    public DictionaryHeader(final int headerSize,
            @Nonnull final DictionaryOptions dictionaryOptions,
            @Nonnull final FormatOptions formatOptions) throws UnsupportedFormatException {
        mDictionaryOptions = dictionaryOptions;
        mFormatOptions = formatOptions;
        mBodyOffset = formatOptions.mVersion < FormatSpec.VERSION4 ? headerSize : 0;
        final String localeString = dictionaryOptions.mAttributes.get(DICTIONARY_LOCALE_KEY);
        if (null == localeString) {
            throw new UnsupportedFormatException("Cannot create a FileHeader without a locale");
        }
        final String versionString = dictionaryOptions.mAttributes.get(DICTIONARY_VERSION_KEY);
        if (null == versionString) {
            throw new UnsupportedFormatException(
                    "Cannot create a FileHeader without a version");
        }
        final String idString = dictionaryOptions.mAttributes.get(DICTIONARY_ID_KEY);
        if (null == idString) {
            throw new UnsupportedFormatException("Cannot create a FileHeader without an ID");
        }
        mLocaleString = localeString;
        mVersionString = versionString;
        mIdString = idString;
    }

    // Helper method to get the description
    @Nullable
    public String getDescription() {
        // TODO: Right now each dictionary file comes with a description in its own language.
        // It will display as is no matter the device's locale. It should be internationalized.
        return mDictionaryOptions.mAttributes.get(DICTIONARY_DESCRIPTION_KEY);
    }
}