Hello I am developing a word game where i want to check the user input as valid word or not please suggest the way i can check the given string in android.
Eg . String s = "asfdaf" i want to check whether its a valid one.
There are many possible solutions to this some are the following
Use a web Dictionary API
https://developer.oxforddictionaries.com/
http://googlesystem.blogspot.com/2009/12/on-googles-unofficial-dictionary-api.html
http://www.dictionaryapi.com/
if you would prefer a local solution
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"));
}
}
this uses the local word list found on all Linux systems to check for the word
First, download a word list from for example here
. Place it in the root directory of your project. Use the following code to check whether a String
is part of the word list or not:
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);
}
}
I'd store a dictionary and do a lookup in there. If the word is present in the dictionary, it's valid.
You can find a some clues on how to do this here: Android dictionary application
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With