From 567f2bd17c05d31cd8a9b9d351f9030cddf53cb7 Mon Sep 17 00:00:00 2001
From: Patrick Simianer
Date: Wed, 9 Jul 2014 15:26:00 +0200
Subject: c++ implementation
---
fast/Makefile | 12 ++++
fast/README.md | 8 +++
fast/fast_weaver | Bin 0 -> 88433 bytes
fast/grammar.cc | 16 ++++++
fast/grammar.hh | 33 +++++++++++
fast/grammar.o | Bin 0 -> 2872 bytes
fast/hypergraph.cc | 161 +++++++++++++++++++++++++++++++++++++++++++++++++++++
fast/hypergraph.hh | 76 +++++++++++++++++++++++++
fast/hypergraph.o | Bin 0 -> 72616 bytes
fast/main.cc | 138 +++++++++++++++++++++++++++++++++++++++++++++
fast/semiring.hh | 36 ++++++++++++
11 files changed, 480 insertions(+)
create mode 100644 fast/Makefile
create mode 100644 fast/README.md
create mode 100755 fast/fast_weaver
create mode 100644 fast/grammar.cc
create mode 100644 fast/grammar.hh
create mode 100644 fast/grammar.o
create mode 100644 fast/hypergraph.cc
create mode 100644 fast/hypergraph.hh
create mode 100644 fast/hypergraph.o
create mode 100644 fast/main.cc
create mode 100644 fast/semiring.hh
diff --git a/fast/Makefile b/fast/Makefile
new file mode 100644
index 0000000..4aea1ac
--- /dev/null
+++ b/fast/Makefile
@@ -0,0 +1,12 @@
+all: hypergraph.o main.cc
+ clang -std=c++11 -lstdc++ main.cc hypergraph.o -o fast_weaver
+
+hypergraph.o: hypergraph.cc hypergraph.hh grammar.o semiring.hh
+ clang -std=c++11 -lstdc++ -c hypergraph.cc grammar.o
+
+grammar.o: grammar.cc grammar.hh
+ clang -std=c++11 -lstdc++ -c grammar.cc
+
+clean:
+ rm fast_weaver hypergraph.o grammar.o
+
diff --git a/fast/README.md b/fast/README.md
new file mode 100644
index 0000000..112a7ae
--- /dev/null
+++ b/fast/README.md
@@ -0,0 +1,8 @@
+TODO
+ * grammar
+ * parser
+ * other semirings
+ * sparse vector
+ * hg: json input (jsoncpp?)
+ * language model: kenlm
+
diff --git a/fast/fast_weaver b/fast/fast_weaver
new file mode 100755
index 0000000..7d349b3
Binary files /dev/null and b/fast/fast_weaver differ
diff --git a/fast/grammar.cc b/fast/grammar.cc
new file mode 100644
index 0000000..946b062
--- /dev/null
+++ b/fast/grammar.cc
@@ -0,0 +1,16 @@
+#include "grammar.hh"
+
+namespace Grammar {
+
+
+string
+NT::s()
+{
+ ostringstream os;
+ os << "NT<" << this->symbol << "," << this->index << ">";
+ return os.str();
+}
+
+
+} // namespace
+
diff --git a/fast/grammar.hh b/fast/grammar.hh
new file mode 100644
index 0000000..5625b85
--- /dev/null
+++ b/fast/grammar.hh
@@ -0,0 +1,33 @@
+#ifndef GRAMMAR_HH
+#define GRAMMAR_HH
+
+#include
+#include
+
+using namespace std;
+
+namespace Grammar {
+
+
+class NT {
+ public:
+ string symbol;
+ unsigned int index;
+
+ string s();
+};
+
+class T {
+ public:
+ string word;
+};
+
+class Rule {
+ public:
+};
+
+
+} // namespace
+
+#endif
+
diff --git a/fast/grammar.o b/fast/grammar.o
new file mode 100644
index 0000000..aae25db
Binary files /dev/null and b/fast/grammar.o differ
diff --git a/fast/hypergraph.cc b/fast/hypergraph.cc
new file mode 100644
index 0000000..08b4d05
--- /dev/null
+++ b/fast/hypergraph.cc
@@ -0,0 +1,161 @@
+#include "hypergraph.hh"
+
+namespace Hg {
+
+
+/*
+ * Node
+ * methods
+ *
+ */
+string
+Node::s()
+{
+ ostringstream os;
+ os << \
+ "Nodeid << \
+ ", symbol=" << this->symbol << \
+ ", span=(" << this->left << "," << this->right << ")" \
+ ", outgoing:" << this->outgoing.size() << \
+ ", incoming:" << this->incoming.size() << ">";
+ return os.str();
+}
+
+/*
+ * Hyperedge
+ * methods
+ *
+ */
+unsigned int
+Hyperedge::arity()
+{
+ return this->arity_;
+}
+
+bool
+Hyperedge::is_marked()
+{
+ return (this->arity_ == this->mark);
+}
+
+string
+Hyperedge::s()
+{
+ ostringstream os;
+ vector tails_ids;
+ ostringstream _;
+ for_each(this->tails.begin(), this->tails.end(), [&_](Node* n) { _ << n->id; _ << ","; });
+ os << \
+ "Hyperedgehead->id << \
+ ", rule:'" << "TODO" << \
+ "', tails=" << _.str() << \
+ ", arity=" << this->arity() << \
+ ", score=" << this->score << \
+ " , f=" << "TODO" << \
+ ", mark=" << this->mark << ">";
+ return os.str();
+}
+
+
+/*
+ * Hypergraph
+ * methods
+ *
+ */
+unsigned int
+Hypergraph::arity()
+{
+ // TODO
+ return 0;
+}
+
+void
+Hypergraph::reset()
+{
+}
+
+string
+Hypergraph::s()
+{
+ // TODO
+ return "";
+}
+
+string
+Hypergraph::json_s()
+{
+ // TODO
+ return "";
+}
+
+/*
+ * functions
+ *
+ */
+vector
+topological_sort(vector& nodes)
+{
+ vector sorted;
+ vector tmp;
+ for_each(nodes.begin(), nodes.end(), [&tmp](Node* n) -> void { if (n->incoming.size()==0) tmp.push_back(n); });
+ while (!tmp.empty()) {
+ Node* n = tmp.front();
+ cout << n->id << endl;
+ tmp.erase(tmp.begin());
+ sorted.push_back(n);
+ for (auto it = n->outgoing.begin(); it != n->outgoing.end(); it++) { // edges
+ if ((*it)->is_marked()) {
+ continue;
+ }
+ (*it)->mark++;
+ bool add = true;
+ for (auto jt = (*it)->head->incoming.begin(); jt != (*it)->head->incoming.end(); jt++) {
+ if (!(*jt)->is_marked()) {
+ add = false;
+ break;
+ }
+ }
+ if (add==true) {
+ tmp.push_back( (*it)->head );
+ cout << " " << (*it)->head->id<< endl;
+ }
+ }
+ }
+ return sorted;
+}
+
+void
+init(vector& nodes, ViterbiSemiring& semiring, Node* root)
+{
+ for (auto it = nodes.begin(); it != nodes.end(); ++it)
+ (*it)->score = semiring.null;
+ root->score = semiring.one;
+}
+
+void
+viterbi(vector& nodes, map nodes_by_id, Node* root)
+{
+ vector sorted = topological_sort(nodes);
+ ViterbiSemiring semiring;
+
+ init(sorted, semiring, root);
+
+ for (auto n_it = sorted.begin(); n_it != sorted.end(); ++n_it) {
+ for (auto e_it = (*n_it)->incoming.begin(); e_it != (*n_it)->incoming.end(); ++e_it) {
+ cout << (*e_it)->s() << endl;
+ double s = semiring.one;
+ for (auto m_it = (*e_it)->tails.begin(); m_it != (*e_it)->tails.end(); m_it++) {
+ s = semiring.multiply(s, (*m_it)->score);
+ }
+ (*n_it)->score = semiring.add((*n_it)->score, semiring.multiply(s, (*e_it)->score));
+ }
+ }
+
+ for (auto it = sorted.begin(); it != sorted.end(); ++it) {
+ cout << (*it)->id << " " << (*it)->score << endl;
+ }
+}
+
+
+} // namespace
+
diff --git a/fast/hypergraph.hh b/fast/hypergraph.hh
new file mode 100644
index 0000000..6e53045
--- /dev/null
+++ b/fast/hypergraph.hh
@@ -0,0 +1,76 @@
+#ifndef HYPERGRAPH_HH
+#define HYPERGRAPH_HH
+
+#include "grammar.hh"
+#include "semiring.hh"
+#include
+#include
+#include
+#include
+#include