Android 获取词典建议(相当于 UiTextChecker)

Android get dictionary suggestions (equivalent to UiTextChecker)

我正在尝试在 Android 中实现一些代码,类似于我的同事为 iOS 编写的代码。

在他的代码中,他接受了一些输入文本并要求系统提供自动完成建议。目的是猜测下一个字母可能是什么,所以如果用户输入了“pri”,则可能性很可能是“c”(代表“price”)、“d”(代表“pride”)、“g” (对于“prig”)等

现在我的同事在 iOS 中使用一个名为“UiTextChecker().completions”的 API 来获取目前输入的文本的可能补全。我在 Android.

中寻找类似的东西

我看到了 which seems to imply that you have to write your own code, and include your own dictionary. Is this still true? Does anyone know of project (and a dictionary) which can be freely used (or at least have some code to parse and organize the dictionaries referred to),还是我必须自己编写字典和所有代码?

似乎不​​太可能需要这么多工作来复制 iOS 中的一个简单调用,但我没有找到任何示例,除了 AutoCompleteTextView 的许多示例以及一个包含 5 个水果或 10 个国家/地区的小字典。

好吧,我真的找不到办法做到这一点 - 所以我只是将最常用的几千个英语单词列表导入我的应用程序(逗号分隔),然后有一些这样的代码:

    // let's create a list of 1000 words
    String thousandWords = resources.getString(R.string.words1000list);
    String[] list = thousandWords.split(",");
    words = Arrays.asList(list);

我写了一些这样的代码(使用 Java 8):

public class WordPredicates {

    public static Predicate<String> startsWith(final String prefix) {
        return p -> p.startsWith(prefix);
    }

    public static List<String> getCandidates (List<String> wordsList, Predicate<String> predicate) {
        return wordsList.stream().filter(predicate).collect(Collectors.<String>toList());
    }
}

然后每当我有一些文本我想要可能的完成时,我只需调用:

    List<String> completions = WordPredicates.getCandidates(words, WordPredicates.startsWith(word));

工作愉快