summaryrefslogtreecommitdiff
path: root/decoder/grammar_test.cc
blob: 11df1e3f96984d9271bb6eb345916af43d771f33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#define BOOST_TEST_MODULE g_test
#include <boost/test/unit_test.hpp>
#include <boost/test/floating_point_comparison.hpp>

#include <cassert>
#include <iostream>
#include <fstream>
#include <vector>
#include "trule.h"
#include "tdict.h"
#include "grammar.h"
#include "bottom_up_parser.h"
#include "hg.h"
#include "ff.h"
#include "ffset.h"
#include "weights.h"

using namespace std;

struct GrammarTest {
  GrammarTest() {
    std::string path(boost::unit_test::framework::master_test_suite().argc == 2 ? boost::unit_test::framework::master_test_suite().argv[1] : TEST_DATA);
    Weights::InitFromFile(path + "/weights.gt", &wts);
  }
  vector<weight_t> wts;
};

BOOST_FIXTURE_TEST_SUITE( s, GrammarTest );

BOOST_AUTO_TEST_CASE(TestTextGrammar) {
  vector<double> w;
  vector<const FeatureFunction*> ms;
  ModelSet models(w, ms);

  TextGrammar g;
  TRulePtr r1(new TRule("[X] ||| a b c ||| A B C ||| 0.1 0.2 0.3"));
  TRulePtr r2(new TRule("[X] ||| a b c ||| 1 2 3 ||| 0.2 0.3 0.4"));
  TRulePtr r3(new TRule("[X] ||| a b c d ||| A B C D ||| 0.1 0.2 0.3"));
  cerr << r1->AsString() << endl;
  g.AddRule(r1);
  g.AddRule(r2);
  g.AddRule(r3);
}

BOOST_AUTO_TEST_CASE(TestTextGrammarFile) {
  std::string path(boost::unit_test::framework::master_test_suite().argc == 2 ? boost::unit_test::framework::master_test_suite().argv[1] : TEST_DATA);
  GrammarPtr g(new TextGrammar(path + "/grammar.prune"));
  vector<GrammarPtr> grammars(1, g);

  LatticeArc a(TD::Convert("ein"), SparseVector<double>(), 1);
  LatticeArc b(TD::Convert("haus"), SparseVector<double>(), 1);
  Lattice lattice(2);
  lattice[0].push_back(a);
  lattice[1].push_back(b);
  Hypergraph forest;
  ExhaustiveBottomUpParser parser("PHRASE", grammars);
  parser.Parse(lattice, &forest);
  forest.PrintGraphviz();
}
BOOST_AUTO_TEST_SUITE_END()