00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "Alignment.h"
00021
00022 #include "phrase-extract/syntax-common/exception.h"
00023
00024 #include <algorithm>
00025 #include <cassert>
00026 #include <cstdlib>
00027
00028 namespace MosesTraining
00029 {
00030
00031 void ReadAlignment(const std::string &s, Alignment &a)
00032 {
00033 const std::string digits = "0123456789";
00034
00035 a.clear();
00036
00037 std::string::size_type begin = 0;
00038 while (true) {
00039 std::string::size_type end = s.find("-", begin);
00040 if (end == std::string::npos) {
00041 return;
00042 }
00043 int src = std::atoi(s.substr(begin, end-begin).c_str());
00044 if (end+1 == s.size()) {
00045 throw Syntax::Exception("Target index missing");
00046 }
00047
00048 begin = end+1;
00049 end = s.find_first_not_of(digits, begin+1);
00050 int tgt;
00051 if (end == std::string::npos) {
00052 tgt = std::atoi(s.substr(begin).c_str());
00053 a.push_back(std::make_pair(src, tgt));
00054 return;
00055 } else {
00056 tgt = std::atoi(s.substr(begin, end-begin).c_str());
00057 a.push_back(std::make_pair(src, tgt));
00058 }
00059 begin = end+1;
00060 }
00061 }
00062
00063 void FlipAlignment(Alignment &a)
00064 {
00065 for (Alignment::iterator p = a.begin(); p != a.end(); ++p) {
00066 std::swap(p->first, p->second);
00067 }
00068 }
00069
00070 }