Skip to content
Snippets Groups Projects
Commit 837cdd73 authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka
Browse files

Add SpacingAndPunctuationsTests

Change-Id: I78d488ad84a11af809ee1f8d3d2fa01a89fbfa28
parent 3f3b0af5
No related branches found
No related tags found
No related merge requests found
...@@ -32,9 +32,9 @@ import java.util.Arrays; ...@@ -32,9 +32,9 @@ import java.util.Arrays;
import java.util.Locale; import java.util.Locale;
public final class SpacingAndPunctuations { public final class SpacingAndPunctuations {
private final int[] mSymbolsPrecededBySpace; private final int[] mSortedSymbolsPrecededBySpace;
private final int[] mSymbolsFollowedBySpace; private final int[] mSortedSymbolsFollowedBySpace;
private final int[] mWordConnectors; private final int[] mSortedWordConnectors;
public final SuggestedWords mSuggestPuncList; public final SuggestedWords mSuggestPuncList;
public final String mWordSeparators; public final String mWordSeparators;
private final int mSentenceSeparator; private final int mSentenceSeparator;
...@@ -44,15 +44,15 @@ public final class SpacingAndPunctuations { ...@@ -44,15 +44,15 @@ public final class SpacingAndPunctuations {
public final boolean mUsesGermanRules; public final boolean mUsesGermanRules;
public SpacingAndPunctuations(final Resources res) { public SpacingAndPunctuations(final Resources res) {
mSymbolsPrecededBySpace = // To be able to binary search the code point. See {@link #isUsuallyPrecededBySpace(int)}.
StringUtils.toCodePointArray(res.getString(R.string.symbols_preceded_by_space)); mSortedSymbolsPrecededBySpace = StringUtils.toSortedCodePointArray(
Arrays.sort(mSymbolsPrecededBySpace); res.getString(R.string.symbols_preceded_by_space));
mSymbolsFollowedBySpace = // To be able to binary search the code point. See {@link #isUsuallyFollowedBySpace(int)}.
StringUtils.toCodePointArray(res.getString(R.string.symbols_followed_by_space)); mSortedSymbolsFollowedBySpace = StringUtils.toSortedCodePointArray(
Arrays.sort(mSymbolsFollowedBySpace); res.getString(R.string.symbols_followed_by_space));
mWordConnectors = // To be able to binary search the code point. See {@link #isWordConnector(int)}.
StringUtils.toCodePointArray(res.getString(R.string.symbols_word_connectors)); mSortedWordConnectors = StringUtils.toSortedCodePointArray(
Arrays.sort(mWordConnectors); res.getString(R.string.symbols_word_connectors));
final String[] suggestPuncsSpec = KeySpecParser.splitKeySpecs(res.getString( final String[] suggestPuncsSpec = KeySpecParser.splitKeySpecs(res.getString(
R.string.suggested_punctuations)); R.string.suggested_punctuations));
mSuggestPuncList = createSuggestPuncList(suggestPuncsSpec); mSuggestPuncList = createSuggestPuncList(suggestPuncsSpec);
...@@ -74,6 +74,7 @@ public final class SpacingAndPunctuations { ...@@ -74,6 +74,7 @@ public final class SpacingAndPunctuations {
if (puncs != null) { if (puncs != null) {
for (final String puncSpec : puncs) { for (final String puncSpec : puncs) {
// TODO: Stop using KeySpceParser.getLabel(). // TODO: Stop using KeySpceParser.getLabel().
// TODO: Punctuation suggestions should honor RTL languages.
puncList.add(new SuggestedWordInfo(KeySpecParser.getLabel(puncSpec), puncList.add(new SuggestedWordInfo(KeySpecParser.getLabel(puncSpec),
SuggestedWordInfo.MAX_SCORE, SuggestedWordInfo.KIND_HARDCODED, SuggestedWordInfo.MAX_SCORE, SuggestedWordInfo.KIND_HARDCODED,
Dictionary.DICTIONARY_HARDCODED, Dictionary.DICTIONARY_HARDCODED,
...@@ -94,7 +95,7 @@ public final class SpacingAndPunctuations { ...@@ -94,7 +95,7 @@ public final class SpacingAndPunctuations {
} }
public boolean isWordConnector(final int code) { public boolean isWordConnector(final int code) {
return Arrays.binarySearch(mWordConnectors, code) >= 0; return Arrays.binarySearch(mSortedWordConnectors, code) >= 0;
} }
public boolean isWordCodePoint(final int code) { public boolean isWordCodePoint(final int code) {
...@@ -102,11 +103,11 @@ public final class SpacingAndPunctuations { ...@@ -102,11 +103,11 @@ public final class SpacingAndPunctuations {
} }
public boolean isUsuallyPrecededBySpace(final int code) { public boolean isUsuallyPrecededBySpace(final int code) {
return Arrays.binarySearch(mSymbolsPrecededBySpace, code) >= 0; return Arrays.binarySearch(mSortedSymbolsPrecededBySpace, code) >= 0;
} }
public boolean isUsuallyFollowedBySpace(final int code) { public boolean isUsuallyFollowedBySpace(final int code) {
return Arrays.binarySearch(mSymbolsFollowedBySpace, code) >= 0; return Arrays.binarySearch(mSortedSymbolsFollowedBySpace, code) >= 0;
} }
public boolean isSentenceSeparator(final int code) { public boolean isSentenceSeparator(final int code) {
......
...@@ -22,6 +22,7 @@ import com.android.inputmethod.annotations.UsedForTesting; ...@@ -22,6 +22,7 @@ import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.latin.Constants; import com.android.inputmethod.latin.Constants;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale; import java.util.Locale;
public final class StringUtils { public final class StringUtils {
...@@ -183,6 +184,12 @@ public final class StringUtils { ...@@ -183,6 +184,12 @@ public final class StringUtils {
return codePoints; return codePoints;
} }
public static int[] toSortedCodePointArray(final String string) {
final int[] codePoints = toCodePointArray(string);
Arrays.sort(codePoints);
return codePoints;
}
// This method assumes the text is not null. For the empty string, it returns CAPITALIZE_NONE. // This method assumes the text is not null. For the empty string, it returns CAPITALIZE_NONE.
public static int getCapitalizationType(final String text) { public static int getCapitalizationType(final String text) {
// If the first char is not uppercase, then the word is either all lower case or // If the first char is not uppercase, then the word is either all lower case or
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment