【问题标题】:Android - Checking valid English wordsAndroid - 检查有效的英文单词
【发布时间】:2017-09-28 06:52:34
【问题描述】:

有效词:牛津词典中的词。一个假设。

我想验证用户形成的单词是否有效。我希望有一本字典是一个不错的选择。我同意使用拼写检查器是错误的选择。

我读过Android Word Validation)。答案建议将字典加载到 sqlite 中。我可能误解了这一点。如果我将单词列表加载到 sqlite 中,我可能会错过一些单词。

所以我的问题是

  1. 是否有任何提供单词列表的android 内置类。我不认为我可以使用 Android 的 UserDictionary。

  1. 有没有办法将整个字典加载到 SQLite 中

  1. 如果我错了,请您建议我最好的选择。

【问题讨论】:

    标签: android dictionary


    【解决方案1】:

    当涉及到其他选项时,您可以尝试使用牛津词典 API 来检查特定单词:

    https://developer.oxforddictionaries.com/documentation

    有一些方法可以检查单词的存在。

    【讨论】:

    • 有多种计划可供选择。可以在developer.oxforddictionaries.com底部查看,还有免费版。
    • 还有一种方法可以检索所选词典中的单词列表。您可以编写一个简单的代码,通过为每个请求指定限制和偏移量来获取整个列表,并将它们放入本地数据库中。您可能需要获得一些许可才能这样做,因为我认为它不应该以这种方式使用。
    【解决方案2】:

    你可以在下面使用这样的东西:

    public class Dictionary
    {
     private Set<String> wordsSet;
    
     public Dictionary() throws IOException    //read the file in the constructor
      {
        Path path = Paths.get("file.txt");  //path of the file in root directory
        byte[] readBytes = Files.readAllBytes(path);
        String wordListContents = new String(readBytes, "UTF-8");
        String[] words = wordListContents.split("\n"); //the text file should contain one word in one line
        wordsSet = new HashSet<>();
        Collections.addAll(wordsSet, words);
      }
    
      public boolean contains(String word)
      {
        return wordsSet.contains(word);   //check if it exists
      }
    }
    

    【讨论】:

    • 你的意思是我应该有一个有效单词的文件。它的大小可能是 GB。不会影响性能吗?
    • 这里是这样一个文件的链接:raw.githubusercontent.com/dwyl/english-words/master/words.txt,它包含大约 350,000 个单词,但大小仍然约为 4.64 MB。所以是的,这不会影响性能
    • 无论如何,更好的解决方案是调用@Bartosz 提到的API,但如果你想要一个本地解决方案,那么这个(上面提到的)可以是解决方案。
    猜你喜欢
    • 2021-08-31
    • 2016-09-27
    • 2019-07-01
    • 1970-01-01
    • 2017-12-11
    • 2020-01-25
    • 2017-07-31
    • 2011-08-13
    • 1970-01-01
    相关资源
    最近更新 更多