Question

I'm looking for an easy way to check whether a certain string is a correctly-spelled English word. For example, 'looked' would return True while 'hurrr' would return False. I don't need spelling suggestions or any spelling-correcting features. Just a simple function that takes a string and returns a boolean value.

I could do this easily with Python using PyEnchant, but it seems you have to compile the library yourself if you want to use it with MS Visual C++.

Was it helpful?

Solution

PyEnchant is based on Enchant, which is a C library providing C and C++ interfaces. So you can just use that for C++. The minimal example will be something like this:

#include <memory>
#include <cstdio>

#include "enchant.h"
#include "enchant++.h"

int main ()
{
    try
        {
            enchant::Broker *broker = enchant::Broker::instance ();
        std::auto_ptr<enchant::Dict> dict (broker->request_dict ("en_US"));

            const char *check_checks[] = { "hello", "helllo" };
            for (int i = 0; i < (sizeof (check_checks) / sizeof (check_checks[0])); ++i)
        {
            printf ("enchant_dict_check (%s): %d\n", check_checks[i],
                dict->check (check_checks[i]) == false);
        }

    } catch (const enchant::Exception &) {
        return 1;
    }
}

For more examples/tests, see their SVN repository.

OTHER TIPS

If you want to implement such function on your own, you'll need a database to query in order to find out whether a given word is valid (usually a plain text file is enough, like /usr/share/dict/words on Linux).

Otherwise you could rely upon a third party spellcheck library that does just that.

You could take one of the GNU dictionaries out there (like /usr/share/dict/words as mentioned) and build it into an appropriate data structure that'll give you fast lookup and membership checking depending on your performance needs, something like a directed acyclic word graph or even just a trie might be sufficient.

You'd need a word list, for starters. (/usr/share/dict/words maybe?)

You should read your word list into a std::set. Then a correct-spelling test consists simply of checking all the user input words for whether or not they are in the set.

bool spell_check(std::string const& str)
{
  std::cout << "Is '" << str << "' spelled correctly? ";
  std::string input;
  std::getline(input);

  return input[0] == 'y' || input[0] == 'Y';
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top