1
0
Fork 0

split Text into Text and TextTools

This commit is contained in:
sspanak 2024-03-18 19:16:32 +02:00 committed by Dimo Karaivanov
parent f65fd29139
commit 76fed90753
9 changed files with 85 additions and 67 deletions

View file

@ -3,8 +3,8 @@ package io.github.sspanak.tt9.db;
import java.util.HashMap;
import io.github.sspanak.tt9.Logger;
import io.github.sspanak.tt9.languages.Text;
import io.github.sspanak.tt9.languages.Language;
import io.github.sspanak.tt9.languages.TextTools;
import io.github.sspanak.tt9.preferences.SettingsStore;
public class SlowQueryStats {
@ -63,7 +63,7 @@ public class SlowQueryStats {
"Queries: " + totalQueries + ". Average time: " + averageTime + " ms." +
"\nSlow: " + slowQueries.size() + ". Average time: " + slowAverageTime + " ms." +
"\nSlowest: " + maxQueryTime + " ms." +
"\nFirst: " + Text.unixTimestampToISODate(firstQueryTime);
"\nFirst: " + TextTools.unixTimestampToISODate(firstQueryTime);
}
public static String getList() {

View file

@ -133,7 +133,7 @@ public class TraditionalT9 extends KeyPadHandler {
private void determineTextCase() {
mInputMode.defaultTextCase();
mInputMode.setTextFieldCase(textField.determineTextCase(inputType));
mInputMode.determineNextWordTextCase(textField.getTextBeforeCursor());
mInputMode.determineNextWordTextCase(textField.getStringBeforeCursor());
InputModeValidator.validateTextCase(mInputMode, settings.getTextCase());
}
@ -310,7 +310,7 @@ public class TraditionalT9 extends KeyPadHandler {
// Auto-adjust the text case before each word, if the InputMode supports it.
if (getComposingText().isEmpty()) {
mInputMode.determineNextWordTextCase(textField.getTextBeforeCursor());
mInputMode.determineNextWordTextCase(textField.getStringBeforeCursor());
}
if (!mInputMode.onNumber(key, hold, repeat)) {
@ -655,7 +655,7 @@ public class TraditionalT9 extends KeyPadHandler {
commitCurrentSuggestion(false);
mInputMode.onAcceptSuggestion(lastComposingText, true);
autoCorrectSpace(lastComposingText, false, -1);
mInputMode.determineNextWordTextCase(textField.getTextBeforeCursor());
mInputMode.determineNextWordTextCase(textField.getStringBeforeCursor());
}
// display the word suggestions
@ -746,7 +746,7 @@ public class TraditionalT9 extends KeyPadHandler {
int nextModeIndex = (allowedInputModes.indexOf(mInputMode.getId()) + 1) % allowedInputModes.size();
mInputMode = InputMode.getInstance(settings, mLanguage, inputType, allowedInputModes.get(nextModeIndex));
mInputMode.setTextFieldCase(textField.determineTextCase(inputType));
mInputMode.determineNextWordTextCase(textField.getTextBeforeCursor());
mInputMode.determineNextWordTextCase(textField.getStringBeforeCursor());
resetKeyRepeat();
}

View file

@ -8,7 +8,6 @@ import io.github.sspanak.tt9.Logger;
import io.github.sspanak.tt9.ime.helpers.InputType;
import io.github.sspanak.tt9.ime.helpers.TextField;
import io.github.sspanak.tt9.languages.Language;
import io.github.sspanak.tt9.languages.Text;
import io.github.sspanak.tt9.preferences.SettingsStore;
abstract public class InputMode {
@ -144,7 +143,7 @@ abstract public class InputMode {
return true;
}
public void determineNextWordTextCase(Text textBeforeCursor) {}
public void determineNextWordTextCase(String textBeforeCursor) {}
// Based on the internal logic of the mode (punctuation or grammar rules), re-adjust the text case for when getSuggestions() is called.
protected String adjustSuggestionTextCase(String word, int newTextCase) { return word; }

View file

@ -328,7 +328,7 @@ public class ModePredictive extends InputMode {
}
@Override
public void determineNextWordTextCase(Text textBeforeCursor) {
public void determineNextWordTextCase(String textBeforeCursor) {
textCase = autoTextCase.determineNextWordTextCase(textCase, textFieldTextCase, textBeforeCursor);
}

View file

@ -70,7 +70,7 @@ public class AutoSpace {
return
nextKey != 1
&& !nextChars.nextIsPunctuation()
&& !Text.nextIsPunctuation(nextChars.toString())
&& !nextChars.startsWithNumber()
&& (
previousChar == '.'

View file

@ -41,7 +41,7 @@ public class AutoTextCase {
* For example, this function will return CASE_LOWER by default, but CASE_UPPER at the beginning
* of a sentence.
*/
public int determineNextWordTextCase(int currentTextCase, int textFieldTextCase, Text textBeforeCursor) {
public int determineNextWordTextCase(int currentTextCase, int textFieldTextCase, String textBeforeCursor) {
if (
// When the setting is off, don't do any changes.
!settings.getAutoTextCase()
@ -57,17 +57,17 @@ public class AutoTextCase {
}
// start of text
if (textBeforeCursor.isEmpty()) {
if (textBeforeCursor != null && textBeforeCursor.isEmpty()) {
return InputMode.CASE_CAPITALIZE;
}
// start of sentence, excluding after "..."
if (textBeforeCursor.isStartOfSentence()) {
if (Text.isStartOfSentence(textBeforeCursor)) {
return InputMode.CASE_CAPITALIZE;
}
// this is mostly for English "I"
if (textBeforeCursor.isNextToWord()) {
if (Text.isNextToWord(textBeforeCursor)) {
return InputMode.CASE_LOWER;
}

View file

@ -19,7 +19,7 @@ public class EmojiLanguage extends Language {
@Override
public String getDigitSequenceForWord(String word) {
return Text.isGraphic(word) ? CUSTOM_EMOJI_SEQUENCE : null;
return TextTools.isGraphic(word) ? CUSTOM_EMOJI_SEQUENCE : null;
}
@Override

View file

@ -2,20 +2,9 @@ package io.github.sspanak.tt9.languages;
import androidx.annotation.NonNull;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.regex.Pattern;
public class Text {
private static final Pattern containsOtherThan1 = Pattern.compile("[02-9]");
private static final Pattern previousIsLetter = Pattern.compile("\\p{L}$");
private static final Pattern nextIsPunctuation = Pattern.compile("^\\p{Punct}");
private static final Pattern nextToWord = Pattern.compile("\\b$");
private static final Pattern startOfSentence = Pattern.compile("(?<!\\.)(^|[.?!؟¿¡])\\s+$");
public class Text extends TextTools {
private final Language language;
private final String text;
@ -41,9 +30,7 @@ public class Text {
}
}
public static boolean containsOtherThan1(String str) {
return str != null && containsOtherThan1.matcher(str).find();
}
public boolean endsWithGraphic() {
return text != null && !text.isEmpty() && Characters.isGraphic(text.charAt(text.length() - 1));
@ -53,19 +40,7 @@ public class Text {
return text == null || text.isEmpty();
}
public static boolean isGraphic(String str) {
if (str == null || str.isEmpty()) {
return false;
}
for (int i = 0, end = str.length(); i < end; i++) {
if (!Characters.isGraphic(str.charAt(i))) {
return false;
}
}
return true;
}
public boolean isMixedCase() {
return
@ -75,14 +50,6 @@ public class Text {
&& !text.toUpperCase(language.getLocale()).equals(text);
}
public boolean isNextToWord() {
return text != null && nextToWord.matcher(text).find();
}
public boolean isStartOfSentence() {
return text != null && startOfSentence.matcher(text).find();
}
public boolean isUpperCase() {
return language != null && text != null && text.toUpperCase(language.getLocale()).equals(text);
}
@ -127,13 +94,9 @@ public class Text {
return sb.toString();
}
public boolean nextIsPunctuation() {
return text != null && !text.isEmpty() && nextIsPunctuation.matcher(text).find();
}
public static boolean previousIsLetter(String str) {
return str != null && previousIsLetter.matcher(str).find();
}
public boolean startsWithWhitespace() {
return text != null && !text.isEmpty() && Character.isWhitespace(text.charAt(0));
@ -203,17 +166,6 @@ public class Text {
}
}
public static String unixTimestampToISODate(long timestamp) {
if (timestamp < 0) {
return "--";
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
sdf.setTimeZone(TimeZone.getDefault());
return sdf.format(new Date(timestamp));
}
@NonNull
@Override
public String toString() {

View file

@ -0,0 +1,67 @@
package io.github.sspanak.tt9.languages;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.regex.Pattern;
public class TextTools {
private static final Pattern containsOtherThan1 = Pattern.compile("[02-9]");
private static final Pattern nextIsPunctuation = Pattern.compile("^\\p{Punct}");
private static final Pattern nextToWord = Pattern.compile("\\b$");
private static final Pattern previousIsLetter = Pattern.compile("\\p{L}$");
private static final Pattern startOfSentence = Pattern.compile("(?<!\\.)(^|[.?!؟¿¡])\\s+$");
public static boolean containsOtherThan1(String str) {
return str != null && containsOtherThan1.matcher(str).find();
}
public static boolean isGraphic(String str) {
if (str == null || str.isEmpty()) {
return false;
}
for (int i = 0, end = str.length(); i < end; i++) {
if (!Characters.isGraphic(str.charAt(i))) {
return false;
}
}
return true;
}
public static boolean isStartOfSentence(String str) {
return str != null && startOfSentence.matcher(str).find();
}
public static boolean isNextToWord(String str) {
return str != null && nextToWord.matcher(str).find();
}
public static boolean nextIsPunctuation(String str) {
return str != null && !str.isEmpty() && nextIsPunctuation.matcher(str).find();
}
public static boolean previousIsLetter(String str) {
return str != null && previousIsLetter.matcher(str).find();
}
public static String unixTimestampToISODate(long timestamp) {
if (timestamp < 0) {
return "--";
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
sdf.setTimeZone(TimeZone.getDefault());
return sdf.format(new Date(timestamp));
}
}