summaryrefslogtreecommitdiff
path: root/decoder/minimal_decoder.cc
diff options
context:
space:
mode:
Diffstat (limited to 'decoder/minimal_decoder.cc')
-rw-r--r--decoder/minimal_decoder.cc45
1 files changed, 45 insertions, 0 deletions
diff --git a/decoder/minimal_decoder.cc b/decoder/minimal_decoder.cc
new file mode 100644
index 00000000..0aa281ae
--- /dev/null
+++ b/decoder/minimal_decoder.cc
@@ -0,0 +1,45 @@
+#include <fstream>
+#include <iostream>
+#include <sstream>
+
+#include "fdict.h"
+#include "filelib.h"
+#include "hg.h"
+#include "hg_io.h"
+#include "sparse_vector.h"
+#include "viterbi.h"
+
+
+using namespace std;
+
+/*
+ * Reads hypergraph from JSON file argv[1],
+ * reweights it using weights from argv[2],
+ * and outputs viterbi translation.
+ *
+ */
+int main(int argc, char** argv)
+{
+ ReadFile rf(argv[1]);
+ Hypergraph hg;
+ HypergraphIO::ReadFromJSON(rf.stream(), &hg);
+ SparseVector<double> v;
+ ifstream f(argv[2]);
+ string line;
+ while (getline(f, line)) {
+ istringstream ss(line);
+ string k; weight_t w;
+ ss >> k >> w;
+ v.add_value(FD::Convert(k), w);
+ }
+ hg.Reweight(v);
+ clock_t begin = clock();
+ hg.TopologicallySortNodesAndEdges(hg.NumberOfNodes()-1);
+ vector<WordID> trans;
+ ViterbiESentence(hg, &trans);
+ cout << TD::GetString(trans) << endl << flush;
+ clock_t end = clock();
+ double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
+ cout << elapsed_secs << " s" << endl;
+}
+