Implemented task per content. However, real T9 would guess fitting words also by frequency of usage #3.

master
Tomasz Polgrabia 2025-01-04 00:09:49 +01:00
parent 0ebf8839e9
commit efbfe88a7c
1 changed files with 11 additions and 5 deletions

View File

@ -58,7 +58,7 @@ public class RstTree {
assert !word.isBlank() : "word must not be blank";
assert word.matches("^[a-z]+$") : "word '" + word + "' must contain only small letters";
RstTreeNode searchedNode = lookupNodeByPrefixOrValueInternal(rstTreeRoot, word, SearchMode.VALUE);
RstTreeNode searchedNode = lookupNodesBySearchKeyInternal(rstTreeRoot, word, SearchMode.VALUE);
if (searchedNode == null) {
return false;
}
@ -67,7 +67,7 @@ public class RstTree {
return word.equals(value);
}
private RstTreeNode lookupNodeByPrefixOrValueInternal(RstTreeNode node, String searchValue, SearchMode searchMode) {
private RstTreeNode lookupNodesBySearchKeyInternal(RstTreeNode node, String searchValue, SearchMode searchMode) {
if (node == null) {
return null;
}
@ -85,6 +85,11 @@ public class RstTree {
return node;
}
if (SearchMode.PREFIX.equals(searchMode)
&& (node.getPrefix().startsWith(searchValue))) {
return node;
}
if (node.getPrefix().startsWith(searchValue)) {
return null;
}
@ -95,7 +100,7 @@ public class RstTree {
// logger.info("Word: {}, prefix: {}, index: {}, c: {}, offset: {}", searchValue, node.getPrefix(), node.getPrefix().length(), c, offset);
var childItem = node.getRstChildren().length <= offset ? null : node.getRstChildren()[offset];
// logger.info("Childitem: {}, c: {}, offset: {}", childItem, c, offset);
return lookupNodeByPrefixOrValueInternal(childItem, searchValue, SearchMode.VALUE);
return lookupNodesBySearchKeyInternal(childItem, searchValue, SearchMode.VALUE);
}
public void dfsByPrefix(String prefix, Consumer<String> wordConsumer) {
@ -103,7 +108,7 @@ public class RstTree {
assert !prefix.isBlank() : "prefix must not be blank";
assert prefix.matches("^[a-z]+$") : "prefix '" + prefix + "' must contain only small letters";
RstTreeNode node = lookupNodeByPrefixOrValueInternal(rstTreeRoot, prefix, SearchMode.PREFIX_OR_VALUE);
RstTreeNode node = lookupNodesBySearchKeyInternal(rstTreeRoot, prefix, SearchMode.PREFIX_OR_VALUE);
dfs(node, wordConsumer);
}
@ -119,6 +124,7 @@ public class RstTree {
private enum SearchMode {
VALUE,
PREFIX_OR_VALUE
PREFIX_OR_VALUE,
PREFIX
}
}