SpacingAndPunctuations.java revision 367a35d377076e387a542560f033434959d72d1d
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.keyboard.internal.MoreKeySpec; 23import com.android.inputmethod.latin.Constants; 24import com.android.inputmethod.latin.Dictionary; 25import com.android.inputmethod.latin.R; 26import com.android.inputmethod.latin.SuggestedWords; 27import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; 28import com.android.inputmethod.latin.utils.CollectionUtils; 29import com.android.inputmethod.latin.utils.StringUtils; 30 31import java.util.ArrayList; 32import java.util.Arrays; 33import java.util.Locale; 34 35public final class SpacingAndPunctuations { 36 private final int[] mSortedSymbolsPrecededBySpace; 37 private final int[] mSortedSymbolsFollowedBySpace; 38 private final int[] mSortedWordConnectors; 39 public final int[] mSortedWordSeparators; 40 public final SuggestedWords mSuggestPuncList; 41 private final int mSentenceSeparator; 42 public final String mSentenceSeparatorAndSpace; 43 public final boolean mCurrentLanguageHasSpaces; 44 public final boolean mUsesAmericanTypography; 45 public final boolean mUsesGermanRules; 46 47 public SpacingAndPunctuations(final Resources res) { 48 // To be able to binary search the code point. See {@link #isUsuallyPrecededBySpace(int)}. 49 mSortedSymbolsPrecededBySpace = StringUtils.toSortedCodePointArray( 50 res.getString(R.string.symbols_preceded_by_space)); 51 // To be able to binary search the code point. See {@link #isUsuallyFollowedBySpace(int)}. 52 mSortedSymbolsFollowedBySpace = StringUtils.toSortedCodePointArray( 53 res.getString(R.string.symbols_followed_by_space)); 54 // To be able to binary search the code point. See {@link #isWordConnector(int)}. 55 mSortedWordConnectors = StringUtils.toSortedCodePointArray( 56 res.getString(R.string.symbols_word_connectors)); 57 mSortedWordSeparators = StringUtils.toSortedCodePointArray( 58 res.getString(R.string.symbols_word_separators)); 59 final String[] suggestPuncsSpec = MoreKeySpec.splitKeySpecs(res.getString( 60 R.string.suggested_punctuations)); 61 mSuggestPuncList = createSuggestPuncList(suggestPuncsSpec); 62 mSentenceSeparator = res.getInteger(R.integer.sentence_separator); 63 mSentenceSeparatorAndSpace = new String(new int[] { 64 mSentenceSeparator, Constants.CODE_SPACE }, 0, 2); 65 mCurrentLanguageHasSpaces = res.getBoolean(R.bool.current_language_has_spaces); 66 final Locale locale = res.getConfiguration().locale; 67 // Heuristic: we use American Typography rules because it's the most common rules for all 68 // English variants. German rules (not "German typography") also have small gotchas. 69 mUsesAmericanTypography = Locale.ENGLISH.getLanguage().equals(locale.getLanguage()); 70 mUsesGermanRules = Locale.GERMAN.getLanguage().equals(locale.getLanguage()); 71 } 72 73 // Helper functions to create member values. 74 private static SuggestedWords createSuggestPuncList(final String[] puncs) { 75 final ArrayList<SuggestedWordInfo> puncList = CollectionUtils.newArrayList(); 76 if (puncs != null) { 77 for (final String puncSpec : puncs) { 78 // TODO: Stop using KeySpecParser.getLabel(). 79 // TODO: Punctuation suggestions should honor RTL languages. 80 puncList.add(new SuggestedWordInfo(KeySpecParser.getLabel(puncSpec), 81 SuggestedWordInfo.MAX_SCORE, SuggestedWordInfo.KIND_HARDCODED, 82 Dictionary.DICTIONARY_HARDCODED, 83 SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, 84 SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */)); 85 } 86 } 87 return new SuggestedWords(puncList, null /* rawSuggestions */, 88 false /* typedWordValid */, 89 false /* hasAutoCorrectionCandidate */, 90 true /* isPunctuationSuggestions */, 91 false /* isObsoleteSuggestions */, 92 false /* isPrediction */); 93 } 94 95 public boolean isWordSeparator(final int code) { 96 return Arrays.binarySearch(mSortedWordSeparators, code) >= 0; 97 } 98 99 public boolean isWordConnector(final int code) { 100 return Arrays.binarySearch(mSortedWordConnectors, code) >= 0; 101 } 102 103 public boolean isWordCodePoint(final int code) { 104 return Character.isLetter(code) || isWordConnector(code); 105 } 106 107 public boolean isUsuallyPrecededBySpace(final int code) { 108 return Arrays.binarySearch(mSortedSymbolsPrecededBySpace, code) >= 0; 109 } 110 111 public boolean isUsuallyFollowedBySpace(final int code) { 112 return Arrays.binarySearch(mSortedSymbolsFollowedBySpace, code) >= 0; 113 } 114 115 public boolean isSentenceSeparator(final int code) { 116 return code == mSentenceSeparator; 117 } 118 119 public String dump() { 120 final StringBuilder sb = new StringBuilder(); 121 sb.append("mSortedSymbolsPrecededBySpace = "); 122 sb.append("" + Arrays.toString(mSortedSymbolsPrecededBySpace)); 123 sb.append("\n mSortedSymbolsFollowedBySpace = "); 124 sb.append("" + Arrays.toString(mSortedSymbolsFollowedBySpace)); 125 sb.append("\n mSortedWordConnectors = "); 126 sb.append("" + Arrays.toString(mSortedWordConnectors)); 127 sb.append("\n mSortedWordSeparators = "); 128 sb.append("" + Arrays.toString(mSortedWordSeparators)); 129 sb.append("\n mSuggestPuncList = "); 130 sb.append("" + mSuggestPuncList); 131 sb.append("\n mSentenceSeparator = "); 132 sb.append("" + mSentenceSeparator); 133 sb.append("\n mSentenceSeparatorAndSpace = "); 134 sb.append("" + mSentenceSeparatorAndSpace); 135 sb.append("\n mCurrentLanguageHasSpaces = "); 136 sb.append("" + mCurrentLanguageHasSpaces); 137 sb.append("\n mUsesAmericanTypography = "); 138 sb.append("" + mUsesAmericanTypography); 139 sb.append("\n mUsesGermanRules = "); 140 sb.append("" + mUsesGermanRules); 141 return sb.toString(); 142 } 143} 144