【问题标题】:How to check whether given string is a word如何检查给定的字符串是否是一个单词
【发布时间】:2012-07-21 09:04:45
【问题描述】:

您好,我正在开发一个文字游戏,我想检查用户输入是否为有效单词 请建议我可以在android中检查给定字符串的方式。

例如。字符串 s = "asfdaf" 我想检查它是否有效。

【问题讨论】:

  • 你需要有一本字典……然后进行匹配……
  • “有效的”是什么意思?这是否意味着,给定的单词是否有意义?您在应用程序中使用字典吗?
  • 在android中你可以有文件或sqlite数据库形式的dict..
  • @AnujAroshA 我不想使用字典数据库。有没有办法使用内置的字典库
  • @Mahesh 你试试[UserDictionary] (developer.android.com/reference/android/provider/…) 如果没有,我想你可以找到一个有GPL 的字典来源,就像[UDM] (udm.adrianvintu.com)跨度>

标签: java android string dictionary


【解决方案1】:

有很多可能的解决方案,其中一些如下

使用网络词典 API

https://developer.oxforddictionaries.com/

http://googlesystem.blogspot.com/2009/12/on-googles-unofficial-dictionary-api.html

http://www.dictionaryapi.com/

如果您更喜欢本地解决方案

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

class WordChecker {
    public static boolean check_for_word(String word) {
        // System.out.println(word);
        try {
            BufferedReader in = new BufferedReader(new FileReader(
                    "/usr/share/dict/american-english"));
            String str;
            while ((str = in.readLine()) != null) {
                if (str.indexOf(word) != -1) {
                    return true;
                }
            }
            in.close();
        } catch (IOException e) {
        }

        return false;
    }

    public static void main(String[] args) {
        System.out.println(check_for_word("hello"));
    }
}

这使用在所有 Linux 系统上找到的本地单词列表来检查单词

【讨论】:

  • @mohammedalisunasara 我修好了,现在应该可以工作了。
  • 兄弟还是不行直接发送返回false
  • @mohammedalisunasara 我的作品与我刚刚提交的代码一样。这取决于 /usr/share/dict/american-english 虽然
【解决方案2】:

您可以尝试此代码进行基本验证

import java.util.Scanner;

public class InputValidation {

    public static void main(String[] args) {
        String input;
        try {
            System.out.println("Enter the input");
            Scanner s = new  Scanner(System.in);

            input = s.next();
            if(input.matches(".*\\d.*")){
                System.out.println(" Contains digit only");
            } else{
                System.out.println(" Only String/words found");
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

【讨论】:

    【解决方案3】:

    首先,从例如here 下载一个单词列表。将其放在项目的根目录中。使用以下代码检查 String 是否是单词列表的一部分:

    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.Collections;
    import java.util.HashSet;
    import java.util.Set;
    
    public class Dictionary
    {
        private Set<String> wordsSet;
    
        public Dictionary() throws IOException
        {
            Path path = Paths.get("words.txt");
            byte[] readBytes = Files.readAllBytes(path);
            String wordListContents = new String(readBytes, "UTF-8");
            String[] words = wordListContents.split("\n");
            wordsSet = new HashSet<>();
            Collections.addAll(wordsSet, words);
        }
    
        public boolean contains(String word)
        {
            return wordsSet.contains(word);
        }
    }
    

    【讨论】:

      【解决方案4】:
      zeitue 说:
      import java.io.BufferedReader;
      import java.io.FileReader;
      import java.io.IOException;
      
      class WordChecker {
          public static boolean check_for_word(String word) {
              // System.out.println(word);
              try {
                  BufferedReader in = new BufferedReader(new FileReader(
                      "/usr/share/dict/american-english"));
                  String str;
                  while ((str = in.readLine()) != null) {
                      if (str.indexOf(word) != -1) {
                          return true;
                      }
                  }
                  in.close();
              } catch (IOException e) {
              }
      
              return false;
          }
      
          public static void main(String[] args) {
              System.out.println(check_for_word("hello"));
          }
      }
      

      但这仅适用于 linux。如果你想在 mac 上做同样的事情,改变路径

      /usr/share/dict/american-english
      

      /usr/share/dict/web2
      

      我没有在 Windows 上尝试过,但如果有人知道下面的评论

      【讨论】:

        【解决方案5】:

        我会存储一本字典并在其中进行查找。如果该词出现在字典中,则它是有效的。

        您可以在此处找到有关如何执行此操作的一些线索: Android dictionary application

        【讨论】:

        • 有没有办法使用android的内置字典功能
        • 是的,我想要的东西,但它只会给我用户定义的单词:(
        【解决方案6】:
        if(s.equals("word from dictionary in loop"){
            //action
        }
        

        也不错

        s = s.toLowerCase();
        

        所以无论“pokemon”是怎样的词条

        【讨论】:

          猜你喜欢
          • 2017-07-03
          • 2012-05-20
          • 1970-01-01
          • 2013-03-09
          • 2016-02-23
          • 2014-12-15
          • 1970-01-01
          • 1970-01-01
          • 2011-05-20
          相关资源
          最近更新 更多