Difference between revisions of "Lttoolbox API examples"

From Apertium
Jump to navigation Jump to search
m (oops)
(that version only works for 'beer')
Line 34: Line 34:
int main (int argc, char** argv)
int main (int argc, char** argv)
{
{
Alphabet alphabet;
Alphabet alphabet;
Transducer t;
Transducer t;


// Set locale
// Set locale
LtLocale::tryToSetLocale();
LtLocale::tryToSetLocale();


// Include symbols into alphabet, keeping the values
// Include symbols into alphabet, keeping the values
alphabet.includeSymbol(L"<n>");
alphabet.includeSymbol(L"<n>");
alphabet.includeSymbol(L"<sg>");
alphabet.includeSymbol(L"<sg>");
alphabet.includeSymbol(L"<pl>");
alphabet.includeSymbol(L"<pl>");


int n_sym = alphabet(L"<n>");
int n_sym = alphabet(L"<n>");
int sg_sym = alphabet(L"<sg>");
int sg_sym = alphabet(L"<sg>");
int pl_sym = alphabet(L"<pl>");
int pl_sym = alphabet(L"<pl>");
// Initial state
// Initial state
int initial = t.getInitial();
int initial = t.getInitial();


// build "beer" manually
// build "beer" manually
int beer = initial;
int beer = initial;
// these are the transitions b:b e:e e:e r:r
// these are the transitions b:b e:e e:e r:r
beer = t.insertSingleTransduction(alphabet(L'b',L'b'), beer);
beer = t.insertSingleTransduction(alphabet(L'b',L'b'), beer);
beer = t.insertSingleTransduction(alphabet(L'e',L'e'), beer);
beer = t.insertSingleTransduction(alphabet(L'e',L'e'), beer);
beer = t.insertSingleTransduction(alphabet(L'e',L'e'), beer);
beer = t.insertSingleTransduction(alphabet(L'e',L'e'), beer);
beer = t.insertSingleTransduction(alphabet(L'r',L'r'), beer);
beer = t.insertSingleTransduction(alphabet(L'r',L'r'), beer);
// these are 0:<n> 0:<sg>
// these are 0:<n> 0:<sg>
beer = t.insertSingleTransduction(alphabet(0, n_sym), beer);
beer = t.insertSingleTransduction(alphabet(0, n_sym), beer);
beer = t.insertSingleTransduction(alphabet(0, sg_sym), beer);
beer = t.insertSingleTransduction(alphabet(0, sg_sym), beer);
t.setFinal(beer);
t.setFinal(beer);


// build "beers" manually
//#if 0
// build "beers" manually
int beers = t.getInitial();
int beers = t.getInitial();
beers = t.insertSingleTransduction(alphabet(L'b',L'b'), beers);
beer = t.insertSingleTransduction(alphabet(L'b',L'b'), beer);
beers = t.insertSingleTransduction(alphabet(L'e',L'e'), beers);
beer = t.insertSingleTransduction(alphabet(L'e',L'e'), beer);
beers = t.insertSingleTransduction(alphabet(L'e',L'e'), beers);
beer = t.insertSingleTransduction(alphabet(L'e',L'e'), beer);
beers = t.insertSingleTransduction(alphabet(L'r',L'r'), beers);
beer = t.insertSingleTransduction(alphabet(L'r',L'r'), beer);


// this transition is s:0
// this transition is s:0
beers = t.insertSingleTransduction(alphabet(L's', 0), beers);
beers = t.insertSingleTransduction(alphabet(L's', 0), beers);
beers = t.insertSingleTransduction(alphabet(0, n_sym), beers);
beers = t.insertSingleTransduction(alphabet(0, n_sym), beers);
beers = t.insertSingleTransduction(alphabet(0, pl_sym), beers);
beers = t.insertSingleTransduction(alphabet(0, pl_sym), beers);
t.setFinal(beers);
t.setFinal(beers);
//#endif


t.minimize();
t.minimize();
t.joinFinals();


//Rather than try to convert between Transducer and TransExe, we'll
//Rather than try to convert between Transducer and TransExe, we'll
//just write and read.
//just write and read.
FILE* fst=fopen("beer.fst", "w");
FILE* fst=fopen("beer.fst", "w");
t.write(fst);
t.write(fst);
fclose(fst);
fclose(fst);
fst=fopen("beer.fst", "r");
fst=fopen("beer.fst", "r");


TransExe te;
TransExe te;
te.read(fst, alphabet);
te.read(fst, alphabet);
fclose(fst);
fclose(fst);


Pool<vector<int> > *pool = new Pool<vector<int> >(1, vector<int>(50));
Pool<vector<int> > *pool = new Pool<vector<int> >(1, vector<int>(50));
State *initial_state = new State(pool);
State *initial_state = new State(pool);
initial_state->init(te.getInitial());
initial_state->init(te.getInitial());
State current_state = *initial_state;
// Node root;
// root.addTransition(0, 0, te.getInitial());
// initial_state->init(&root);
State current_state = *initial_state;


wstring input, output=L"";
wstring input, output=L"";


set<Node *> anfinals;
set<Node *> anfinals;
anfinals.insert(te.getFinals().begin(), te.getFinals().end());
anfinals.insert(te.getFinals().begin(), te.getFinals().end());


FILE* in=stdin;
cout << "Finals: " << te.getFinals().size() <<endl;


bool reading=true;
FILE* in=stdin;
// This is our runtime: see if the input matches

bool reading=true;
while (reading)
{
// This is our runtime: see if the input matches
wchar_t val = (wchar_t)fgetwc(in);
while (reading)
if(val==WEOF||iswspace(val))
{
{
wchar_t val = (wchar_t)fgetwc(in);
reading=false;
if(val==WEOF||iswspace(val))
{
}
reading=false;
else
}
{
if (!reading)
else
{
{
// At the end. We don't need to do anything but
if (!reading || current_state.isFinal(anfinals))
// leave the loop in this simplistic example
{
break;
// At the end. We don't need to do anything but
}
// leave the loop in this simplistic example
break;
else
}
{
current_state.step(val);
else
alphabet.getSymbol(input, val);
{
current_state.step(val);
alphabet.getSymbol(input, val);
}
}
}
}
}
if(!current_state.isFinal(anfinals)) wcout << L"saw: " << val << endl;
}


// Not used, just don't want it to be empty...
// Not used, just don't want it to be empty...
if (current_state.isFinal(anfinals))
set<wchar_t> escaped;
{
escaped.insert(L'$');
set<wchar_t> escaped;
output = current_state.filterFinals(anfinals, alphabet, escaped);
escaped.insert(L'$');
output = current_state.filterFinals(anfinals, alphabet, escaped);


wcout << L"Input: " << endl;
wcout << input << output << endl;
}
wcout << input << endl;
else
wcout << L"Output: " << endl;
{
wcout << output << endl;
wcout << L"Unrecognised: " << input << endl;
}


return 0;
return 0;
}
}
</pre>
</pre>

Revision as of 18:53, 23 February 2011

This is a simple C++/lttoolbox version of the 'beer' program in Morphological dictionaries

// g++ -I/usr/local/include/lttoolbox-3.2 -I/usr/local/lib -llttoolbox3 lt_beer.cc -o lt-beer

#include <cwchar>
#include <cstdio>
#include <cerrno>
#include <string>
#include <iostream>
#include <list>
#include <set>

#include <lttoolbox/ltstr.h>

// LtLocale::tryToSetLocale()
#include <lttoolbox/lt_locale.h>

// Transducer class
#include <lttoolbox/transducer.h>

// Alphabet class
#include <lttoolbox/alphabet.h>

// Pool class
#include <lttoolbox/pool.h>

// State class
#include <lttoolbox/state.h>

// TransExe class
#include <lttoolbox/trans_exe.h>

int main (int argc, char** argv)
{
  Alphabet alphabet;
  Transducer t;

  // Set locale
  LtLocale::tryToSetLocale();

  // Include symbols into alphabet, keeping the values
  alphabet.includeSymbol(L"<n>");
  alphabet.includeSymbol(L"<sg>");
  alphabet.includeSymbol(L"<pl>");

  int n_sym = alphabet(L"<n>");
  int sg_sym = alphabet(L"<sg>");
  int pl_sym = alphabet(L"<pl>");
  // Initial state
  int initial = t.getInitial();

  // build "beer" manually
  int beer = initial;
  // these are the transitions b:b e:e e:e r:r
  beer = t.insertSingleTransduction(alphabet(L'b',L'b'), beer);
  beer = t.insertSingleTransduction(alphabet(L'e',L'e'), beer);
  beer = t.insertSingleTransduction(alphabet(L'e',L'e'), beer);
  beer = t.insertSingleTransduction(alphabet(L'r',L'r'), beer);
  // these are 0:<n> 0:<sg>
  beer = t.insertSingleTransduction(alphabet(0, n_sym), beer);
  beer = t.insertSingleTransduction(alphabet(0, sg_sym), beer);
  t.setFinal(beer);

  // build "beers" manually
  int beers = t.getInitial();
  beers = t.insertSingleTransduction(alphabet(L'b',L'b'), beers);
  beers = t.insertSingleTransduction(alphabet(L'e',L'e'), beers);
  beers = t.insertSingleTransduction(alphabet(L'e',L'e'), beers);
  beers = t.insertSingleTransduction(alphabet(L'r',L'r'), beers);

  // this transition is s:0
  beers = t.insertSingleTransduction(alphabet(L's', 0), beers);
  beers = t.insertSingleTransduction(alphabet(0, n_sym), beers);
  beers = t.insertSingleTransduction(alphabet(0, pl_sym), beers);
  t.setFinal(beers);

  t.minimize();

  //Rather than try to convert between Transducer and TransExe, we'll
  //just write and read.
  FILE* fst=fopen("beer.fst", "w");
  t.write(fst);
  fclose(fst);
  fst=fopen("beer.fst", "r");

  TransExe te;
  te.read(fst, alphabet);
  fclose(fst);

  Pool<vector<int> > *pool = new Pool<vector<int> >(1, vector<int>(50));
  State *initial_state = new State(pool);
  initial_state->init(te.getInitial());
  State current_state = *initial_state;

  wstring input, output=L"";

  set<Node *> anfinals;
  anfinals.insert(te.getFinals().begin(), te.getFinals().end());

  FILE* in=stdin;

  bool reading=true;
  // This is our runtime: see if the input matches
  while (reading)
  {
    wchar_t val = (wchar_t)fgetwc(in);
    if(val==WEOF||iswspace(val))
    {
      reading=false;
    }
    else
    {
      if (!reading)
      {
        // At the end. We don't need to do anything but
        // leave the loop in this simplistic example
        break;
      }
      else
      {
        current_state.step(val);
        alphabet.getSymbol(input, val);
      }
    }
  }

  // Not used, just don't want it to be empty...
  if (current_state.isFinal(anfinals))
  {
    set<wchar_t> escaped;
    escaped.insert(L'$');
    output = current_state.filterFinals(anfinals, alphabet, escaped);

    wcout << input << output << endl;
  }
  else
  {
    wcout << L"Unrecognised: " << input << endl;
  }

  return 0;
}