如何检查给定的字符串是否为单词

Mah*_*esh 5 java string android dictionary

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

例如 String s ="asfdaf"我想检查它是否是有效的.

zei*_*tue 7

有许多可能的解决方案,有些是以下几种

使用Web Dictionary 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"));
    }
}
Run Code Online (Sandbox Code Playgroud)

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


Bul*_*aza 5

首先,从例如下载单词列表here.将其放在项目的根目录中.使用以下代码检查a是否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);
    }
}
Run Code Online (Sandbox Code Playgroud)


Luc*_*man 2

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

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