SpacingAndPunctuations.java revision 837cdd738b7ddbeac04b15230f01e44d247bd50a
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[] mSortedSymbolsPrecededBySpace; 36 private final int[] mSortedSymbolsFollowedBySpace; 37 private final int[] mSortedWordConnectors; 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 // To be able to binary search the code point. See {@link #isUsuallyPrecededBySpace(int)}. 48 mSortedSymbolsPrecededBySpace = StringUtils.toSortedCodePointArray( 49 res.getString(R.string.symbols_preceded_by_space)); 50 // To be able to binary search the code point. See {@link #isUsuallyFollowedBySpace(int)}. 51 mSortedSymbolsFollowedBySpace = StringUtils.toSortedCodePointArray( 52 res.getString(R.string.symbols_followed_by_space)); 53 // To be able to binary search the code point. See {@link #isWordConnector(int)}. 54 mSortedWordConnectors = StringUtils.toSortedCodePointArray( 55 res.getString(R.string.symbols_word_connectors)); 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 // TODO: Punctuation suggestions should honor RTL languages. 78 puncList.add(new SuggestedWordInfo(KeySpecParser.getLabel(puncSpec), 79 SuggestedWordInfo.MAX_SCORE, SuggestedWordInfo.KIND_HARDCODED, 80 Dictionary.DICTIONARY_HARDCODED, 81 SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, 82 SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */)); 83 } 84 } 85 return new SuggestedWords(puncList, 86 false /* typedWordValid */, 87 false /* hasAutoCorrectionCandidate */, 88 true /* isPunctuationSuggestions */, 89 false /* isObsoleteSuggestions */, 90 false /* isPrediction */); 91 } 92 93 public boolean isWordSeparator(final int code) { 94 return mWordSeparators.contains(String.valueOf((char)code)); 95 } 96 97 public boolean isWordConnector(final int code) { 98 return Arrays.binarySearch(mSortedWordConnectors, code) >= 0; 99 } 100 101 public boolean isWordCodePoint(final int code) { 102 return Character.isLetter(code) || isWordConnector(code); 103 } 104 105 public boolean isUsuallyPrecededBySpace(final int code) { 106 return Arrays.binarySearch(mSortedSymbolsPrecededBySpace, code) >= 0; 107 } 108 109 public boolean isUsuallyFollowedBySpace(final int code) { 110 return Arrays.binarySearch(mSortedSymbolsFollowedBySpace, code) >= 0; 111 } 112 113 public boolean isSentenceSeparator(final int code) { 114 return code == mSentenceSeparator; 115 } 116} 117