2014-01-08 09:06:32 +00:00
|
|
|
/*
|
|
|
|
* 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.settings;
|
|
|
|
|
|
|
|
import android.content.res.Resources;
|
|
|
|
|
2014-09-03 06:27:01 +00:00
|
|
|
import com.android.inputmethod.annotations.UsedForTesting;
|
2014-01-31 06:58:14 +00:00
|
|
|
import com.android.inputmethod.keyboard.internal.MoreKeySpec;
|
2014-01-08 09:06:32 +00:00
|
|
|
import com.android.inputmethod.latin.Constants;
|
2014-02-13 09:43:48 +00:00
|
|
|
import com.android.inputmethod.latin.PunctuationSuggestions;
|
2014-01-08 09:06:32 +00:00
|
|
|
import com.android.inputmethod.latin.R;
|
|
|
|
import com.android.inputmethod.latin.utils.StringUtils;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
2014-01-09 07:59:23 +00:00
|
|
|
import java.util.Locale;
|
2014-01-08 09:06:32 +00:00
|
|
|
|
|
|
|
public final class SpacingAndPunctuations {
|
2014-01-20 06:41:50 +00:00
|
|
|
private final int[] mSortedSymbolsPrecededBySpace;
|
|
|
|
private final int[] mSortedSymbolsFollowedBySpace;
|
2014-05-01 03:33:59 +00:00
|
|
|
private final int[] mSortedSymbolsClusteringTogether;
|
2014-01-20 06:41:50 +00:00
|
|
|
private final int[] mSortedWordConnectors;
|
2014-01-20 05:56:56 +00:00
|
|
|
public final int[] mSortedWordSeparators;
|
2014-02-13 09:43:48 +00:00
|
|
|
public final PunctuationSuggestions mSuggestPuncList;
|
2014-01-08 09:06:32 +00:00
|
|
|
private final int mSentenceSeparator;
|
2014-09-05 07:49:32 +00:00
|
|
|
private final int mAbbreviationMarker;
|
|
|
|
private final int[] mSortedSentenceTerminators;
|
2014-01-08 09:06:32 +00:00
|
|
|
public final String mSentenceSeparatorAndSpace;
|
|
|
|
public final boolean mCurrentLanguageHasSpaces;
|
2014-01-09 07:59:23 +00:00
|
|
|
public final boolean mUsesAmericanTypography;
|
2014-01-15 19:32:32 +00:00
|
|
|
public final boolean mUsesGermanRules;
|
2014-01-08 09:06:32 +00:00
|
|
|
|
|
|
|
public SpacingAndPunctuations(final Resources res) {
|
2014-01-20 06:41:50 +00:00
|
|
|
// To be able to binary search the code point. See {@link #isUsuallyPrecededBySpace(int)}.
|
|
|
|
mSortedSymbolsPrecededBySpace = StringUtils.toSortedCodePointArray(
|
|
|
|
res.getString(R.string.symbols_preceded_by_space));
|
|
|
|
// To be able to binary search the code point. See {@link #isUsuallyFollowedBySpace(int)}.
|
|
|
|
mSortedSymbolsFollowedBySpace = StringUtils.toSortedCodePointArray(
|
|
|
|
res.getString(R.string.symbols_followed_by_space));
|
2014-05-01 03:33:59 +00:00
|
|
|
mSortedSymbolsClusteringTogether = StringUtils.toSortedCodePointArray(
|
|
|
|
res.getString(R.string.symbols_clustering_together));
|
2014-01-20 06:41:50 +00:00
|
|
|
// To be able to binary search the code point. See {@link #isWordConnector(int)}.
|
|
|
|
mSortedWordConnectors = StringUtils.toSortedCodePointArray(
|
|
|
|
res.getString(R.string.symbols_word_connectors));
|
2014-01-20 05:56:56 +00:00
|
|
|
mSortedWordSeparators = StringUtils.toSortedCodePointArray(
|
|
|
|
res.getString(R.string.symbols_word_separators));
|
2014-09-05 07:49:32 +00:00
|
|
|
mSortedSentenceTerminators = StringUtils.toSortedCodePointArray(
|
|
|
|
res.getString(R.string.symbols_sentence_terminators));
|
2014-01-08 09:06:32 +00:00
|
|
|
mSentenceSeparator = res.getInteger(R.integer.sentence_separator);
|
2014-09-05 07:49:32 +00:00
|
|
|
mAbbreviationMarker = res.getInteger(R.integer.abbreviation_marker);
|
2014-01-08 09:06:32 +00:00
|
|
|
mSentenceSeparatorAndSpace = new String(new int[] {
|
|
|
|
mSentenceSeparator, Constants.CODE_SPACE }, 0, 2);
|
|
|
|
mCurrentLanguageHasSpaces = res.getBoolean(R.bool.current_language_has_spaces);
|
2014-01-09 07:59:23 +00:00
|
|
|
final Locale locale = res.getConfiguration().locale;
|
|
|
|
// Heuristic: we use American Typography rules because it's the most common rules for all
|
2014-01-15 19:32:32 +00:00
|
|
|
// English variants. German rules (not "German typography") also have small gotchas.
|
2014-01-09 07:59:23 +00:00
|
|
|
mUsesAmericanTypography = Locale.ENGLISH.getLanguage().equals(locale.getLanguage());
|
2014-01-15 19:32:32 +00:00
|
|
|
mUsesGermanRules = Locale.GERMAN.getLanguage().equals(locale.getLanguage());
|
2014-02-13 09:43:48 +00:00
|
|
|
final String[] suggestPuncsSpec = MoreKeySpec.splitKeySpecs(
|
2014-03-20 06:28:05 +00:00
|
|
|
res.getString(R.string.suggested_punctuations));
|
2014-02-13 09:43:48 +00:00
|
|
|
mSuggestPuncList = PunctuationSuggestions.newPunctuationSuggestions(suggestPuncsSpec);
|
2014-01-08 09:06:32 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 06:27:01 +00:00
|
|
|
@UsedForTesting
|
|
|
|
public SpacingAndPunctuations(final SpacingAndPunctuations model,
|
|
|
|
final int[] overrideSortedWordSeparators) {
|
|
|
|
mSortedSymbolsPrecededBySpace = model.mSortedSymbolsPrecededBySpace;
|
|
|
|
mSortedSymbolsFollowedBySpace = model.mSortedSymbolsFollowedBySpace;
|
|
|
|
mSortedSymbolsClusteringTogether = model.mSortedSymbolsClusteringTogether;
|
|
|
|
mSortedWordConnectors = model.mSortedWordConnectors;
|
|
|
|
mSortedWordSeparators = overrideSortedWordSeparators;
|
2014-09-08 13:40:36 +00:00
|
|
|
mSortedSentenceTerminators = model.mSortedSentenceTerminators;
|
2014-09-03 06:27:01 +00:00
|
|
|
mSuggestPuncList = model.mSuggestPuncList;
|
|
|
|
mSentenceSeparator = model.mSentenceSeparator;
|
2014-09-08 13:40:36 +00:00
|
|
|
mAbbreviationMarker = model.mAbbreviationMarker;
|
2014-09-03 06:27:01 +00:00
|
|
|
mSentenceSeparatorAndSpace = model.mSentenceSeparatorAndSpace;
|
|
|
|
mCurrentLanguageHasSpaces = model.mCurrentLanguageHasSpaces;
|
|
|
|
mUsesAmericanTypography = model.mUsesAmericanTypography;
|
|
|
|
mUsesGermanRules = model.mUsesGermanRules;
|
|
|
|
}
|
|
|
|
|
2014-01-08 09:06:32 +00:00
|
|
|
public boolean isWordSeparator(final int code) {
|
2014-01-20 05:56:56 +00:00
|
|
|
return Arrays.binarySearch(mSortedWordSeparators, code) >= 0;
|
2014-01-08 09:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isWordConnector(final int code) {
|
2014-01-20 06:41:50 +00:00
|
|
|
return Arrays.binarySearch(mSortedWordConnectors, code) >= 0;
|
2014-01-08 09:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isWordCodePoint(final int code) {
|
|
|
|
return Character.isLetter(code) || isWordConnector(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isUsuallyPrecededBySpace(final int code) {
|
2014-01-20 06:41:50 +00:00
|
|
|
return Arrays.binarySearch(mSortedSymbolsPrecededBySpace, code) >= 0;
|
2014-01-08 09:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isUsuallyFollowedBySpace(final int code) {
|
2014-01-20 06:41:50 +00:00
|
|
|
return Arrays.binarySearch(mSortedSymbolsFollowedBySpace, code) >= 0;
|
2014-01-08 09:06:32 +00:00
|
|
|
}
|
|
|
|
|
2014-05-01 03:33:59 +00:00
|
|
|
public boolean isClusteringSymbol(final int code) {
|
|
|
|
return Arrays.binarySearch(mSortedSymbolsClusteringTogether, code) >= 0;
|
|
|
|
}
|
|
|
|
|
2014-09-05 07:49:32 +00:00
|
|
|
public boolean isSentenceTerminator(final int code) {
|
|
|
|
return Arrays.binarySearch(mSortedSentenceTerminators, code) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isAbbreviationMarker(final int code) {
|
|
|
|
return code == mAbbreviationMarker;
|
|
|
|
}
|
|
|
|
|
2014-01-08 09:06:32 +00:00
|
|
|
public boolean isSentenceSeparator(final int code) {
|
|
|
|
return code == mSentenceSeparator;
|
|
|
|
}
|
2014-02-12 13:53:15 +00:00
|
|
|
|
|
|
|
public String dump() {
|
|
|
|
final StringBuilder sb = new StringBuilder();
|
|
|
|
sb.append("mSortedSymbolsPrecededBySpace = ");
|
|
|
|
sb.append("" + Arrays.toString(mSortedSymbolsPrecededBySpace));
|
|
|
|
sb.append("\n mSortedSymbolsFollowedBySpace = ");
|
|
|
|
sb.append("" + Arrays.toString(mSortedSymbolsFollowedBySpace));
|
|
|
|
sb.append("\n mSortedWordConnectors = ");
|
|
|
|
sb.append("" + Arrays.toString(mSortedWordConnectors));
|
|
|
|
sb.append("\n mSortedWordSeparators = ");
|
|
|
|
sb.append("" + Arrays.toString(mSortedWordSeparators));
|
|
|
|
sb.append("\n mSuggestPuncList = ");
|
|
|
|
sb.append("" + mSuggestPuncList);
|
|
|
|
sb.append("\n mSentenceSeparator = ");
|
|
|
|
sb.append("" + mSentenceSeparator);
|
|
|
|
sb.append("\n mSentenceSeparatorAndSpace = ");
|
|
|
|
sb.append("" + mSentenceSeparatorAndSpace);
|
|
|
|
sb.append("\n mCurrentLanguageHasSpaces = ");
|
|
|
|
sb.append("" + mCurrentLanguageHasSpaces);
|
|
|
|
sb.append("\n mUsesAmericanTypography = ");
|
|
|
|
sb.append("" + mUsesAmericanTypography);
|
|
|
|
sb.append("\n mUsesGermanRules = ");
|
|
|
|
sb.append("" + mUsesGermanRules);
|
|
|
|
return sb.toString();
|
|
|
|
}
|
2014-01-08 09:06:32 +00:00
|
|
|
}
|