diff options
| author | Jules Aguillon | 2026-02-22 21:13:11 +0100 |
|---|---|---|
| committer | GitHub | 2026-02-22 21:13:11 +0100 |
| commit | a80afa5fbebd023167394cdc3e31fc26790cffaa (patch) | |
| tree | 8edc8644c6cd67b7cebbbf8cefff1052d1e3b38c /srcs/juloo.keyboard2/suggestions/Suggestions.java | |
| parent | d172455eb4fa7627674647121a42311d518dd759 (diff) | |
| download | unexpected-keyboard-a80afa5fbebd023167394cdc3e31fc26790cffaa.tar.gz unexpected-keyboard-a80afa5fbebd023167394cdc3e31fc26790cffaa.zip | |
Improve suggestion for capitalized words (#1183)
Suggestions were previously case-sensitive, which gave very bad results
for the first word of a sentence.
This makes sure that the suggestions for capitalized words are as good
as for all lower-case words and that the suggestion doesn't turn the
uppercase letter into lower case.
Diffstat (limited to 'srcs/juloo.keyboard2/suggestions/Suggestions.java')
| -rw-r--r-- | srcs/juloo.keyboard2/suggestions/Suggestions.java | 53 |
1 files changed, 39 insertions, 14 deletions
diff --git a/srcs/juloo.keyboard2/suggestions/Suggestions.java b/srcs/juloo.keyboard2/suggestions/Suggestions.java index 89ed86e..a55acdd 100644 --- a/srcs/juloo.keyboard2/suggestions/Suggestions.java +++ b/srcs/juloo.keyboard2/suggestions/Suggestions.java @@ -32,22 +32,46 @@ public final class Suggestions } else { - Cdict.Result r = dict.find(word); - String[] suggestions = new String[3]; - int i = 0; + String[] dst = new String[3]; + query_suggestions(dict, word, dst, 3); + set_suggestions(Arrays.asList(dst)); + } + } + + int query_suggestions(Cdict dict, String word, String[] dst, int max_count) + { + Cdict.Result r = dict.find(word); + int i = 0; + if (r.found) + dst[i++] = word; + boolean first_char_upper = Character.isUpperCase(word.charAt(0)); + // Do the dictionary query in lower case and re-apply the upper case after + if (first_char_upper) + { + r = dict.find(word.toLowerCase()); if (r.found) - suggestions[i++] = word; - int[] suffixes = dict.suffixes(r, 3); - int[] dist = dict.distance(word, 1, 3); - for (int j = 0; j < 3 && i < 3; j++) - { - if (suffixes.length > j) - suggestions[i++] = dict.word(suffixes[j]); - if (dist.length > j && i < 3) - suggestions[i++] = dict.word(dist[j]); - } - set_suggestions(Arrays.asList(suggestions)); + dst[i++] = word; } + int[] suffixes = dict.suffixes(r, max_count); + // Disable distance search for small words + int[] dist = (word.length() < 3 || i + 1 >= max_count) ? NO_RESULTS : + dict.distance(word, 1, max_count); + for (int j = 0; j < max_count && i < max_count; j++) + { + if (suffixes.length > j) + dst[i++] = dict.word(suffixes[j]); + if (dist.length > j && i < max_count) + dst[i++] = dict.word(dist[j]); + } + if (first_char_upper) + capitalize_results(dst); + return i; + } + + void capitalize_results(String[] rs) + { + for (int i = 0; i < rs.length; i++) + rs[i] = rs[i].substring(0, 1).toUpperCase() + rs[i].substring(1); } void set_suggestions(List<String> ws) @@ -57,6 +81,7 @@ public final class Suggestions } static final List<String> NO_SUGGESTIONS = Arrays.asList(); + static final int[] NO_RESULTS = new int[0]; public static interface Callback { |
