summaryrefslogtreecommitdiff
path: root/utils/alignment_io.cc
diff options
context:
space:
mode:
authorChris Dyer <cdyer@cs.cmu.edu>2012-03-24 23:04:46 -0400
committerChris Dyer <cdyer@cs.cmu.edu>2012-03-24 23:04:46 -0400
commitb6eede632af4fa58a6f5325ee0d059c02a898b9f (patch)
tree4d29d1d1d700b9540af213bac32ffda96046abe1 /utils/alignment_io.cc
parent0c7e078d14dd7078ec4a5b3e77007609aec5e54c (diff)
rename aligner, add support for distinguishing translation / transliteration
Diffstat (limited to 'utils/alignment_io.cc')
-rw-r--r--utils/alignment_io.cc97
1 files changed, 97 insertions, 0 deletions
diff --git a/utils/alignment_io.cc b/utils/alignment_io.cc
new file mode 100644
index 00000000..1d923f7f
--- /dev/null
+++ b/utils/alignment_io.cc
@@ -0,0 +1,97 @@
+#include "utils/alignment_io.h"
+
+using namespace std;
+
+static bool is_digit(char x) { return x >= '0' && x <= '9'; }
+
+boost::shared_ptr<Array2D<bool> > AlignmentIO::ReadPharaohAlignmentGrid(const string& al) {
+ int max_x = 0;
+ int max_y = 0;
+ int i = 0;
+ size_t pos = al.rfind(" ||| ");
+ if (pos != string::npos) { i = pos + 5; }
+ while (i < al.size()) {
+ if (al[i] == '\n' || al[i] == '\r') break;
+ int x = 0;
+ while(i < al.size() && is_digit(al[i])) {
+ x *= 10;
+ x += al[i] - '0';
+ ++i;
+ }
+ if (x > max_x) max_x = x;
+ assert(i < al.size());
+ if(al[i] != '-') {
+ cerr << "BAD ALIGNMENT: " << al << endl;
+ abort();
+ }
+ ++i;
+ int y = 0;
+ while(i < al.size() && is_digit(al[i])) {
+ y *= 10;
+ y += al[i] - '0';
+ ++i;
+ }
+ if (y > max_y) max_y = y;
+ while(i < al.size() && al[i] == ' ') { ++i; }
+ }
+
+ boost::shared_ptr<Array2D<bool> > grid(new Array2D<bool>(max_x + 1, max_y + 1));
+ i = 0;
+ if (pos != string::npos) { i = pos + 5; }
+ while (i < al.size()) {
+ if (al[i] == '\n' || al[i] == '\r') break;
+ int x = 0;
+ while(i < al.size() && is_digit(al[i])) {
+ x *= 10;
+ x += al[i] - '0';
+ ++i;
+ }
+ assert(i < al.size());
+ assert(al[i] == '-');
+ ++i;
+ int y = 0;
+ while(i < al.size() && is_digit(al[i])) {
+ y *= 10;
+ y += al[i] - '0';
+ ++i;
+ }
+ (*grid)(x, y) = true;
+ while(i < al.size() && al[i] == ' ') { ++i; }
+ }
+ // cerr << *grid << endl;
+ return grid;
+}
+
+void AlignmentIO::SerializePharaohFormat(const Array2D<bool>& alignment, ostream* o) {
+ ostream& out = *o;
+ bool need_space = false;
+ for (int i = 0; i < alignment.width(); ++i)
+ for (int j = 0; j < alignment.height(); ++j)
+ if (alignment(i,j)) {
+ if (need_space) out << ' '; else need_space = true;
+ out << i << '-' << j;
+ }
+ out << endl;
+}
+
+void AlignmentIO::SerializeTypedAlignment(const Array2D<AlignmentType>& alignment, ostream* o) {
+ ostream& out = *o;
+ bool need_space = false;
+ for (int i = 0; i < alignment.width(); ++i)
+ for (int j = 0; j < alignment.height(); ++j) {
+ const AlignmentType& aij = alignment(i,j);
+ if (aij != kNONE) {
+ if (need_space) out << ' '; else need_space = true;
+ if (aij == kTRANSLATION) {}
+ else if (aij == kTRANSLITERATION) {
+ out << 'T' << ':';
+ } else {
+ cerr << "\nUnexpected alignment point type: " << static_cast<int>(aij) << endl;
+ abort();
+ }
+ out << i << '-' << j;
+ }
+ }
+ out << endl;
+}
+