SpacingAndPunctuations.java revision 60afa7000f14f8f8ca890236f636d45a2b59b61e
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
17package com.android.inputmethod.latin.settings;
18
19import android.content.res.Resources;
20
21import com.android.inputmethod.keyboard.internal.KeySpecParser;
22import com.android.inputmethod.latin.Constants;
23import com.android.inputmethod.latin.Dictionary;
24import com.android.inputmethod.latin.R;
25import com.android.inputmethod.latin.SuggestedWords;
26import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
27import com.android.inputmethod.latin.utils.CollectionUtils;
28import com.android.inputmethod.latin.utils.StringUtils;
29
30import java.util.ArrayList;
31import java.util.Arrays;
32import java.util.Locale;
33
34public final class SpacingAndPunctuations {
35    private final int[] mSymbolsPrecededBySpace;
36    private final int[] mSymbolsFollowedBySpace;
37    private final int[] mWordConnectors;
38    public final SuggestedWords mSuggestPuncList;
39    public final String mWordSeparators;
40    private final int mSentenceSeparator;
41    public final String mSentenceSeparatorAndSpace;
42    public final boolean mCurrentLanguageHasSpaces;
43    public final boolean mUsesAmericanTypography;
44    public final boolean mUsesGermanRules;
45
46    public SpacingAndPunctuations(final Resources res) {
47        mSymbolsPrecededBySpace =
48                StringUtils.toCodePointArray(res.getString(R.string.symbols_preceded_by_space));
49        Arrays.sort(mSymbolsPrecededBySpace);
50        mSymbolsFollowedBySpace =
51                StringUtils.toCodePointArray(res.getString(R.string.symbols_followed_by_space));
52        Arrays.sort(mSymbolsFollowedBySpace);
53        mWordConnectors =
54                StringUtils.toCodePointArray(res.getString(R.string.symbols_word_connectors));
55        Arrays.sort(mWordConnectors);
56        final String[] suggestPuncsSpec = KeySpecParser.splitKeySpecs(res.getString(
57                R.string.suggested_punctuations));
58        mSuggestPuncList = createSuggestPuncList(suggestPuncsSpec);
59        mWordSeparators = res.getString(R.string.symbols_word_separators);
60        mSentenceSeparator = res.getInteger(R.integer.sentence_separator);
61        mSentenceSeparatorAndSpace = new String(new int[] {
62                mSentenceSeparator, Constants.CODE_SPACE }, 0, 2);
63        mCurrentLanguageHasSpaces = res.getBoolean(R.bool.current_language_has_spaces);
64        final Locale locale = res.getConfiguration().locale;
65        // Heuristic: we use American Typography rules because it's the most common rules for all
66        // English variants. German rules (not "German typography") also have small gotchas.
67        mUsesAmericanTypography = Locale.ENGLISH.getLanguage().equals(locale.getLanguage());
68        mUsesGermanRules = Locale.GERMAN.getLanguage().equals(locale.getLanguage());
69    }
70
71    // Helper functions to create member values.
72    private static SuggestedWords createSuggestPuncList(final String[] puncs) {
73        final ArrayList<SuggestedWordInfo> puncList = CollectionUtils.newArrayList();
74        if (puncs != null) {
75            for (final String puncSpec : puncs) {
76                // TODO: Stop using KeySpceParser.getLabel().
77                puncList.add(new SuggestedWordInfo(KeySpecParser.getLabel(puncSpec),
78                        SuggestedWordInfo.MAX_SCORE, SuggestedWordInfo.KIND_HARDCODED,
79                        Dictionary.DICTIONARY_HARDCODED,
80                        SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */,
81                        SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */));
82            }
83        }
84        return new SuggestedWords(puncList,
85                false /* typedWordValid */,
86                false /* hasAutoCorrectionCandidate */,
87                true /* isPunctuationSuggestions */,
88                false /* isObsoleteSuggestions */,
89                false /* isPrediction */);
90    }
91
92    public boolean isWordSeparator(final int code) {
93        return mWordSeparators.contains(String.valueOf((char)code));
94    }
95
96    public boolean isWordConnector(final int code) {
97        return Arrays.binarySearch(mWordConnectors, code) >= 0;
98    }
99
100    public boolean isWordCodePoint(final int code) {
101        return Character.isLetter(code) || isWordConnector(code);
102    }
103
104    public boolean isUsuallyPrecededBySpace(final int code) {
105        return Arrays.binarySearch(mSymbolsPrecededBySpace, code) >= 0;
106    }
107
108    public boolean isUsuallyFollowedBySpace(final int code) {
109        return Arrays.binarySearch(mSymbolsFollowedBySpace, code) >= 0;
110    }
111
112    public boolean isSentenceSeparator(final int code) {
113        return code == mSentenceSeparator;
114    }
115}
116