Skip to content

Commit

Permalink
refactor: use Long insteadof Double for word frefrequency
Browse files Browse the repository at this point in the history
  • Loading branch information
dongyuwei committed Feb 15, 2024
1 parent 9bff245 commit 7bbddda
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions app/src/main/java/rkr/tinykeyboard/inputmethod/SoftKeyboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class SoftKeyboard extends InputMethodService

private ExecutorService executorService;
private StringBuilder compositionText = new StringBuilder();
private PatriciaTrie<Double> trie;
private PatriciaTrie<Long> trie;
private List<String> candidates = new ArrayList<>();
private Map<String, List<String>> pinyinMap = new HashMap<>();

Expand All @@ -88,11 +88,11 @@ private void loadDictionaryAsync() {
executorService.execute(() -> {
Gson gson = new Gson();
String jsonString = DictUtil.getJsonFromAssets(getApplicationContext(), "google_227800_words.json");
Type mapType = new TypeToken<Map<String, Double>>(){}.getType();
Map<String, Double> map = gson.fromJson(jsonString, mapType);
Type mapType = new TypeToken<Map<String, Long>>(){}.getType();
Map<String, Long> map = gson.fromJson(jsonString, mapType);

trie = new PatriciaTrie<>();
for (Map.Entry<String, Double> entry : map.entrySet()) {
for (Map.Entry<String, Long> entry : map.entrySet()) {
trie.put(entry.getKey(), entry.getValue());
}

Expand Down Expand Up @@ -333,16 +333,16 @@ private List<String> getCandidates() {
return new ArrayList<>();
}
String prefix = compositionText.toString();
Map<String, Double> prefixMap = trie.prefixMap(prefix);
List<Map.Entry<String, Double>> matchingWords = new ArrayList<>(prefixMap.entrySet());
Map<String, Long> prefixMap = trie.prefixMap(prefix);
List<Map.Entry<String, Long>> matchingWords = new ArrayList<>(prefixMap.entrySet());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
matchingWords.sort(Map.Entry.comparingByValue(Collections.reverseOrder())); // Sort by frequency, highest first
}

List<String> sortedWords = new ArrayList<>();
sortedWords.add(prefix);
if (!matchingWords.isEmpty()) {
for (Map.Entry<String, Double> entry : matchingWords) {
for (Map.Entry<String, Long> entry : matchingWords) {
sortedWords.add(entry.getKey());
}
} else {
Expand Down

0 comments on commit 7bbddda

Please sign in to comment.