-1

rookie programmer here.

By word I mean like "apple" is a word but "opple" isn't.

So I wanted to make a really simple app that checked to see what the user inputs is a word or not.

Is there a widget or something I can import to do this?

I haven't seen any documentation on this, but I know spellcheck is built into the phone, so I could use some help.

  • 5
    Define "word". Precisely. Once you have the definition, you'll almost have the implementation. – JB Nizet Aug 16 '17 at 17:09
  • 3
    [Android API](https://developer.android.com/guide/topics/text/spell-checker-framework.html) I'm not terribly familiar with the API so I can't write up an answer per say, but this should be what you want. – drelliot Aug 16 '17 at 17:11

3 Answers3

0

For checking if there's a single word in a string, I'd look for spaces:

if (str != null && !str.trim().isEmpty() && str.trim().indexOf(' ') == -1) {
    // it's a single word
}
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 1
    This does not answer the question. It merely tests if a string is non-null and contains no space char. –  Aug 16 '17 at 17:14
  • 1
    The question was previously unclear - OP edited to clarify that he was asking about spell checking – drelliot Aug 16 '17 at 17:21
  • Should have waited for clarification, because there was nothing in the original Q that suggested "detect if a string is a single string of chars with no space". –  Aug 16 '17 at 18:19
0

There may be a lot way to do this but one way is you basically need to have a list of word in your root directory of your project. After that you user your code to check String is part of the word list you have it in your root directory or not.

If you are using java , you may do so as follow `

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);
    }
}

`

Moh K
  • 484
  • 1
  • 4
  • 17
  • Android has a spellcheck service you can leverage, so a word list is overkill. –  Aug 16 '17 at 18:21
0

I assume you are referring to english language here, there are multiple libraries available if you want to create a dictionary structure like jaazy, or google spell check api.

Although if you don't want all set of some language words and have a small set of words of your own..then

//Use a hashSet keep it static and load all the words in the hashSet //Then as a word is typed just use hashSet.contains() function to get if that element is there in hashSet or not [o(1) operation]. //your method can return whatever contains method returns.

Let me know if you need any more details.

-Sami