Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether given string is a word

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.

like image 795
Mahesh Avatar asked Jul 23 '12 06:07

Mahesh


3 Answers

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

like image 175
Taylor Ramirez Avatar answered Nov 08 '22 20:11

Taylor Ramirez


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);
    }
}
like image 5
BullyWiiPlaza Avatar answered Nov 08 '22 18:11

BullyWiiPlaza


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

like image 2
Lucas Kauffman Avatar answered Nov 08 '22 19:11

Lucas Kauffman