aspell-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [aspell] sample use of aspell as a library?


From: Kevin Atkinson
Subject: Re: [aspell] sample use of aspell as a library?
Date: Thu, 2 Mar 2000 18:17:29 -0500 (EST)

On Thu, 2 Mar 2000, Michael A. Schoen wrote:

> I'm trying to use aspell as a library in an application. I understand that 
> the C library is currently under construction, but I'm wondering if it's 
> still possible to use aspell as a library in a C++ application.
> 
> I've tried going through aspell.cc, but I'm having trouble -- in particular 
> I'm pretty unfamiliar with the use of templates.
> 
> My use of aspell will be of the most trivial kind. All I need to do is to 
> spell check a string, and get back an array of strings of the incorrectly 
> spelled words. My goal is actually to include this in a Java application 
> (using the native interface), so my usage would look like...
> 
>     Aspell aspell = new Aspell(); // instantiates a "manager" component, and 
> does whatever initialization is required.
> 
>     ...
> 
> 
>     String foo = "this is a stringg I want to check";
>     String[] errors = aspell.check(foo);
> 
> And in this case errors would have 1 element, of the string "stringg".
> 
> Does anyone have any simple sample code I could leverage to accomplish this?

This should work:

#include <iostream>

#include <aspell/manager.hh>
#include <aspell/check.hh>
#include <aspell/config.hh>
#include <aspell/clone_ptr-t.hh>

int main() {
  using namespace aspell;

  Manager manager("");
  CheckState<string::const_iterator> state(manager);

  string line;
  vector<string> errors;

  getline(cin, line);
  line += '\n';

  state.restart(line.begin(), line.end());
  // initializes the state with the string
  // needs to be done when ever the string changed.

  while (state.advance(), 
         // advanced the state from the last error or from the beggining
         check(state), 
         // checks the string stopping at the next error
         !state.at_end()
         // it the state is at the end there are no more errors
         ) 
    {
      errors.push_back(state.word());
    }

  // print out all the errors.
  for (int i = 0; i != errors.size(); ++i) {
    cout << errors[i] << "\n";
  }

}


When compiling remember to link it with the aspell library using "-laspell".
The CheckState stuff is not very stable so be aware that this may change
a bit between releases.  Let me know if you have any other problems.

---
Kevin Atkinson
address@hidden
http://metalab.unc.edu/kevina/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]